From cd49f41791d8c942a53bbe74109be121cde8cc80 Mon Sep 17 00:00:00 2001 From: Joel Takvorian Date: Wed, 23 Sep 2020 15:40:45 +0200 Subject: [PATCH] Add opentracing example to set span Signed-off-by: Joel Takvorian --- vertx-opentracing/src/main/asciidoc/index.adoc | 15 +++++++++++++++ .../main/java/examples/OpenTracingExamples.java | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/vertx-opentracing/src/main/asciidoc/index.adoc b/vertx-opentracing/src/main/asciidoc/index.adoc index 7401866..f00b5c8 100644 --- a/vertx-opentracing/src/main/asciidoc/index.adoc +++ b/vertx-opentracing/src/main/asciidoc/index.adoc @@ -17,3 +17,18 @@ over the configuration. ---- {@link examples.OpenTracingExamples#ex2} ---- + +After the Tracer has been configured, Vert.x may inject _spans_ into existing traces for +some API calls, such as HTTP client or server requests. A trace that starts from a server +call will be automatically created by Vert.x. + +However, to initiate a trace for a client call, you need to create it first and make Vert.x +aware of it by using `OpenTracingUtil.setSpan`: + +[source,$lang] +---- +{@link examples.OpenTracingExamples#ex3} +---- + +In an HTTP scenario between two Vert.x services, a span will be created client-side, then +the trace context will be propagated server-side and another span will be added to the trace. diff --git a/vertx-opentracing/src/main/java/examples/OpenTracingExamples.java b/vertx-opentracing/src/main/java/examples/OpenTracingExamples.java index d5d067f..f12510b 100644 --- a/vertx-opentracing/src/main/java/examples/OpenTracingExamples.java +++ b/vertx-opentracing/src/main/java/examples/OpenTracingExamples.java @@ -1,10 +1,12 @@ package examples; +import io.opentracing.Span; import io.opentracing.Tracer; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.docgen.Source; import io.vertx.tracing.opentracing.OpenTracingOptions; +import io.vertx.tracing.opentracing.OpenTracingUtil; @Source public class OpenTracingExamples { @@ -24,4 +26,13 @@ public void ex2(Tracer tracer) { ) ); } + + public void ex3(Tracer tracer) { + Span span = tracer.buildSpan("my-operation") + .withTag("some-key", "some-value") + .start(); + OpenTracingUtil.setSpan(span); + // Do something, e.g. client request + span.finish(); + } }