I have an old application where I am trying to introduce opentrace. Instead of changing individual methods, I have added AOP/aspectJ to add span.But when i try to do
Span currentSpan = GlobalTracer.get().activeSpan()
in my original code, currentSpan is null.
I wonder Why ?
Here is my Code Aspect/AOP.
package com.visenti.LogAspectJ;
import io.jaegertracing.Configuration;
import io.jaegertracing.internal.JaegerTracer;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import org.aspectj.lang.Signature;
public aspect LogAspectJ {
pointcut methodExecution(): execution(* com.visenti.globaliams..*.*(..));
private Tracer tracer = initTracer("AspectJ");
public static JaegerTracer initTracer(String service) {
System.setProperty("JAEGER_AGENT_HOST", "*.*.*.*");
Configuration.SamplerConfiguration samplerConfig = Configuration.SamplerConfiguration.fromEnv().withType("const").withParam(1);
Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv().withLogSpans(true);
Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
JaegerTracer tracer = config.getTracer();
GlobalTracer.registerIfAbsent(tracer);
return tracer;
}
Object around(): methodExecution() {
Signature signature = thisJoinPoint.getSignature();
String operationName = signature.getName();
String declaringTypeName = signature.getDeclaringType().getSimpleName()+"::"+operationName;
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(declaringTypeName);
Span spanStart = spanBuilder.start();
Scope scope = tracer.activateSpan(spanStart);
Object response = proceed();
scope.close();
spanStart.finish();
return response;
}
}
This is my old code where I am trying to get parent span. This below method is covered by AOP, I can see in debugger.
What I am trying to understand is why it is null ? I expected the span to be propagated the method call chain.