|
| 1 | +/* |
| 2 | + * Copyright 2017 VMware, Inc. |
| 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 | +package io.micrometer.core.instrument.binder.okhttp3; |
| 17 | + |
| 18 | +import io.micrometer.common.KeyValue; |
| 19 | +import io.micrometer.common.KeyValues; |
| 20 | +import io.micrometer.common.lang.NonNullApi; |
| 21 | +import io.micrometer.common.lang.NonNullFields; |
| 22 | +import io.micrometer.common.lang.Nullable; |
| 23 | +import io.micrometer.core.instrument.Tag; |
| 24 | +import io.micrometer.core.instrument.Tags; |
| 25 | +import okhttp3.Request; |
| 26 | +import okhttp3.Response; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.lang.reflect.Method; |
| 30 | +import java.util.List; |
| 31 | +import java.util.function.BiFunction; |
| 32 | +import java.util.function.Function; |
| 33 | +import java.util.stream.Collectors; |
| 34 | +import java.util.stream.Stream; |
| 35 | + |
| 36 | +import static java.util.stream.Collectors.toList; |
| 37 | +import static java.util.stream.StreamSupport.stream; |
| 38 | + |
| 39 | +@NonNullApi |
| 40 | +@NonNullFields |
| 41 | +public class DefaultOkHttpKeyValuesProvider implements OkHttpKeyValuesProvider { |
| 42 | + |
| 43 | + static final boolean REQUEST_TAG_CLASS_EXISTS; |
| 44 | + |
| 45 | + static { |
| 46 | + REQUEST_TAG_CLASS_EXISTS = getMethod(Class.class) != null; |
| 47 | + } |
| 48 | + |
| 49 | + @Nullable |
| 50 | + private static Method getMethod(Class<?>... parameterTypes) { |
| 51 | + try { |
| 52 | + return Request.class.getMethod("tag", parameterTypes); |
| 53 | + } |
| 54 | + catch (NoSuchMethodException e) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static final String TAG_TARGET_SCHEME = "target.scheme"; |
| 60 | + |
| 61 | + private static final String TAG_TARGET_HOST = "target.host"; |
| 62 | + |
| 63 | + private static final String TAG_TARGET_PORT = "target.port"; |
| 64 | + |
| 65 | + private static final String TAG_VALUE_UNKNOWN = "UNKNOWN"; |
| 66 | + |
| 67 | + private static final KeyValues TAGS_TARGET_UNKNOWN = KeyValues.of(TAG_TARGET_SCHEME, TAG_VALUE_UNKNOWN, |
| 68 | + TAG_TARGET_HOST, TAG_VALUE_UNKNOWN, TAG_TARGET_PORT, TAG_VALUE_UNKNOWN); |
| 69 | + |
| 70 | + @Override |
| 71 | + public KeyValues getLowCardinalityKeyValues(OkHttpContext context) { |
| 72 | + OkHttpMetricsEventListener.CallState state = context.getState(); |
| 73 | + Request request = state.request; |
| 74 | + boolean requestAvailable = request != null; |
| 75 | + Function<Request, String> urlMapper = context.getUrlMapper(); |
| 76 | + Iterable<Tag> extraTags = context.getExtraTags(); |
| 77 | + Iterable<BiFunction<Request, Response, Tag>> contextSpecificTags = context.getContextSpecificTags(); |
| 78 | + Iterable<Tag> unknownRequestTags = context.getUnknownRequestTags(); |
| 79 | + boolean includeHostTag = context.isIncludeHostTag(); |
| 80 | + KeyValues keyValues = KeyValues.of("method", requestAvailable ? request.method() : TAG_VALUE_UNKNOWN, "uri", |
| 81 | + getUriTag(urlMapper, state, request), "status", getStatusMessage(state.response, state.exception)) |
| 82 | + .and(tagsToKeyValues(stream(extraTags.spliterator(), false))) |
| 83 | + .and(stream(contextSpecificTags.spliterator(), false) |
| 84 | + .map(contextTag -> contextTag.apply(request, state.response)) |
| 85 | + .map(tag -> KeyValue.of(tag.getKey(), tag.getValue())).collect(toList())) |
| 86 | + .and(getRequestTags(request, tagsToKeyValues(stream(unknownRequestTags.spliterator(), false)))) |
| 87 | + .and(generateTagsForRoute(request)); |
| 88 | + if (includeHostTag) { |
| 89 | + keyValues = KeyValues.of(keyValues).and("host", |
| 90 | + requestAvailable ? request.url().host() : TAG_VALUE_UNKNOWN); |
| 91 | + } |
| 92 | + return keyValues; |
| 93 | + } |
| 94 | + |
| 95 | + private String getUriTag(Function<Request, String> urlMapper, OkHttpMetricsEventListener.CallState state, |
| 96 | + @Nullable Request request) { |
| 97 | + if (request == null) { |
| 98 | + return TAG_VALUE_UNKNOWN; |
| 99 | + } |
| 100 | + return state.response != null && (state.response.code() == 404 || state.response.code() == 301) ? "NOT_FOUND" |
| 101 | + : urlMapper.apply(request); |
| 102 | + } |
| 103 | + |
| 104 | + private String getStatusMessage(@Nullable Response response, @Nullable IOException exception) { |
| 105 | + if (exception != null) { |
| 106 | + return "IO_ERROR"; |
| 107 | + } |
| 108 | + |
| 109 | + if (response == null) { |
| 110 | + return "CLIENT_ERROR"; |
| 111 | + } |
| 112 | + |
| 113 | + return Integer.toString(response.code()); |
| 114 | + } |
| 115 | + |
| 116 | + private Iterable<KeyValue> getRequestTags(@Nullable Request request, Iterable<KeyValue> unknownRequestTags) { |
| 117 | + if (request == null) { |
| 118 | + return unknownRequestTags; |
| 119 | + } |
| 120 | + if (REQUEST_TAG_CLASS_EXISTS) { |
| 121 | + Tags requestTag = request.tag(Tags.class); |
| 122 | + if (requestTag != null) { |
| 123 | + return tagsToKeyValues(requestTag.stream()); |
| 124 | + } |
| 125 | + } |
| 126 | + Object requestTag = request.tag(); |
| 127 | + if (requestTag instanceof Tags) { |
| 128 | + return tagsToKeyValues(((Tags) requestTag).stream()); |
| 129 | + } |
| 130 | + return KeyValues.empty(); |
| 131 | + } |
| 132 | + |
| 133 | + private List<KeyValue> tagsToKeyValues(Stream<Tag> requestTag) { |
| 134 | + return requestTag.map(tag -> KeyValue.of(tag.getKey(), tag.getValue())).collect(Collectors.toList()); |
| 135 | + } |
| 136 | + |
| 137 | + private KeyValues generateTagsForRoute(@Nullable Request request) { |
| 138 | + if (request == null) { |
| 139 | + return TAGS_TARGET_UNKNOWN; |
| 140 | + } |
| 141 | + return KeyValues.of(TAG_TARGET_SCHEME, request.url().scheme(), TAG_TARGET_HOST, request.url().host(), |
| 142 | + TAG_TARGET_PORT, Integer.toString(request.url().port())); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments