wu-sheng commented on a change in pull request #7554: URL: https://github.com/apache/skywalking/pull/7554#discussion_r698214053
########## File path: oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/sampling/SamplingPolicySettingsReader.java ########## @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.skywalking.oap.server.analyzer.provider.trace.sampling; + +import org.apache.skywalking.apm.util.StringUtil; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.SafeConstructor; + +import java.io.InputStream; +import java.io.Reader; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * SamplePolicySettingsReader parses the given `trace-sampling-policy-settings.yml` config file, to the target {@link SamplingPolicySettings}. + */ +public class SamplingPolicySettingsReader { + private Map yamlData; + + public SamplingPolicySettingsReader(InputStream inputStream) { + Yaml yaml = new Yaml(new SafeConstructor()); + yamlData = (Map) yaml.load(inputStream); + } + + public SamplingPolicySettingsReader(Reader io) { + Yaml yaml = new Yaml(new SafeConstructor()); + yamlData = (Map) yaml.load(io); + } + + /** + * Read policy config file to {@link SamplingPolicySettings} + */ + public SamplingPolicySettings readSettings() { + SamplingPolicySettings samplingPolicySettings = new SamplingPolicySettings(); + if (Objects.nonNull(yamlData)) { + readDefaultSamplingPolicy(samplingPolicySettings); + readServicesSamplingPolicy(samplingPolicySettings); + } + return samplingPolicySettings; + } + + private void readDefaultSamplingPolicy(SamplingPolicySettings samplingPolicySettings) { + Map<String, Object> objectMap = (Map<String, Object>) yamlData.get("default"); + if (objectMap == null) { + return; + } + if (objectMap.get("rate") != null) { + samplingPolicySettings.getDefaultPolicy().setRate((Integer) objectMap.get("rate")); + } + if (objectMap.get("duration") != null) { + samplingPolicySettings.getDefaultPolicy().setDuration((Integer) objectMap.get("duration")); + } Review comment: I think you should use `"abc"` in the YAML? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
