简体   繁体   中英

How to create OpenTelemetry Traces in a Vertx Verticle

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();
  });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM