diff --git a/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsync.java b/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsync.java index 7b8a872..a09cce5 100644 --- a/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsync.java +++ b/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsync.java @@ -13,11 +13,15 @@ private SpanHttpAsync() { } public static CloseableHttpAsyncClient trace(CloseableHttpAsyncClient client) { + return SpanHttpAsync.trace(client, NoOpSpanModifier.INSTANCE); + } + + public static CloseableHttpAsyncClient trace(CloseableHttpAsyncClient client, SpanModifier spanModifier) { final HttpTaggerFactory taggerFactory = CombinedHttpTagger.factory(Arrays.asList( StandardHttpTagger.FACTORY, ContentHttpTagger.FACTORY )); - return new SpanHttpAsyncClient(new ContextAsyncClient(client, Context.DEFAULT), GlobalTracer.get(), ContextSpan.DEFAULT, taggerFactory); + return new SpanHttpAsyncClient(new ContextAsyncClient(client, Context.DEFAULT), GlobalTracer.get(), ContextSpan.DEFAULT, taggerFactory, spanModifier); } } diff --git a/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsyncClient.java b/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsyncClient.java index 03d2e0b..bbbdafd 100644 --- a/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsyncClient.java +++ b/httpasyncclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpAsyncClient.java @@ -20,12 +20,18 @@ public class SpanHttpAsyncClient extends CloseableHttpAsyncClient { private final Tracer tracer; private final ContextSpan contextSpan; private final HttpTaggerFactory taggerFactory; + private final SpanModifier spanModifier; public SpanHttpAsyncClient(final CloseableHttpAsyncClient client, final Tracer tracer, final ContextSpan contextSpan, final HttpTaggerFactory taggerFactory) { + this(client, tracer, contextSpan, taggerFactory, NoOpSpanModifier.INSTANCE); + } + + public SpanHttpAsyncClient(final CloseableHttpAsyncClient client, final Tracer tracer, final ContextSpan contextSpan, final HttpTaggerFactory taggerFactory, SpanModifier spanModifier) { this.client = client; this.tracer = tracer; this.contextSpan = contextSpan; this.taggerFactory = taggerFactory; + this.spanModifier = spanModifier; } public boolean isRunning() { @@ -49,6 +55,9 @@ public Future execute(HttpAsyncRequestProducer producer, HttpAsyncRespons .asChildOf(parentSpan) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT) .start(); + if (this.spanModifier != null) { + this.spanModifier.modify(span); + } HttpTagger tagger = taggerFactory.create(span, context); return contextSpan.set(span).supply(() -> client.execute( new SpanHttpAsyncRequestProducer(producer, tracer, span, tagger), diff --git a/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanExec.java b/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanExec.java index 50d36fb..eb739d5 100644 --- a/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanExec.java +++ b/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanExec.java @@ -21,12 +21,18 @@ public class SpanExec implements ClientExecChain { private final ContextSpan contextSpan; private final HttpTaggerFactory taggerFactory; private final Tracer tracer; + private final SpanModifier spanModifier; public SpanExec(ClientExecChain exec, Tracer tracer, ContextSpan contextSpan, HttpTaggerFactory taggerFactory) { + this(exec, tracer, contextSpan, taggerFactory, NoOpSpanModifier.INSTANCE); + } + + public SpanExec(ClientExecChain exec, Tracer tracer, ContextSpan contextSpan, HttpTaggerFactory taggerFactory, SpanModifier spanModifier) { this.exec = exec; this.contextSpan = contextSpan; this.taggerFactory = taggerFactory; this.tracer = tracer; + this.spanModifier = spanModifier; } public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException { @@ -39,6 +45,9 @@ public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT); Span span = spanBuilder.start(); + if (this.spanModifier != null) { + this.spanModifier.modify(span); + } this.tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new HttpRequestTextMap(request)); return contextSpan.set(span).supplyException2(() -> { HttpTagger tagger = taggerFactory.create(span, context); diff --git a/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttp.java b/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttp.java index 82db0bd..746fbf5 100644 --- a/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttp.java +++ b/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttp.java @@ -8,9 +8,14 @@ private SpanHttp() { } public static HttpClientBuilder trace() { + return SpanHttp.trace(NoOpSpanModifier.INSTANCE); + } + + public static HttpClientBuilder trace(SpanModifier spanModifier) { return new SpanHttpClientBuilder() .addTaggerFactory(StandardHttpTagger.FACTORY) - .addTaggerFactory(ContentHttpTagger.FACTORY); + .addTaggerFactory(ContentHttpTagger.FACTORY) + .setSpanModifier(spanModifier); } } diff --git a/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpClientBuilder.java b/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpClientBuilder.java index 06a9b88..b619f20 100644 --- a/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpClientBuilder.java +++ b/httpclient/src/main/java/io/opentracing/contrib/httpcomponents/SpanHttpClientBuilder.java @@ -13,6 +13,7 @@ public class SpanHttpClientBuilder extends HttpClientBuilder { private final List taggers; private Tracer tracer; private ContextSpan contextSpan; + private SpanModifier spanModifier = NoOpSpanModifier.INSTANCE; private String name; public SpanHttpClientBuilder() { @@ -30,6 +31,11 @@ public SpanHttpClientBuilder setSpanManager(ContextSpan contextSpan) { return this; } + public SpanHttpClientBuilder setSpanModifier(SpanModifier spanModifier) { + this.spanModifier = spanModifier; + return this; + } + public SpanHttpClientBuilder setTracer(Tracer tracer) { this.tracer = tracer; return this; @@ -38,7 +44,7 @@ public SpanHttpClientBuilder setTracer(Tracer tracer) { protected ClientExecChain decorateMainExec(ClientExecChain exec) { Tracer tracer = this.tracer != null ? this.tracer : GlobalTracer.get(); ContextSpan contextSpan = this.contextSpan != null ? this.contextSpan : ContextSpan.DEFAULT; - return new SpanExec(exec, tracer, contextSpan, CombinedHttpTagger.factory(taggers)); + return new SpanExec(exec, tracer, contextSpan, CombinedHttpTagger.factory(taggers), this.spanModifier); } } diff --git a/httpcore/src/main/java/io/opentracing/contrib/httpcomponents/SpanModifier.java b/httpcore/src/main/java/io/opentracing/contrib/httpcomponents/SpanModifier.java new file mode 100644 index 0000000..1412e0a --- /dev/null +++ b/httpcore/src/main/java/io/opentracing/contrib/httpcomponents/SpanModifier.java @@ -0,0 +1,13 @@ +package io.opentracing.contrib.httpcomponents; + +import io.opentracing.Span; + +@FunctionalInterface +interface SpanModifier { + public void modify(Span span); +} + +class NoOpSpanModifier implements SpanModifier { + public static SpanModifier INSTANCE = new NoOpSpanModifier(); + public void modify(Span span) {} +}