Andreas Joensen
Andreas Joensen

Reputation: 41

Tracer.currentSpan()" is null for ErrorPages for Spring Boot 3

After migrating to Spring Boot 3 the micrometer traceId is null when redirecting to a org.springframework.boot.web.server.ErrorPage.

I get this error:

Cannot invoke "io.micrometer.tracing.Span.context()" because the return value of "io.micrometer.tracing.Tracer.currentSpan()" is null

If I use a @ExceptionHandler, then I am able to get the traceId. But would rather not do it like this, as I am migrating JavaEE applications with error pages defined in a web.xml.

When using Spring Boot 2.7 together with Spring Cloud Sleuth I am able to get the traceId after a redirect using ErrorPages.

Am I missing some configuration to let the micrometer Trace context be propagated to an ErrorPage?

Here is some code I made that reproduces the issue. The endpoint http://localhost:8080/test1 fails with the NPE:

package eu.joensen.error;

import io.micrometer.tracing.Tracer;
//import brave.Tracer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequiredArgsConstructor
@RestController
@SpringBootApplication
public class ErrorTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ErrorTestApplication.class, args);
    }

    private final Tracer tracer;

    @Bean
    ErrorPageRegistrar errorPageRegistrar() {
        ErrorPage errorPage = new ErrorPage(IllegalArgumentException.class, "/errorpage");
        return registry -> registry.addErrorPages(errorPage);
    }

    @GetMapping("test1")
    void triggerErrorA() {
        log.info("Before error page: {}", getTraceId());
        throw new IllegalArgumentException();
    }

    // ErrorPage endpoint defined for IllegalArgumentException in ErrorPageRegistrar
    // Results in a NPE:
    // Cannot invoke "io.micrometer.tracing.Span.context()" because the return value of "io.micrometer.tracing.Tracer.currentSpan()" is null
    @GetMapping("errorpage")
    String errorPage() {
        try {
            String traceId = getTraceId();
            log.info("After error page: {}", traceId);
            return traceId;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("test2")
    void triggerErrorB() {
        log.info("Before exception handler: {}", getTraceId());
        throw new IllegalStateException();
    }

    @ExceptionHandler(IllegalStateException.class)
    String handleError(IllegalStateException e) {
        String traceId = getTraceId();
        log.info("After exception handler: {}", traceId);
        return traceId;
    }

    private String getTraceId() {
//        return tracer.currentSpan().context().traceIdString();
        return tracer.currentSpan().context().traceId();
    }

}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <!--<version>2.7.18</version>-->
        <relativePath/>
    </parent>
    <groupId>eu.joensen.error</groupId>
    <artifactId>test</artifactId>
    <version>1.0.0</version>
    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing-bridge-brave</artifactId>
        </dependency>

<!--        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
            <version>3.1.11</version>
        </dependency>-->

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Upvotes: 1

Views: 1297

Answers (1)

abshar
abshar

Reputation: 475

Add config class :

import brave.Tracing;
import brave.propagation.CurrentTraceContext;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.brave.bridge.BraveCurrentTraceContext;
import io.micrometer.tracing.brave.bridge.BraveTracer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TracingConfiguration {


    @Bean
    public CurrentTraceContext braveCurrentTraceContext() {
        return CurrentTraceContext.Default.create();
    }

    @Bean
    public Tracing braveTracing(CurrentTraceContext currentTraceContext) {
        return Tracing.newBuilder()
                .localServiceName("Your-Application-Name")
                .currentTraceContext(currentTraceContext)
                .build();
    }

    @Bean
    public BraveTracer braveTracer(Tracing tracing) {
        return new BraveTracer(tracing.tracer(), new BraveCurrentTraceContext(tracing.currentTraceContext()));
    }

    @Bean
    public Tracer tracer(BraveTracer braveTracer) {
        return braveTracer;
    }

    @Bean
    public TraceIdProvider traceIdProvider(Tracer tracer) {
        return new TraceIdProvider(tracer);
    }
}

class TraceIdProvider {

    private final Tracer tracer;

    public TraceIdProvider(Tracer tracer) {
        this.tracer = tracer;
    }

    public String getTraceId() {
        Span currentSpan = this.tracer.currentSpan();
        if (currentSpan != null) {
            return currentSpan.context().traceId();
        }
        Span newSpan = this.tracer.nextSpan().start();
        try {
            return newSpan.context().traceId();
        } finally {
            newSpan.end();
        }
    }
}

And reference it like this :

@RequiredArgsConstructor
@Slf4j
public class YourExceptionController {
   private final TraceIdProvider traceIdProvider;
   ...
   private String getTraceId() {
        return traceIdProvider.getTraceId();
   }
   ...
}

Upvotes: 0

Related Questions