Skip to content

Commit

Permalink
Add a forceIgnoring mechanism and apply it to the plugins (Spring Clo…
Browse files Browse the repository at this point in the history
…ud Gateway, HttpClient, JDK-ForkJoinPool, JDK-Threading, JDK-ThreadPool, Toolkit-Trace, Toolkit-WebFlux)
  • Loading branch information
gzlicanyi committed May 13, 2024
1 parent 0bf535e commit d751b53
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Release Notes.
* Use a daemon thread to flush logs.
* Fix typos in `URLParser`.
* Add support for `Derby`/`Sybase`/`SQLite`/`DB2`/`OceanBase` jdbc url format in `URLParser`.
* Add a forceIgnoring mechanism and apply it to the plugins (Spring Cloud Gateway, HttpClient, JDK-ForkJoinPool, JDK-Threading, JDK-ThreadPool, Toolkit-Trace, Toolkit-WebFlux).

All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/213?closed=1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.skywalking.apm.agent.core.context;

import java.util.Objects;

import org.apache.skywalking.apm.agent.core.boot.BootService;
import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
Expand All @@ -45,12 +46,12 @@ public class ContextManager implements BootService {
private static ThreadLocal<RuntimeContext> RUNTIME_CONTEXT = new ThreadLocal<RuntimeContext>();
private static ContextManagerExtendService EXTEND_SERVICE;

private static AbstractTracerContext getOrCreate(String operationName, boolean forceSampling) {
private static AbstractTracerContext getOrCreate(String operationName, boolean forceSampling, boolean forceIgnoring) {
AbstractTracerContext context = CONTEXT.get();
if (context == null) {
if (StringUtil.isEmpty(operationName)) {
if (forceIgnoring || StringUtil.isEmpty(operationName)) {
if (LOGGER.isDebugEnable()) {
LOGGER.debug("No operation name, ignore this trace.");
LOGGER.debug("No operation name or forceIgnoring, ignore this trace.");
}
context = new IgnoredTracerContext();
} else {
Expand Down Expand Up @@ -108,36 +109,52 @@ public static AbstractSpan createEntrySpan(String operationName, ContextCarrier
if (carrier != null && carrier.isValid()) {
SamplingService samplingService = ServiceManager.INSTANCE.findService(SamplingService.class);
samplingService.forceSampled();
context = getOrCreate(operationName, true);
context = getOrCreate(operationName, true, false);
span = context.createEntrySpan(operationName);
context.extract(carrier);
} else {
context = getOrCreate(operationName, false);
context = getOrCreate(operationName, false, false);
span = context.createEntrySpan(operationName);
}
return span;
}

public static AbstractSpan createLocalSpan(String operationName) {
operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD);
AbstractTracerContext context = getOrCreate(operationName, false);
AbstractTracerContext context = getOrCreate(operationName, false, false);
return context.createLocalSpan(operationName);
}

public static AbstractSpan createLocalSpan(String operationName, ContextSnapshot snapshot) {
operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD);

boolean forceIgnoring = false;
if (snapshot != null) {
forceIgnoring = !snapshot.isValid();
}
AbstractTracerContext context = getOrCreate(operationName, false, forceIgnoring);

AbstractSpan span = context.createLocalSpan(operationName);
if (snapshot != null) {
context.continued(snapshot);
}
return span;
}

public static AbstractSpan createExitSpan(String operationName, ContextCarrier carrier, String remotePeer) {
if (carrier == null) {
throw new IllegalArgumentException("ContextCarrier can't be null.");
}
operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD);
AbstractTracerContext context = getOrCreate(operationName, false);
AbstractTracerContext context = getOrCreate(operationName, false, false);
AbstractSpan span = context.createExitSpan(operationName, remotePeer);
context.inject(carrier);
return span;
}

public static AbstractSpan createExitSpan(String operationName, String remotePeer) {
operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD);
AbstractTracerContext context = getOrCreate(operationName, false);
AbstractTracerContext context = getOrCreate(operationName, false, false);
return context.createExitSpan(operationName, remotePeer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.agent.core.context;

import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
import org.apache.skywalking.apm.agent.core.conf.Config;
import org.apache.skywalking.apm.agent.core.context.ids.NewDistributedTraceId;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.LocalSpan;
import org.apache.skywalking.apm.agent.core.context.trace.NoopSpan;
import org.apache.skywalking.apm.agent.core.profile.ProfileStatusContext;
import org.apache.skywalking.apm.agent.core.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.core.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.core.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.core.test.tools.TracingSegmentRunner;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;

@RunWith(TracingSegmentRunner.class)
public class SnapshotCreateContextTest {

@SegmentStoragePoint
private SegmentStorage tracingData;

@Rule
public AgentServiceRule agentServiceRule = new AgentServiceRule();

@BeforeClass
public static void beforeClass() {
Config.Agent.KEEP_TRACING = true;
}

@AfterClass
public static void afterClass() {
Config.Agent.KEEP_TRACING = false;
ServiceManager.INSTANCE.shutdown();
}

@Test
public void testCreateLocalSpanWithNullSnapshot() {

AbstractSpan span = ContextManager.createLocalSpan("testLocalSpanWithNullSnapshot", null);
ContextManager.stopSpan();

assertTrue(span instanceof LocalSpan);
}

@Test
public void testCreateLocalSpanWithIgnoreSnapshot() {

ContextSnapshot ignoreContextSnapshot =
new ContextSnapshot(null, -1, null, null,
new CorrelationContext(), new ExtensionContext(), ProfileStatusContext.createWithNone());

AbstractSpan span = ContextManager.createLocalSpan("testLocalSpanWithIgnoreSnapshot", ignoreContextSnapshot);
ContextManager.stopSpan();

assertTrue(span instanceof NoopSpan);
}

@Test
public void testCreateLocalSpanWithSnapshot() {

NewDistributedTraceId distributedTraceId = new NewDistributedTraceId();
ContextSnapshot ignoreContextSnapshot =
new ContextSnapshot(
"1, 2, 3",
1,
distributedTraceId,
"/for-test-createLocalSpan",
new CorrelationContext(),
new ExtensionContext(),
ProfileStatusContext.createWithNone()
);

AbstractSpan span = ContextManager.createLocalSpan("testLocalSpanWithSnapshot", ignoreContextSnapshot);

assertTrue(span instanceof LocalSpan);
assertTrue(distributedTraceId.getId().equals(ContextManager.getGlobalTraceId()));

ContextManager.stopSpan();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ private HttpContext findHttpContext(Object[] allArguments) {
ContextSnapshot snapshot = (ContextSnapshot) contextInConn.getAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT);
conn.getContext().removeAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT);
if (snapshot != null) {
AbstractSpan localSpan = ContextManager.createLocalSpan("HttpAsyncClient/local");
AbstractSpan localSpan = ContextManager.createLocalSpan("HttpAsyncClient/local", snapshot);
localSpan.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
localSpan.setLayer(SpanLayer.HTTP);
ContextManager.continued(snapshot);
}
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
return;
}
ContextSnapshot snapshot = (ContextSnapshot) array[0];
AbstractSpan localSpan = ContextManager.createLocalSpan("httpasyncclient/local");
AbstractSpan localSpan = ContextManager.createLocalSpan("httpasyncclient/local", snapshot);
localSpan.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
localSpan.setLayer(SpanLayer.HTTP);
if (snapshot != null) {
ContextManager.continued(snapshot);
}
Constants.HTTP_CONTEXT_LOCAL.set((HttpContext) array[1]);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA
return ret;
}
httpContext.removeAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT);
AbstractSpan localSpan = ContextManager.createLocalSpan("httpasyncclient/local");
AbstractSpan localSpan = ContextManager.createLocalSpan("httpasyncclient/local", snapshot);
localSpan.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
localSpan.setLayer(SpanLayer.HTTP);
ContextManager.continued(snapshot);

final ContextCarrier contextCarrier = new ContextCarrier();
BasicHttpRequest request = (BasicHttpRequest) httpContext.getAttribute(HttpClientContext.HTTP_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ public class CallableOrRunnableInvokeInterceptor implements InstanceMethodsAroun
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
ContextManager.createLocalSpan("Thread/" + objInst.getClass().getName() + "/" + method.getName());
ContextSnapshot cachedObjects = (ContextSnapshot) objInst.getSkyWalkingDynamicField();
if (cachedObjects != null) {
ContextManager.continued(cachedObjects);
}

ContextManager.createLocalSpan("Thread/" + objInst.getClass().getName() + "/" + method.getName(), cachedObjects);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ public void beforeMethod(Class clazz, Method method, Object[] allArguments, Clas
if (parameterTypes[0] == Context.class) {
((Context) allArguments[0]).getOrEmpty("SKYWALKING_CONTEXT_SNAPSHOT")
.ifPresent(ctx -> {
ContextManager.createLocalSpan("WebFluxOperators/onNext").setComponent(ComponentsDefine.SPRING_WEBFLUX);
ContextManager.continued((ContextSnapshot) ctx);
ContextManager.createLocalSpan("WebFluxOperators/onNext", (ContextSnapshot) ctx).setComponent(ComponentsDefine.SPRING_WEBFLUX);
});
} else if (parameterTypes[0] == ServerWebExchange.class) {
EnhancedInstance instance = getInstance(allArguments[0]);
if (instance != null && instance.getSkyWalkingDynamicField() != null) {
ContextManager.createLocalSpan("WebFluxOperators/onNext").setComponent(ComponentsDefine.SPRING_WEBFLUX);
ContextManager.continued((ContextSnapshot) instance.getSkyWalkingDynamicField());
ContextManager.createLocalSpan("WebFluxOperators/onNext", (ContextSnapshot) instance.getSkyWalkingDynamicField()).setComponent(ComponentsDefine.SPRING_WEBFLUX);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ public class ForkJoinWorkerQueueMethodInterceptor implements InstanceMethodsArou
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInvocationContext context) throws Throwable {
AbstractSpan span = ContextManager.createLocalSpan(generateOperationName(objInst, method));
span.setComponent(ComponentsDefine.JDK_THREADING);
context.setContext(span);
EnhancedInstance forkJoinTask = (EnhancedInstance) allArguments[0];
ContextSnapshot contextSnapshot = null;
if (forkJoinTask != null) {
final Object storedField = forkJoinTask.getSkyWalkingDynamicField();
if (storedField != null) {
final ContextSnapshot contextSnapshot = (ContextSnapshot) storedField;
ContextManager.continued(contextSnapshot);
contextSnapshot = (ContextSnapshot) storedField;
}
}

AbstractSpan span = ContextManager.createLocalSpan(generateOperationName(objInst, method), contextSnapshot);
span.setComponent(ComponentsDefine.JDK_THREADING);
context.setContext(span);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ public class ThreadingMethodInterceptor implements InstanceMethodsAroundIntercep
@Override
public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
final Class<?>[] argumentsTypes, final MethodInterceptResult result) {

AbstractSpan span = ContextManager.createLocalSpan(generateOperationName(objInst, method));
span.setComponent(ComponentsDefine.JDK_THREADING);

final Object storedField = objInst.getSkyWalkingDynamicField();
ContextSnapshot contextSnapshot = null;
if (storedField != null) {
final ContextSnapshot contextSnapshot = (ContextSnapshot) storedField;
ContextManager.continued(contextSnapshot);
contextSnapshot = (ContextSnapshot) storedField;
}

AbstractSpan span = ContextManager.createLocalSpan(generateOperationName(objInst, method), contextSnapshot);
span.setComponent(ComponentsDefine.JDK_THREADING);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public SwCallableWrapper(Callable callable, ContextSnapshot contextSnapshot) {

@Override
public Object call() throws Exception {
AbstractSpan span = ContextManager.createLocalSpan(getOperationName());
AbstractSpan span = ContextManager.createLocalSpan(getOperationName(), contextSnapshot);
span.setComponent(ComponentsDefine.JDK_THREADING);
ContextManager.continued(contextSnapshot);
try {
return callable.call();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public SwRunnableWrapper(Runnable runnable, ContextSnapshot contextSnapshot) {

@Override
public void run() {
AbstractSpan span = ContextManager.createLocalSpan(getOperationName());
AbstractSpan span = ContextManager.createLocalSpan(getOperationName(), contextSnapshot);
span.setComponent(ComponentsDefine.JDK_THREADING);
ContextManager.continued(contextSnapshot);
try {
runnable.run();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x;

import java.lang.reflect.Method;

import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
Expand All @@ -37,10 +38,13 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
EnhancedInstance enhancedInstance = getInstance(exchange);

AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/RoutingFilter");
ContextSnapshot contextSnapshot = null;
if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) {
ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField());
contextSnapshot = (ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField();
}

AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/RoutingFilter", contextSnapshot);

span.setComponent(SPRING_CLOUD_GATEWAY);
span.prepareForAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr

EnhancedInstance enhancedInstance = getInstance(exchange);

AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/RoutingFilter");
ContextSnapshot contextSnapshot = null;
if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) {
ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField());
contextSnapshot = (ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField();
}

AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/RoutingFilter", contextSnapshot);

span.setComponent(SPRING_CLOUD_GATEWAY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr

EnhancedInstance enhancedInstance = getInstance(allArguments[0]);

AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/RoutingFilter");
ContextSnapshot contextSnapshot = null;
if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) {
ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField());
contextSnapshot = (ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField();
}

AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/RoutingFilter", contextSnapshot);

span.setComponent(SPRING_CLOUD_GATEWAY);
}

Expand Down
Loading

0 comments on commit d751b53

Please sign in to comment.