Spring - micrometer + opentelemetry-exporter-otlp
Asked Answered
S

2

5

Is it possible to configure micrometer to send traces to otel container in spring?

I easily configured sending spans to Zipkin and Wavefront: https://docs.spring.io/spring-boot/docs/3.1.0-SNAPSHOT/reference/htmlsingle/#actuator.micrometer-tracing.tracers but there is nothing about exporting to Otel.

Micrometer documentation also does not mention about exporting spans to otel container https://micrometer.io/docs/tracing#_using_micrometer_tracing_directly

What dependencies need to be included in pom.xml for spring boot 3 to run this sucessfully

Strengthen answered 31/1, 2023 at 6:13 Comment(0)
L
11
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.trace.export.SpanExporter;

/**
 * As of SpringBoot 3.0.2 the inclusion of io.micrometer:micrometer-tracing-bridge-otel and
 * io.opentelemetry:opentelemetry-exporter-otlp is not sufficient to bootstrap the SpanExporter. Adding
 * io.opentelemetry:opentelemetry-sdk-extension-autoconfigure also does not help. Hence this solution which will
 * probably be redundant one day.
 */
@Configuration
public class OpenTelemetryConfig {

    @Value("${otel.exporter.otlp.traces.endpoint:http://localhost:4317}")
    private String tracesEndpoint;

    @Bean
    public SpanExporter spanExporter() {
        return OtlpGrpcSpanExporter.builder().setEndpoint(tracesEndpoint).build();
    }

}

I also found the following necessary if you want to use io.opentelemetry.instrumentation:opentelemetry-jdbc (and probably others) as it relies on GlobalOpenTelemetry.get(). This is forcing it to be the instance produced by the micrometer-tracing-bridge-otel.

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;

@Configuration
public class GlobalOpenTelemetrySetter implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if (bean instanceof OpenTelemetry openTelemetry) {
            GlobalOpenTelemetry.set(openTelemetry);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

}

I worry that this could have startup race conditions but is working for me at the moment. I hope the Spring team can provide proper clarification at some point.

Leontina answered 31/1, 2023 at 23:20 Comment(2)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Dietsche
I am facing the same problem. Tried the solution from @Leontina but get the following error. java.lang.NoSuchMethodError: 'io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder io.opentelemetry.exporter.internal.grpc.GrpcExporter.builder(java.lang.String, long, java.net.URI, java.util.function.Supplier, java.lang.String, java.lang.String)' at io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder.<init> OtlpGrpcSpanExporterBuilder.java:37) at io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter.builder(OtlpGrpcSpanExporter.java:39). Any help for exporting the mMeter-OLTP?Latt
S
1

Micrometer Tracing's documentation shows how to replace the exporter: https://micrometer.io/docs/tracing#_micrometer_tracing_opentelemetry_setup Boot auto-configures this so you only need a SpanExporter bean:

@Bean SpanExporter spanExporter() {
    return <the OTel exporter of your choice>;
}
Subsidy answered 21/2, 2023 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.