|
| 1 | +/* |
| 2 | + * Copyright 2002-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.web.observation; |
| 18 | + |
| 19 | +import io.micrometer.common.KeyValue; |
| 20 | +import io.micrometer.common.KeyValues; |
| 21 | + |
| 22 | +import org.springframework.http.HttpStatus; |
| 23 | + |
| 24 | +/** |
| 25 | + * Default {@link HttpRequestsObservationConvention}. |
| 26 | + * @author Brian Clozel |
| 27 | + * @since 6.0 |
| 28 | + */ |
| 29 | +public class DefaultHttpRequestsObservationConvention implements HttpRequestsObservationConvention { |
| 30 | + |
| 31 | + private static final String DEFAULT_NAME = "http.server.requests"; |
| 32 | + |
| 33 | + private static final KeyValue METHOD_UNKNOWN = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.METHOD, "UNKNOWN"); |
| 34 | + |
| 35 | + private static final KeyValue STATUS_UNKNOWN = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.STATUS, "UNKNOWN"); |
| 36 | + |
| 37 | + private static final KeyValue URI_UNKNOWN = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.URI, "UNKNOWN"); |
| 38 | + |
| 39 | + private static final KeyValue URI_ROOT = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.URI, "root"); |
| 40 | + |
| 41 | + private static final KeyValue URI_NOT_FOUND = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.URI, "NOT_FOUND"); |
| 42 | + |
| 43 | + private static final KeyValue URI_REDIRECTION = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.URI, "REDIRECTION"); |
| 44 | + |
| 45 | + private static final KeyValue EXCEPTION_NONE = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.EXCEPTION, "none"); |
| 46 | + |
| 47 | + private static final KeyValue OUTCOME_UNKNOWN = KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.OUTCOME, "UNKNOWN"); |
| 48 | + |
| 49 | + private static final KeyValue URI_EXPANDED_UNKNOWN = KeyValue.of(HttpRequestsObservation.HighCardinalityKeyNames.URI_EXPANDED, "UNKNOWN"); |
| 50 | + |
| 51 | + private final String name; |
| 52 | + |
| 53 | + /** |
| 54 | + * Create a convention with the default name {@code "http.server.requests"}. |
| 55 | + */ |
| 56 | + public DefaultHttpRequestsObservationConvention() { |
| 57 | + this(DEFAULT_NAME); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a convention with a custom name. |
| 62 | + * @param name the observation name |
| 63 | + */ |
| 64 | + public DefaultHttpRequestsObservationConvention(String name) { |
| 65 | + this.name = name; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public String getName() { |
| 70 | + return this.name; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public KeyValues getLowCardinalityKeyValues(HttpRequestsObservationContext context) { |
| 75 | + return KeyValues.of(method(context), uri(context), status(context), exception(context), outcome(context)); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public KeyValues getHighCardinalityKeyValues(HttpRequestsObservationContext context) { |
| 80 | + return KeyValues.of(uriExpanded(context)); |
| 81 | + } |
| 82 | + |
| 83 | + protected KeyValue method(HttpRequestsObservationContext context) { |
| 84 | + return (context.getCarrier() != null) ? KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.METHOD, context.getCarrier().getMethod()) : METHOD_UNKNOWN; |
| 85 | + } |
| 86 | + |
| 87 | + protected KeyValue status(HttpRequestsObservationContext context) { |
| 88 | + return (context.getResponse() != null) ? KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.STATUS, Integer.toString(context.getResponse().getStatus())) : STATUS_UNKNOWN; |
| 89 | + } |
| 90 | + |
| 91 | + protected KeyValue uri(HttpRequestsObservationContext context) { |
| 92 | + if (context.getCarrier() != null) { |
| 93 | + String pattern = context.getPathPattern(); |
| 94 | + if (pattern != null) { |
| 95 | + if (pattern.isEmpty()) { |
| 96 | + return URI_ROOT; |
| 97 | + } |
| 98 | + return KeyValue.of("uri", pattern); |
| 99 | + } |
| 100 | + if (context.getResponse() != null) { |
| 101 | + HttpStatus status = HttpStatus.resolve(context.getResponse().getStatus()); |
| 102 | + if (status != null) { |
| 103 | + if (status.is3xxRedirection()) { |
| 104 | + return URI_REDIRECTION; |
| 105 | + } |
| 106 | + if (status == HttpStatus.NOT_FOUND) { |
| 107 | + return URI_NOT_FOUND; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return URI_UNKNOWN; |
| 113 | + } |
| 114 | + |
| 115 | + protected KeyValue exception(HttpRequestsObservationContext context) { |
| 116 | + return context.getError().map(throwable -> |
| 117 | + KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.EXCEPTION, throwable.getClass().getSimpleName())) |
| 118 | + .orElse(EXCEPTION_NONE); |
| 119 | + } |
| 120 | + |
| 121 | + protected KeyValue outcome(HttpRequestsObservationContext context) { |
| 122 | + if (context.getResponse() != null) { |
| 123 | + HttpStatus status = HttpStatus.resolve(context.getResponse().getStatus()); |
| 124 | + if (status != null) { |
| 125 | + return KeyValue.of(HttpRequestsObservation.LowCardinalityKeyNames.OUTCOME, status.series().name()); |
| 126 | + } |
| 127 | + } |
| 128 | + return OUTCOME_UNKNOWN; |
| 129 | + } |
| 130 | + |
| 131 | + protected KeyValue uriExpanded(HttpRequestsObservationContext context) { |
| 132 | + if (context.getCarrier() != null) { |
| 133 | + String uriExpanded = (context.getCarrier().getPathInfo() != null) ? context.getCarrier().getPathInfo() : "/"; |
| 134 | + return KeyValue.of(HttpRequestsObservation.HighCardinalityKeyNames.URI_EXPANDED, uriExpanded); |
| 135 | + } |
| 136 | + return URI_EXPANDED_UNKNOWN; |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments