How to configure opentelemetry using depedencies in spring boot microservices?

1k Views Asked by At

Add dependency of opentelemetry jar in all the docker files Enable promethues for all spring microservices Enable/disable opentelemetry

I wanted to implement opentelemetry-javaagent dependency in my spring boot cloud project in every microservices. here they have used jar files and show the metrics on SigNoz but I wanted to use the dependency for opentelemetry.

The problem is I am getting dependency but they are not working. No metrics are being projected in the SigNoz dashboard.

1

There are 1 best solutions below

0
On

If you don't want to use the agent, you may use the direct dependency in your application code.
https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-api.

<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-api</artifactId>
</dependency>

And configure the SDK -

Resource resource = Resource.getDefault()
  .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "logical-service-name")));

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
  .addSpanProcessor(BatchSpanProcessor.builder(OtlpGrpcSpanExporter.builder().build()).build())
  .setResource(resource)
  .build();

SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder()
  .registerMetricReader(PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build()).build())
  .setResource(resource)
  .build();

OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
  .setTracerProvider(sdkTracerProvider)
  .setMeterProvider(sdkMeterProvider)
  .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
  .buildAndRegisterGlobal();

Then acquire an instance of the tracer -

import io.opentelemetry.api;

//...

Tracer tracer =
    openTelemetry.getTracer("instrumentation-library-name", "1.0.0");

And create spans -

Span span = tracer.spanBuilder("my span").startSpan();

// Make the span the current span
try (Scope ss = span.makeCurrent()) {
  // In this scope, the span is the current/active span
} finally {
    span.end();
}

Taken from here -
https://opentelemetry.io/docs/instrumentation/java/manual/