Skip to content

Commit

Permalink
Merge pull request #11 from sriraamas/span-modifier-option
Browse files Browse the repository at this point in the history
add ability to modify span properties outside of library
  • Loading branch information
sriraamas authored Feb 3, 2022
2 parents 4f1a4d8 + de090fc commit e888a33
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -49,6 +55,9 @@ public <T> Future<T> 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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).<CloseableHttpResponse, HttpException, IOException>supplyException2(() -> {
HttpTagger tagger = taggerFactory.create(span, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SpanHttpClientBuilder extends HttpClientBuilder {
private final List<HttpTaggerFactory> taggers;
private Tracer tracer;
private ContextSpan contextSpan;
private SpanModifier spanModifier = NoOpSpanModifier.INSTANCE;
private String name;

public SpanHttpClientBuilder() {
Expand All @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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) {}
}

0 comments on commit e888a33

Please sign in to comment.