opentelemetry-java-instrume.../instrumentation/java-http-client/library
Lauri Tulmin 08236a710f
Add library instrumentation for java http client (#8138)
Resolves
https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/8069
The javaagent instrumentation also supports propagating context into
[BodyHandler](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpResponse.BodyHandler.html)
implemented in
https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/java-http-client/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/BodyHandlerWrapper.java
I think the initial idea behind it was that this allowed propagating
context into callbacks. Because this didn't work for
`connectionErrorUnopenedPortWithCallback` test later we also added
wrapping completable future to take care of propagating context into
callbacks. Should I also implement context propagation for `BodyHandler`
in library instrumentation or should I just delete it? I guess it could
come handy if someone builds a custom `BodyHandler` and wants to emit
spans from there, though this doesn't feel too likely. I'd like deleting
it more.

---------

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
2023-04-03 13:08:29 -07:00
..
src Add library instrumentation for java http client (#8138) 2023-04-03 13:08:29 -07:00
README.md Add library instrumentation for java http client (#8138) 2023-04-03 13:08:29 -07:00
build.gradle.kts Add library instrumentation for java http client (#8138) 2023-04-03 13:08:29 -07:00

README.md

Library Instrumentation for Java HTTP Client

Provides OpenTelemetry instrumentation for Java HTTP Client.

Quickstart

Add these dependencies to your project

Replace OPENTELEMETRY_VERSION with the latest release.

For Maven, add to your pom.xml dependencies:

<dependencies>
  <dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-java-http-client</artifactId>
    <version>OPENTELEMETRY_VERSION</version>
  </dependency>
</dependencies>

For Gradle, add to your dependencies:

implementation("io.opentelemetry.instrumentation:opentelemetry-java-http-client:OPENTELEMETRY_VERSION")

Usage

The instrumentation library contains an HttpClient wrapper that provides OpenTelemetry-based spans and context propagation.

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.httpclient.JavaHttpClientTelemetry;
import java.net.http.HttpClient;

import java.util.concurrent.ExecutorService;

public class JavaHttpClientConfiguration {

  //Use this HttpClient implementation for making standard http client calls.
  public HttpClient createTracedClient(OpenTelemetry openTelemetry) {
    return JavaHttpClientTelemetry.builder(openTelemetry).build().newHttpClient(createClient());
  }

  //your configuration of the Java HTTP Client goes here:
  private HttpClient createClient() {
    return HttpClient.newBuilder().build();
  }
}