0

What is the best way to get a handle of an OpenTelemetry object in a Verticle and how to effectively use it? The official examples are unfortunately not very helpful. It is shown how to set it up but not how to get a handle of it for manual instrumentation.

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

  VertxOptions vertxOptions = new VertxOptions();
  vertxOptions.setTracingOptions(new OpenTelemetryOptions(openTelemetry));

  Vertx vertx = Vertx.vertx(vertxOptions);

I would like to do something like this:

 router.route(HttpMethod.GET, "/stats/system/time").handler(ctx -> {
     Tracer tracer = ...vertext.please.getTracer...get("Test");
     SpanBuilder spanBuilder = tracer.spanBuilder("testSpan").setAttribute("task", "json-generation");
     Span span = spanBuilder.startSpan();
     ctx.json(new JsonObject().put("systemTime", LocalDateTime.now()));
     span.end();
  });
alknows
  • 1,972
  • 3
  • 22
  • 26

1 Answers1

0

You can get a reference to the Vert.x tracer object like this:

VertxTracer<Span, Span> tracer = ((VertxInternal) vertx).tracer();

Then you can use the tracer receiveRequest() / sendResponse() methods at the beginning / end of the process.

tsegismont
  • 8,591
  • 1
  • 17
  • 27
  • Thanks for the answer. Is there now way to use the tracer the OpenTelemetry way? By building a span from the tracer instead of passing 7 arguments to sendRequest(...)? It look pretty low level implementation! – alknows Jan 05 '23 at 20:24
  • In the OpenTracing implementation of the Vert.x tracing SPI, we have a method which retrieves the current span, would that help if we added the same in OpenTelemetry? – tsegismont Jan 13 '23 at 15:24
  • That would be a great implementation. A way to get the span from an active context. Something like vertxContext.getSpan();. This works also by passing down span objects, but it looks ugly and screams of anti-pattern since every method which needs instrumentation needs access to opentelemetry and parent spans. Its one of those things that the vertx infrastructure could handle seamlessly. If you create a pull request i would be glad to give it a look. Thanks for getting back to me. – alknows Jan 14 '23 at 19:55
  • Actually, you can get the current with `io.opentelemetry.api.trace.Span#current` – tsegismont Mar 10 '23 at 17:54