|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.metrics.web.client; |
| 18 | + |
| 19 | +import io.micrometer.common.KeyValues; |
| 20 | +import io.micrometer.core.instrument.Tag; |
| 21 | +import io.micrometer.observation.Observation; |
| 22 | + |
| 23 | +import org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExchangeTagsProvider; |
| 24 | +import org.springframework.web.reactive.function.client.ClientObservationContext; |
| 25 | +import org.springframework.web.reactive.function.client.ClientObservationConvention; |
| 26 | +import org.springframework.web.reactive.function.client.ClientRequest; |
| 27 | +import org.springframework.web.reactive.function.client.WebClient; |
| 28 | + |
| 29 | +/** |
| 30 | + * Adapter class that applies {@link WebClientExchangeTagsProvider} tags as a |
| 31 | + * {@link ClientObservationConvention}. |
| 32 | + * |
| 33 | + * @author Brian Clozel |
| 34 | + */ |
| 35 | +@SuppressWarnings("deprecation") |
| 36 | +class ClientObservationConventionAdapter implements ClientObservationConvention { |
| 37 | + |
| 38 | + private static final String URI_TEMPLATE_ATTRIBUTE = WebClient.class.getName() + ".uriTemplate"; |
| 39 | + |
| 40 | + private final String metricName; |
| 41 | + |
| 42 | + private final WebClientExchangeTagsProvider tagsProvider; |
| 43 | + |
| 44 | + ClientObservationConventionAdapter(String metricName, WebClientExchangeTagsProvider tagsProvider) { |
| 45 | + this.metricName = metricName; |
| 46 | + this.tagsProvider = tagsProvider; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean supportsContext(Observation.Context context) { |
| 51 | + return context instanceof ClientObservationContext; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public KeyValues getLowCardinalityKeyValues(ClientObservationContext context) { |
| 56 | + KeyValues keyValues = KeyValues.empty(); |
| 57 | + mutateClientRequest(context); |
| 58 | + Iterable<Tag> tags = this.tagsProvider.tags(context.getCarrier(), context.getResponse(), |
| 59 | + context.getError().orElse(null)); |
| 60 | + for (Tag tag : tags) { |
| 61 | + keyValues = keyValues.and(tag.getKey(), tag.getValue()); |
| 62 | + } |
| 63 | + return keyValues; |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * {@link WebClientExchangeTagsProvider} relies on a request attribute to get the URI |
| 68 | + * template, we need to adapt to that. |
| 69 | + */ |
| 70 | + private static void mutateClientRequest(ClientObservationContext context) { |
| 71 | + ClientRequest clientRequest = ClientRequest.from(context.getCarrier()) |
| 72 | + .attribute(URI_TEMPLATE_ATTRIBUTE, context.getUriTemplate()).build(); |
| 73 | + context.setCarrier(clientRequest); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public KeyValues getHighCardinalityKeyValues(ClientObservationContext context) { |
| 78 | + return KeyValues.empty(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getName() { |
| 83 | + return this.metricName; |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments