-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reinstate former HttpComponents instrumentation
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
Showing
7 changed files
with
842 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...micrometer/core/instrument/binder/httpcomponents/hc5/MicrometerHttpClientInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.