Java HttpClient instrumentation

Since Java 11, an HttpClient is provided as part of the JDK. See this introduction to it. Micrometer provides instrumentation of this via a micrometer-java11 module. This module requires Java 11 or later.

For Gradle, add the following implementation:

implementation 'io.micrometer:micrometer-java11'

For Maven, add the following dependency:

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-java11</artifactId>
</dependency>

Create an HttpClient as you normally would. For example:

HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(2)).build();

You can instrument this HttpClient as follows with an ObservationRegistry:

HttpClient observedClient = MicrometerHttpClient.instrumentationBuilder(httpClient, meterRegistry)
    .observationRegistry(observationRegistry)
    .build();

Alternatively, if you are not using an ObservationRegistry, you can instrument with only a MeterRegistry as follows:

HttpClient observedClient = MicrometerHttpClient.instrumentationBuilder(httpClient, meterRegistry).build();