0

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.

enter image description here

What I am trying to understand is why it is null ? I expected the span to be propagated the method call chain.

1 Answers1

2

I've had a similar problem in a jax-rs application. What solved the issue for me was adding jaeger and opentracing-jaxrs2 dependencies.

    <dependency>
        <groupId>io.jaegertracing</groupId>
        <artifactId>jaeger-client</artifactId>
        <version>1.5.0</version>
    </dependency>

    <dependency>
        <groupId>io.opentracing.contrib</groupId>
        <artifactId>opentracing-jaxrs2</artifactId>
        <version>1.0.0</version>
    </dependency>

And the web.xml file I added filter and filter-mapping tags as follows:

<filter>
    <filter-name>TracingFilter</filter-name>
    <filter-class>io.opentracing.contrib.web.servlet.filter.TracingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>TracingFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

I think the problem was that java CDI was not injecting those filters.

Cheers!