This is an automated email from the ASF dual-hosted git repository. tanjian pushed a commit to branch feature-trace-ignore-dynamic in repository https://gitbox.apache.org/repos/asf/skywalking.git
commit 87fe80b9217b1d103f71216b3c0001e48f204323 Author: JaredTan95 <[email protected]> AuthorDate: Wed Jan 27 14:25:09 2021 +0800 agent update agent.trace.ignore_path within CDS. --- CHANGES.md | 2 +- .../trace/ignore/TraceIgnoreExtendService.java | 23 +++++-- .../trace/ignore/TraceIgnorePatternWatcher.java | 72 ++++++++++++++++++++++ .../ignore/TraceIgnorePatternWatcherTest.java | 57 +++++++++++++++++ 4 files changed, 149 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c8500ec..0647f94 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -37,7 +37,7 @@ Release Notes. * Support collecting logs and forwarding through gRPC. * Support config `agent.sample_n_per_3_secs` can be changed in the runtime. * Support DNS periodic resolving mechanism to update backend service. - +* Support config `agent.trace.ignore_path` can be changed in the runtime. #### OAP-Backend * Make meter receiver support MAL. * Support influxDB connection response format option. Fix some error when use JSON as influxDB response format. diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreExtendService.java b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreExtendService.java index baaac8b..22e74c8 100644 --- a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreExtendService.java +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreExtendService.java @@ -19,6 +19,8 @@ package org.apache.skywalking.apm.plugin.trace.ignore; import org.apache.skywalking.apm.agent.core.boot.OverrideImplementor; +import org.apache.skywalking.apm.agent.core.boot.ServiceManager; +import org.apache.skywalking.apm.agent.core.conf.dynamic.ConfigurationDiscoveryService; import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; import org.apache.skywalking.apm.agent.core.sampling.SamplingService; @@ -34,6 +36,13 @@ public class TraceIgnoreExtendService extends SamplingService { private static final String PATTERN_SEPARATOR = ","; private TracePathMatcher pathMatcher = new FastPathMatcher(); private String[] patterns = new String[] {}; + private TraceIgnorePatternWatcher traceIgnorePatternWatcher; + + @Override + public void prepare() { + super.prepare(); + traceIgnorePatternWatcher = new TraceIgnorePatternWatcher("agent.trace.ignore_path", this); + } @Override public void boot() { @@ -43,11 +52,11 @@ public class TraceIgnoreExtendService extends SamplingService { if (StringUtil.isNotEmpty(IgnoreConfig.Trace.IGNORE_PATH)) { patterns = IgnoreConfig.Trace.IGNORE_PATH.split(PATTERN_SEPARATOR); } - } - @Override - public void prepare() { - super.prepare(); + ServiceManager.INSTANCE.findService(ConfigurationDiscoveryService.class) + .registerAgentConfigChangeWatcher(traceIgnorePatternWatcher); + + handleTraceIgnorePatternsChanged(); } @Override @@ -76,4 +85,10 @@ public class TraceIgnoreExtendService extends SamplingService { public void forceSampled() { super.forceSampled(); } + + void handleTraceIgnorePatternsChanged() { + if (StringUtil.isNotBlank(traceIgnorePatternWatcher.getTraceIgnorePathPatterns())) { + patterns = traceIgnorePatternWatcher.getTraceIgnorePathPatterns().split(PATTERN_SEPARATOR); + } + } } diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnorePatternWatcher.java b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnorePatternWatcher.java new file mode 100644 index 0000000..d5e1c3d --- /dev/null +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnorePatternWatcher.java @@ -0,0 +1,72 @@ +/* + * 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.apm.plugin.trace.ignore; + +import java.util.concurrent.atomic.AtomicReference; +import org.apache.skywalking.apm.agent.core.conf.dynamic.AgentConfigChangeWatcher; +import org.apache.skywalking.apm.agent.core.logging.api.ILog; +import org.apache.skywalking.apm.agent.core.logging.api.LogManager; +import org.apache.skywalking.apm.plugin.trace.ignore.conf.IgnoreConfig; + +public class TraceIgnorePatternWatcher extends AgentConfigChangeWatcher { + private static final ILog LOGGER = LogManager.getLogger(TraceIgnorePatternWatcher.class); + + private final AtomicReference<String> traceIgnorePathPatterns; + private final TraceIgnoreExtendService traceIgnoreExtendService; + + public TraceIgnorePatternWatcher(final String propertyKey, TraceIgnoreExtendService traceIgnoreExtendService) { + super(propertyKey); + this.traceIgnorePathPatterns = new AtomicReference(getDefaultValue()); + this.traceIgnoreExtendService = traceIgnoreExtendService; + } + + private void activeSetting(String config) { + if (LOGGER.isDebugEnable()) { + LOGGER.debug("Updating using new static config: {}", config); + } + try { + this.traceIgnorePathPatterns.set(config); + traceIgnoreExtendService.handleTraceIgnorePatternsChanged(); + } catch (NumberFormatException ex) { + LOGGER.error(ex, "Cannot load {} from: {}", getPropertyKey(), config); + } + } + + @Override + public void notify(final ConfigChangeEvent value) { + if (EventType.DELETE.equals(value.getEventType())) { + activeSetting(getDefaultValue()); + } else { + activeSetting(value.getNewValue()); + } + } + + @Override + public String value() { + return traceIgnorePathPatterns.get(); + } + + private String getDefaultValue() { + return IgnoreConfig.Trace.IGNORE_PATH; + } + + public String getTraceIgnorePathPatterns() { + return traceIgnorePathPatterns.get(); + } +} diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/test/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnorePatternWatcherTest.java b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/test/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnorePatternWatcherTest.java new file mode 100644 index 0000000..b37361f --- /dev/null +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/test/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnorePatternWatcherTest.java @@ -0,0 +1,57 @@ +/* + * 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.apm.plugin.trace.ignore; + +import org.apache.skywalking.apm.agent.core.conf.dynamic.AgentConfigChangeWatcher; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +public class TraceIgnorePatternWatcherTest { + private TraceIgnoreExtendService traceIgnoreExtendService = new TraceIgnoreExtendService(); + + @Before + public void setUp() { + traceIgnoreExtendService.prepare(); + } + + @Test + public void testConfigModifyEvent() { + TraceIgnorePatternWatcher traceIgnorePatternWatcher = Whitebox.getInternalState(traceIgnoreExtendService + , "traceIgnorePatternWatcher"); + traceIgnorePatternWatcher.notify(new AgentConfigChangeWatcher.ConfigChangeEvent( + "/eureka/apps/**", + AgentConfigChangeWatcher.EventType.MODIFY + )); + Assert.assertEquals("/eureka/apps/**", traceIgnorePatternWatcher.getTraceIgnorePathPatterns()); + Assert.assertEquals("agent.trace.ignore_path", traceIgnorePatternWatcher.getPropertyKey()); + } + + @Test + public void testConfigDeleteEvent() { + TraceIgnorePatternWatcher traceIgnorePatternWatcher = Whitebox.getInternalState(traceIgnoreExtendService + , "traceIgnorePatternWatcher"); + traceIgnorePatternWatcher.notify(new AgentConfigChangeWatcher.ConfigChangeEvent( + null, + AgentConfigChangeWatcher.EventType.DELETE + )); + Assert.assertEquals("agent.trace.ignore_path", traceIgnorePatternWatcher.getPropertyKey()); + } +}
