Skip to content

Commit

Permalink
Actually finish() the span :/
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldraper committed Mar 10, 2017
1 parent c65a2e8 commit d8478c3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void failed(Exception ex) {
}

public void cancelled() {
context.run(() -> delegate.cancelled());
context.run(delegate::cancelled);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.opentracing.contrib.httpcomponents;

import io.opentracing.Span;
import io.opentracing.tag.Tags;
import org.apache.http.concurrent.FutureCallback;

public class SpanFutureCallback<T> implements FutureCallback<T> {

private final FutureCallback<T> delegate;
private final Span span;

public SpanFutureCallback(FutureCallback<T> delegate, Span span) {
this.delegate = delegate;
this.span = span;
}

public void completed(T result) {
span.finish();
delegate.completed(result);
}

public void failed(Exception ex) {
span.finish();
Tags.ERROR.set(span, true);
delegate.failed(ex);
}

public void cancelled() {
span.finish();
Tags.ERROR.set(span, true);
span.log("client cancel");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public <T> Future<T> execute(HttpAsyncRequestProducer producer, HttpAsyncRespons
Span span = tracer.buildSpan("HTTP").addReference(References.CHILD_OF, contextSpan.get().context())
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.start();
return contextSpan.set(span).supply(() -> client.execute(producer, responseConsumer, context, callback));
return contextSpan.set(span).supply(() -> client.execute(
new SpanHttpAsyncRequestProducer(producer, span),
new SpanHttpAsyncResponseConsumer<>(responseConsumer, span),
context,
new SpanFutureCallback<>(callback, span)
));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOEx
}

public void requestCompleted(HttpContext context) {
span.log("requestCompleted");
span.log("request complete");
delegate.requestCompleted(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public SpanHttpAsyncResponseConsumer(HttpAsyncResponseConsumer<T> delegate, Span

public void responseReceived(HttpResponse response) throws IOException, HttpException {
StandardHttpTagger.tagResponse(span, response);
delegate.responseReceived(response);
}

public void consumeContent(ContentDecoder decoder, IOControl ioctrl) throws IOException {
Expand Down Expand Up @@ -55,7 +56,7 @@ public void close() throws IOException {
}

public boolean cancel() {
span.log("cancel");
span.log("response cancel");
return delegate.cancel();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,28 @@ public SpanExec(ClientExecChain exec, Tracer tracer, ContextSpan contextSpan, Ht
}

public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException {
Span span = tracer.buildSpan("HTTP " + request.getRequestLine().getMethod())
.asChildOf(contextSpan.get())
.start();
this.tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new HttpRequestTextMap(request));

return contextSpan.set(span).<CloseableHttpResponse, HttpException, IOException>supplyException2(() -> {
for (HttpTagger tagger : taggers) {
tagger.tag(span, route, request, context);
}
CloseableHttpResponse response;
try {
response = exec.execute(route, request, context, execAware);
} catch (HttpException | IOException e) {
Tags.ERROR.set(span, true);
throw e;
}
for (HttpTagger tagger : taggers) {
tagger.tag(span, route, response, context);
}
return response;
});
Tracer.SpanBuilder spanBuilder = tracer.buildSpan("HTTP " + request.getRequestLine().getMethod())
.asChildOf(contextSpan.get());
try(Span span = spanBuilder.start()) {
this.tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new HttpRequestTextMap(request));
return contextSpan.set(span).<CloseableHttpResponse, HttpException, IOException>supplyException2(() -> {
for (HttpTagger tagger : taggers) {
tagger.tag(span, route, request, context);
}
CloseableHttpResponse response;
try {
response = exec.execute(route, request, context, execAware);
} catch (HttpException | IOException e) {
Tags.ERROR.set(span, true);
throw e;
}
for (HttpTagger tagger : taggers) {
tagger.tag(span, route, response, context);
}
span.finish();
return response;
});
}
}

}

0 comments on commit d8478c3

Please sign in to comment.