Skip to content

lucidsoftware/opentracing-httpcomponents

Repository files navigation

OpenTracing for HttpComponents

Build Status Maven Version

This library works with GlobalTracer and DefaultSpanManager.

HttpClient

Install com.lucidchart:opentracing-httpclient:<version>

import org.apache.http.impl.client.CloseableHttpClient;
import io.opentracing.contrib.httpcomponents.SpanHttp;
import io.opentracing.threadcontext.ContextSpan;
import io.opentracing.Span;

Span span = ...
CloseableHttpClient client = SpanHttp.trace().build();
ContextSpan.set(span).supplyException2(() -> {
  try {
    client.execute(new HttpGet("http://example.org/"))
  } catch (IOException | ClientProtocolException e) {
  }
})

If you are already using a custom subclass of HttpClientBuilder, override the decorateMainExec of the class.

import org.apache.http.impl.execchain.*;
import io.opentracing.contrib.httpcomponents.*;

new MyHttpClientBuilder {
    protected ClientExecChain decorateMainExec(ClientExecChain exec) {
        new DefaultSpanManagerExec(super.decorateMainExec(exec), new HttpTagger[] { new StandardHttpTagger(); }
    }
}

HttpAsyncClient

Install com.lucidchart:opentracing-httpasyncclient:<version>.

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import io.opentracing.contrib.httpcomponents.SpanHttpAsync;
import io.opentracing.threadcontext.ContentSpan;
import io.opentracing.Span;

Span span = ...
CloseableHttpAyncClient client = SpanHttpAsync.trace(HttpAsyncClients.createDefault());
ContextSpan.set(span).supplyException2(() -> {
  try {
    client.execute(new HttpGet("http://example.org/"), null)
  } catch (IOException | ClientProtocolException e) {
  }
})

Taggers

Tags for client Spans come from io.opentracing.contrib.httpcomponents.HttpTagger instances.

Built-in taggers:

  • StandardHttpTagger
  • ContentHttpTagger