Skip to content

Commit

Permalink
Reinstate former HttpComponents instrumentation
Browse files Browse the repository at this point in the history
This commit reinstates the former HttpComponents instrumentation for 4.x
and 5.x, in a deprecated fashion for 5.x since it will be replaced by
the `ObservationExecChainHandler` in this dedicated issue.

See gh-3800
  • Loading branch information
bclozel committed Jun 26, 2023
1 parent cb86bc2 commit 196d07d
Show file tree
Hide file tree
Showing 7 changed files with 842 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.micrometer.core.instrument.binder.http;

import io.micrometer.common.lang.Nullable;
import io.micrometer.common.util.StringUtils;
import io.micrometer.core.annotation.Incubating;
import io.micrometer.core.instrument.Tag;
Expand Down Expand Up @@ -88,7 +87,7 @@ public static Tag status(jakarta.servlet.http.HttpServletResponse response) {
* @param exception the exception, may be {@code null}
* @return the exception tag derived from the exception
*/
public static Tag exception(@Nullable Throwable exception) {
public static Tag exception(Throwable exception) {
if (exception != null) {
String simpleName = exception.getClass().getSimpleName();
return Tag.of("exception",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.binder.http.Outcome;
import io.micrometer.core.instrument.binder.httpcomponents.hc5.ApacheHttpClientMetricsBinder;
import io.micrometer.core.instrument.binder.httpcomponents.hc5.ObservationExecChainHandler;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponseInterceptor;
Expand All @@ -46,7 +46,7 @@
* .build();
* }</pre>
* <p>
* See {@link ApacheHttpClientMetricsBinder} for Apache HTTP client 5 support.
* See {@link ObservationExecChainHandler} for Apache HTTP client 5 support.
*
* @author Jon Schneider
* @since 1.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.binder.http.Outcome;
import io.micrometer.core.instrument.binder.httpcomponents.hc5.ApacheHttpClientMetricsBinder;
import io.micrometer.core.instrument.binder.httpcomponents.hc5.ObservationExecChainHandler;
import io.micrometer.core.instrument.observation.ObservationOrTimerCompatibleInstrumentation;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
Expand Down Expand Up @@ -51,7 +51,7 @@
* .build();
* </pre>
* <p>
* See {@link ApacheHttpClientMetricsBinder} for Apache HTTP client 5 support.
* See {@link ObservationExecChainHandler} for Apache HTTP client 5 support.
*
* @author Benjamin Hubert (benjamin.hubert@willhaben.at)
* @author Tommy Ludwig
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.httpcomponents.hc5;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.binder.http.Outcome;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpRequestInterceptor;
import org.apache.hc.core5.http.HttpResponseInterceptor;
import org.apache.hc.core5.http.protocol.HttpContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
* Provides {@link HttpRequestInterceptor} and {@link HttpResponseInterceptor} for
* instrumenting async Apache HTTP Client 5. Configure the interceptors on an
* {@link org.apache.hc.client5.http.async.HttpAsyncClient}. Usage example: <pre>{@code
* MicrometerHttpClientInterceptor interceptor = new MicrometerHttpClientInterceptor(registry,
* HttpRequest::getRequestUri,
* Tags.empty(),
* true);
*
* CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom()
* .addRequestInterceptorFirst(interceptor.getRequestInterceptor())
* .addResponseInterceptorLast(interceptor.getResponseInterceptor())
* .build();
* }</pre>
*
* @author Jon Schneider
* @since 1.11.0
* @deprecated since 1.12.0 in favor of {@link ObservationExecChainHandler}.
*/
@Deprecated
public class MicrometerHttpClientInterceptor {

private static final String METER_NAME = "httpcomponents.httpclient.request";

private final Map<HttpContext, Timer.ResourceSample> timerByHttpContext = new ConcurrentHashMap<>();

private final HttpRequestInterceptor requestInterceptor;

private final HttpResponseInterceptor responseInterceptor;

/**
* Create a {@code MicrometerHttpClientInterceptor} instance.
* @param meterRegistry meter registry to bind
* @param uriMapper URI mapper to create {@code uri} tag
* @param extraTags extra tags
* @param exportTagsForRoute whether to export tags for route
*/
public MicrometerHttpClientInterceptor(MeterRegistry meterRegistry, Function<HttpRequest, String> uriMapper,
Iterable<Tag> extraTags, boolean exportTagsForRoute) {
this.requestInterceptor = (request, entityDetails, context) -> timerByHttpContext.put(context,
Timer.resource(meterRegistry, METER_NAME)
.tags("method", request.getMethod(), "uri", uriMapper.apply(request)));

this.responseInterceptor = (response, entityDetails, context) -> {
timerByHttpContext.remove(context)
.tag("status", Integer.toString(response.getCode()))
.tag("outcome", Outcome.forStatus(response.getCode()).name())
.tags(exportTagsForRoute ? HttpContextUtils.generateTagsForRoute(context) : Tags.empty())
.tags(extraTags)
.close();
};
}

/**
* Create a {@code MicrometerHttpClientInterceptor} instance with
* {@link DefaultUriMapper}.
* @param meterRegistry meter registry to bind
* @param extraTags extra tags
* @param exportTagsForRoute whether to export tags for route
*/
public MicrometerHttpClientInterceptor(MeterRegistry meterRegistry, Iterable<Tag> extraTags,
boolean exportTagsForRoute) {
this(meterRegistry, new DefaultUriMapper(), extraTags, exportTagsForRoute);
}

public HttpRequestInterceptor getRequestInterceptor() {
return requestInterceptor;
}

public HttpResponseInterceptor getResponseInterceptor() {
return responseInterceptor;
}

}

0 comments on commit 196d07d

Please sign in to comment.