|
| 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.observation.web.servlet; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.micrometer.common.KeyValues; |
| 22 | +import io.micrometer.core.instrument.Tag; |
| 23 | +import io.micrometer.observation.Observation; |
| 24 | + |
| 25 | +import org.springframework.boot.actuate.metrics.web.servlet.DefaultWebMvcTagsProvider; |
| 26 | +import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsContributor; |
| 27 | +import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider; |
| 28 | +import org.springframework.http.observation.ServerRequestObservationContext; |
| 29 | +import org.springframework.http.observation.ServerRequestObservationConvention; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.web.servlet.HandlerMapping; |
| 32 | + |
| 33 | +/** |
| 34 | + * Adapter class that applies {@link WebMvcTagsProvider} tags as a |
| 35 | + * {@link ServerRequestObservationConvention}. |
| 36 | + * |
| 37 | + * @author Brian Clozel |
| 38 | + */ |
| 39 | +@SuppressWarnings({ "deprecation", "removal" }) |
| 40 | +class ServerRequestObservationConventionAdapter implements ServerRequestObservationConvention { |
| 41 | + |
| 42 | + private final String observationName; |
| 43 | + |
| 44 | + private final WebMvcTagsProvider tagsProvider; |
| 45 | + |
| 46 | + ServerRequestObservationConventionAdapter(String observationName, WebMvcTagsProvider tagsProvider, |
| 47 | + List<WebMvcTagsContributor> contributors) { |
| 48 | + Assert.state((tagsProvider != null) || (contributors != null), |
| 49 | + "adapter should adapt to a WebMvcTagsProvider or a list of contributors"); |
| 50 | + this.observationName = observationName; |
| 51 | + this.tagsProvider = (tagsProvider != null) ? tagsProvider : new DefaultWebMvcTagsProvider(contributors); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getName() { |
| 56 | + return this.observationName; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean supportsContext(Observation.Context context) { |
| 61 | + return context instanceof ServerRequestObservationContext; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext context) { |
| 66 | + KeyValues keyValues = KeyValues.empty(); |
| 67 | + Iterable<Tag> tags = this.tagsProvider.getTags(context.getCarrier(), context.getResponse(), getHandler(context), |
| 68 | + context.getError()); |
| 69 | + for (Tag tag : tags) { |
| 70 | + keyValues = keyValues.and(tag.getKey(), tag.getValue()); |
| 71 | + } |
| 72 | + return keyValues; |
| 73 | + } |
| 74 | + |
| 75 | + private Object getHandler(ServerRequestObservationContext context) { |
| 76 | + return context.getCarrier().getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments