wu-sheng commented on a change in pull request #7554:
URL: https://github.com/apache/skywalking/pull/7554#discussion_r697883079



##########
File path: 
oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/TraceSamplingPolicyWatcher.java
##########
@@ -0,0 +1,217 @@
+/*
+ * 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;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.apm.util.StringUtil;
+import org.apache.skywalking.oap.server.analyzer.module.AnalyzerModule;
+import org.apache.skywalking.oap.server.analyzer.provider.AnalyzerModuleConfig;
+import org.apache.skywalking.oap.server.configuration.api.ConfigChangeWatcher;
+import org.apache.skywalking.oap.server.core.Const;
+import org.apache.skywalking.oap.server.library.module.ModuleProvider;
+import org.apache.skywalking.oap.server.library.util.ResourceUtils;
+import 
org.apache.skywalking.oap.server.library.util.yaml.ClassFilterConstructor;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.Reader;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static java.util.Objects.isNull;
+
+@Slf4j
+public class TraceSamplingPolicyWatcher extends ConfigChangeWatcher {
+
+    private final AtomicReference<String> settingsString = new 
AtomicReference<>(Const.EMPTY_STRING);
+    private final AtomicReference<SamplePolicySettings> samplePolicySettings = 
new AtomicReference<>(null);
+    private final AnalyzerModuleConfig moduleConfig;
+
+    public TraceSamplingPolicyWatcher(AnalyzerModuleConfig moduleConfig, 
ModuleProvider provider) {
+        super(AnalyzerModule.NAME, provider, "traceSamplingPolicy");
+        this.moduleConfig = moduleConfig;
+        loadDefaultPolicy();
+    }
+
+    @Override
+    public void notify(ConfigChangeEvent value) {
+        if (EventType.DELETE.equals(value.getEventType())) {
+            this.settingsString.set("");
+            log.info("[trace-sampling-policy] Delete trace-sample-policy,use 
default config");
+            loadDefaultPolicy();
+        } else {
+            activeSetting(value.getNewValue());
+        }
+    }
+
+    @Override
+    public String value() {
+        return this.settingsString.get();
+    }
+
+    public SamplePolicy getSamplePolicy(String service) {
+        return this.samplePolicySettings.get().getPolicy(service);
+    }
+
+    public int getSampleRate() {
+        return this.samplePolicySettings.get().global.rate;
+    }
+
+    public int getSlowTraceSegmentThreshold() {
+        return this.samplePolicySettings.get().global.duration;
+    }
+
+    public boolean shouldSample(int duration) {
+        return (this.samplePolicySettings.get().global.duration > -1) && 
(duration >= this.samplePolicySettings.get().global.duration);
+    }
+
+    private void loadDefaultPolicy() {
+        SamplePolicySettings rateSetting = 
parseFromFile(moduleConfig.getTraceSamplingPolicySettingsFile());
+        // If settingFile has a empty config, use the default settings
+        this.samplePolicySettings.set(rateSetting == null ? 
defaultSampleConfigSettings() : rateSetting);
+        log.info("[trace-sampling-policy] Default configured 
trace-sample-policy: {}", this.samplePolicySettings);
+    }
+
+    private void activeSetting(String config) {
+        if (log.isDebugEnabled()) {
+            log.debug("[trace-sampling-policy] Updating using new config: {}", 
config);
+        }
+        onUpdated(parseFromYml(config));
+    }
+
+    private void onUpdated(final SamplePolicySettings samplePolicySettings) {
+        if (!isNull(samplePolicySettings)) {
+            this.samplePolicySettings.set(samplePolicySettings);
+            log.info("[trace-sampling-policy] Updating trace-sample-policy 
with: {}", samplePolicySettings);
+        } else if (StringUtil.isBlank(this.settingsString.get())) {
+            log.info("[trace-sampling-policy] Trace-sample-policy been set 
empty string,use default config");
+            loadDefaultPolicy();
+        } else {
+            log.info("[trace-sampling-policy] Parse yaml fail, retain last 
configuration: {}", this.samplePolicySettings);
+        }
+    }
+
+    private SamplePolicySettings parseFromFile(final String file) {
+        try {
+            final Reader reader = ResourceUtils.read(file);
+            Map<String, Object> map = new Yaml(new ClassFilterConstructor(new 
Class[]{

Review comment:
       This is a wrong usage of API. You should use `SafeConstructor`. Take 
`RulesReader` as a reference. 
   The `ClassFilterConstructor` is designed for requiring initialization from 
customer Java class.




-- 
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]


Reply via email to