Skip to content

Commit

Permalink
Polish "Apache HTTP client instrumentation with Observation"
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Sep 18, 2022
1 parent 05a1d93 commit 511ec47
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 VMware, Inc.
* Copyright 2022 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,11 +35,11 @@ public Class<? extends ObservationConvention<? extends Observation.Context>> get

@Override
public KeyName[] getLowCardinalityKeyNames() {
return ApacheHttpClientTags.values();
return ApacheHttpClientKeyNames.values();
}
};

enum ApacheHttpClientTags implements KeyName {
enum ApacheHttpClientKeyNames implements KeyName {

STATUS {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public String getContextualName(ApacheHttpClientContext context) {
@Override
public KeyValues getLowCardinalityKeyValues(ApacheHttpClientContext context) {
KeyValues keyValues = KeyValues.of(
ApacheHttpClientDocumentedObservation.ApacheHttpClientTags.METHOD
ApacheHttpClientDocumentedObservation.ApacheHttpClientKeyNames.METHOD
.withValue(getMethodString(context.getCarrier())),
ApacheHttpClientDocumentedObservation.ApacheHttpClientTags.URI
ApacheHttpClientDocumentedObservation.ApacheHttpClientKeyNames.URI
.withValue(context.getUriMapper().apply(context.getCarrier())),
ApacheHttpClientDocumentedObservation.ApacheHttpClientTags.STATUS
ApacheHttpClientDocumentedObservation.ApacheHttpClientKeyNames.STATUS
.withValue(getStatusValue(context.getResponse())));
if (context.shouldExportTagsForRoute()) {
keyValues = keyValues.and(HttpContextUtils.generateTagStringsForRoute(context.getApacheHttpContext()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ static String[] generateTagStringsForRoute(HttpContext context) {
targetHost = host.getHostName();
targetPort = String.valueOf(host.getPort());
}
return new String[] { ApacheHttpClientDocumentedObservation.ApacheHttpClientTags.TARGET_SCHEME.asString(),
targetScheme, ApacheHttpClientDocumentedObservation.ApacheHttpClientTags.TARGET_HOST.asString(),
targetHost, ApacheHttpClientDocumentedObservation.ApacheHttpClientTags.TARGET_PORT.asString(),
return new String[] { ApacheHttpClientDocumentedObservation.ApacheHttpClientKeyNames.TARGET_SCHEME.asString(),
targetScheme, ApacheHttpClientDocumentedObservation.ApacheHttpClientKeyNames.TARGET_HOST.asString(),
targetHost, ApacheHttpClientDocumentedObservation.ApacheHttpClientKeyNames.TARGET_PORT.asString(),
targetPort };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public Builder exportTagsForRoute(boolean exportTagsForRoute) {
* API instead of directly with a {@link Timer}.
* @param observationRegistry registry with which to instrument
* @return This builder instance.
* @since 1.10.0
*/
public Builder observationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
Expand All @@ -226,11 +227,12 @@ public Builder observationRegistry(ObservationRegistry observationRegistry) {

/**
* Provide a custom convention to override the default convention used when
* instrumenting with the {@link Observation} API. This only takes effect when a
* instrumenting with the {@link Observation} API. This only takes effect when an
* {@link #observationRegistry(ObservationRegistry)} is configured.
* @param convention semantic convention to use
* @return This builder instance.
* @see #observationRegistry(ObservationRegistry)
* @since 1.10.0
*/
public Builder observationConvention(ApacheHttpClientObservationConvention convention) {
this.observationConvention = convention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,18 @@ void contextualNameContainsRequestMethod(String method, @WiremockResolver.Wiremo
MicrometerHttpRequestExecutor micrometerHttpRequestExecutor = MicrometerHttpRequestExecutor.builder(registry)
.observationRegistry(observationRegistry).build();
HttpClient client = client(micrometerHttpRequestExecutor);
if ("get".contentEquals(method)) {
EntityUtils.consume(client.execute(new HttpGet(server.baseUrl())).getEntity());
}
else if ("post".contentEquals(method)) {
EntityUtils.consume(client.execute(new HttpPost(server.baseUrl())).getEntity());
}
else {
EntityUtils.consume(client.execute(new HttpCustomMethod(method, server.baseUrl())).getEntity());
switch (method) {
case "get":
EntityUtils.consume(client.execute(new HttpGet(server.baseUrl())).getEntity());
break;

case "post":
EntityUtils.consume(client.execute(new HttpPost(server.baseUrl())).getEntity());
break;

default:
EntityUtils.consume(client.execute(new HttpCustomMethod(method, server.baseUrl())).getEntity());
break;
}
TestObservationRegistryAssert.assertThat(observationRegistry).hasSingleObservationThat()
.hasContextualNameEqualToIgnoringCase("http " + method);
Expand All @@ -283,7 +287,6 @@ private static class HttpCustomMethod extends HttpEntityEnclosingRequestBase {
private final String method;

private HttpCustomMethod(String method, String uri) {
super();
setURI(URI.create(uri));
this.method = method;
}
Expand Down Expand Up @@ -311,10 +314,6 @@ private HttpClient client(HttpRequestExecutor executor) {
return HttpClientBuilder.create().setRequestExecutor(executor).build();
}

private HttpRequestExecutor executor(boolean exportRoutes) {
return executor(exportRoutes, false);
}

private HttpRequestExecutor executor(boolean exportRoutes, boolean configureObservationRegistry) {
MicrometerHttpRequestExecutor.Builder builder = MicrometerHttpRequestExecutor.builder(registry);
if (configureObservationRegistry) {
Expand Down

0 comments on commit 511ec47

Please sign in to comment.