kezhenxu94 commented on a change in pull request #7554: URL: https://github.com/apache/skywalking/pull/7554#discussion_r698117457
########## 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); Review comment: Casting `yaml.load` to `Map` is redundant ########## 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: Here is one caveat: when the settings is configured via dynamic configuration, and the configured policy is ```yaml default: rate: 123 duration: non-a-number ``` The default policy is **partially** updated, i.e., the `rate` is updated while the `duration` is not updated because it has an invalid value, and if I were a user, I'd expect the `rate` and `duration` to be updated both or none ########## 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); Review comment: Casting `yaml.load` to `Map` is redundant ########## File path: oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/AnalyzerModuleProvider.java ########## @@ -111,18 +107,17 @@ public void prepare() throws ServiceNotProvidedException, ModuleStartException { public void start() throws ModuleStartException { // load official analysis getManager().find(CoreModule.NAME) - .provider() - .getService(OALEngineLoaderService.class) - .load(CoreOALDefine.INSTANCE); + .provider() + .getService(OALEngineLoaderService.class) + .load(CoreOALDefine.INSTANCE); Review comment: Please import our code style settings, and be aware of these unrelated changes when submitting an PR ########## File path: oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/sampling/SamplingPolicySettings.java ########## @@ -0,0 +1,50 @@ +/* + * 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 lombok.Getter; +import lombok.ToString; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +@ToString +public class SamplingPolicySettings { + + @Getter + private SamplingPolicy defaultPolicy; + private Map<String, SamplingPolicy> services; + + /** + * The sample rate precision is 1/10000. 10000 means 100% sample in default. + * Setting this threshold about the latency would make the slow trace segments sampled if they cost more time, even the sampling mechanism activated. The default value is `-1`, which means would not sample slow traces. Unit, millisecond. + */ + public SamplingPolicySettings() { + SamplingPolicy samplingPolicy = new SamplingPolicy(10000, -1); Review comment: Local variable 'samplingPolicy' is redundant ########## File path: oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/parser/listener/TraceSegmentSampler.java ########## @@ -18,20 +18,24 @@ package org.apache.skywalking.oap.server.analyzer.provider.trace.parser.listener; -import org.apache.skywalking.oap.server.analyzer.provider.trace.TraceSampleRateWatcher; +import org.apache.skywalking.apm.network.language.agent.v3.SegmentObject; +import org.apache.skywalking.oap.server.analyzer.provider.trace.TraceSamplingPolicyWatcher; /** * The sampler makes the sampling mechanism works at backend side. Sample result: [0,sampleRate) sampled, (sampleRate,~) * ignored */ public class TraceSegmentSampler { - private TraceSampleRateWatcher traceSampleRateWatcher; + private TraceSamplingPolicyWatcher traceSamplingPolicyWatcher; Review comment: Field 'traceSamplingPolicyWatcher' may be 'final' and let's use `@RequiredArgsConstructor` ########## File path: oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/TraceSamplingPolicyWatcher.java ########## @@ -0,0 +1,187 @@ +/* + * 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.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.analyzer.provider.trace.sampling.SamplingPolicy; +import org.apache.skywalking.oap.server.analyzer.provider.trace.sampling.SamplingPolicySettings; +import org.apache.skywalking.oap.server.analyzer.provider.trace.sampling.SamplingPolicySettingsReader; +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 java.io.StringReader; +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<SamplingPolicySettings> samplingPolicySettings = new AtomicReference<>(null); + private final SamplingPolicySettings defaultSamplingPolicySettings; + + public TraceSamplingPolicyWatcher(AnalyzerModuleConfig moduleConfig, ModuleProvider provider) { + super(AnalyzerModule.NAME, provider, "traceSamplingPolicy"); + SamplingPolicySettings samplingPolicySettings = parseFromFile(moduleConfig.getTraceSamplingPolicySettingsFile()); Review comment: Local variable 'samplingPolicySettings' is redundant -- 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]
