Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference doc update for OkHttp #4586

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/modules/ROOT/pages/reference/okhttpclient.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
[[overview]]
= OkHttpClient Instrumentation

Micrometer supports https://square.github.io/okhttp/[OkHttp Client] instrumentation through Observation and metrics.

[[okhttpclient-observation]]
== OkHttpClient Observation

Below you can find an example of how to instrument OkHttp Client with https://docs.micrometer.io/micrometer/reference/observation.html[Micrometer Observation]. That means that depending on your Observation Handler configuration you instrument once, and can have multiple benefits out of it (e.g. metrics, distributed tracing).

[source,java,subs=+attributes]
-----
// Setting up instrumentation
include::{include-core-test-java}/io/micrometer/core/instrument/binder/okhttp3/OkHttpObservationInterceptorTest.java[tags=setup, indent=0]

// Usage example
include::{include-core-test-java}/io/micrometer/core/instrument/binder/okhttp3/OkHttpObservationInterceptorTest.java[tags=example, indent=0]
-----

The builder allows to change the default `ObservationConvention` as follows.

[source,java,subs=+attributes]
-----
// Setting up instrumentation with custom convention
include::{include-core-test-java}/io/micrometer/core/instrument/binder/okhttp3/OkHttpObservationInterceptorTest.java[tags=custom_convention, indent=0]
-----

[[okhttpclient-metrics]]
== OkHttpClient Metrics

Micrometer supports binding metrics to `OkHttpClient` through `EventListener`.

You can collect metrics from `OkHttpClient` by adding `OkHttpMetricsEventListener` as follows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ class OkHttpObservationInterceptorTest {

private TestObservationRegistry observationRegistry = TestObservationRegistry.create();

private TestHandler testHandler = new TestHandler();

// tag::setup[]
private OkHttpClient client = new OkHttpClient.Builder().addInterceptor(defaultInterceptorBuilder().build())
.build();

private TestHandler testHandler = new TestHandler();

private OkHttpObservationInterceptor.Builder defaultInterceptorBuilder() {
return OkHttpObservationInterceptor.builder(observationRegistry, "okhttp.requests")
.tags(KeyValues.of("foo", "bar"))
.uriMapper(URI_MAPPER);
}
// end::setup[]

@BeforeEach
void setup() {
Expand All @@ -85,6 +87,7 @@ void setup() {
void timeSuccessfulWithDefaultObservation(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
client = new OkHttpClient.Builder().addInterceptor(defaultInterceptorBuilder().build()).build();
server.stubFor(any(anyUrl()));
// tag::example[]
Request request = new Request.Builder().url(server.baseUrl()).build();

makeACall(client, request);
Expand All @@ -97,17 +100,20 @@ void timeSuccessfulWithDefaultObservation(@WiremockResolver.Wiremock WireMockSer
assertThat(testHandler.context).isNotNull();
assertThat(testHandler.context.getAllKeyValues()).contains(KeyValue.of("foo", "bar"),
KeyValue.of("status", "200"));
// end::example[]
server.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/")).withHeader("foo", WireMock.equalTo("bar")));
}

@Test
void timeSuccessfulWithObservationConvention(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
// tag::custom_convention[]
MyConvention myConvention = new MyConvention();
client = new OkHttpClient.Builder()
.addInterceptor(defaultInterceptorBuilder()
.observationConvention(new StandardizedOkHttpObservationConvention(myConvention))
.build())
.build();
// end::custom_convention[]
server.stubFor(any(anyUrl()));
Request request = new Request.Builder().url(server.baseUrl()).build();

Expand Down