-
Notifications
You must be signed in to change notification settings - Fork 1k
OkHttp3 instrumentation with observation api #3176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
97429e5
ba97f84
3cc22e9
ffbac1a
6b1c973
5177ef5
f95468d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright 2022 VMware, Inc. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.common; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* {@link KeyValue} with value validation. | ||
* | ||
* @param <T> | ||
* @author Marcin Grzejszczak | ||
* @since 1.10.0 | ||
*/ | ||
class ValidatedKeyValue<T> implements KeyValue { | ||
|
||
private final String key; | ||
|
||
private final T value; | ||
|
||
ValidatedKeyValue(String key, T value, Predicate<Object> validator) { | ||
this.key = key; | ||
this.value = assertValue(validator, value); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return this.key; | ||
} | ||
|
||
@Override | ||
public String getValue() { | ||
return String.valueOf(this.value); | ||
} | ||
|
||
private T assertValue(Predicate<Object> validator, T value) { | ||
if (!validator.test(value)) { | ||
throw new IllegalArgumentException( | ||
"Argument [" + this.value + "] does not follow required format for key [" + this.key + "]"); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "tag(" + key + "=" + value + ")"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2022 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.common.docs; | ||
|
||
/** | ||
* Renames the metric / trace / observation depending on standards. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 1.10.0 | ||
*/ | ||
public interface SemanticNameProvider<T> { | ||
|
||
/** | ||
* Will return a standardized name. | ||
* @return name | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Returns {@code true} when this {@link SemanticNameProvider} should be applied and a | ||
* new name should be set. | ||
* @param object object against which we determine whether this provider is applicable | ||
* or not | ||
* @return {@code true} when new name should be applied | ||
*/ | ||
boolean isApplicable(T object); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright 2017 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.okhttp3; | ||
|
||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.KeyValues; | ||
import io.micrometer.common.lang.NonNullApi; | ||
import io.micrometer.common.lang.NonNullFields; | ||
import io.micrometer.common.lang.Nullable; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static java.util.stream.StreamSupport.stream; | ||
|
||
@NonNullApi | ||
@NonNullFields | ||
public class DefaultOkHttpKeyValuesProvider implements OkHttpKeyValuesProvider { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking if we should separate instrumentation using the Observation API from using the Metrics API (binders). What do you think about using a separate package so it is easier to find (i.e.: |
||
|
||
static final boolean REQUEST_TAG_CLASS_EXISTS; | ||
|
||
static { | ||
REQUEST_TAG_CLASS_EXISTS = getMethod(Class.class) != null; | ||
} | ||
|
||
@Nullable | ||
private static Method getMethod(Class<?>... parameterTypes) { | ||
try { | ||
return Request.class.getMethod("tag", parameterTypes); | ||
} | ||
catch (NoSuchMethodException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private static final String TAG_TARGET_SCHEME = "target.scheme"; | ||
|
||
private static final String TAG_TARGET_HOST = "target.host"; | ||
|
||
private static final String TAG_TARGET_PORT = "target.port"; | ||
|
||
private static final String TAG_VALUE_UNKNOWN = "UNKNOWN"; | ||
|
||
private static final KeyValues TAGS_TARGET_UNKNOWN = KeyValues.of(TAG_TARGET_SCHEME, TAG_VALUE_UNKNOWN, | ||
TAG_TARGET_HOST, TAG_VALUE_UNKNOWN, TAG_TARGET_PORT, TAG_VALUE_UNKNOWN); | ||
|
||
@Override | ||
public KeyValues getLowCardinalityKeyValues(OkHttpContext context) { | ||
OkHttpMetricsEventListener.CallState state = context.getState(); | ||
Request request = state.request; | ||
boolean requestAvailable = request != null; | ||
Function<Request, String> urlMapper = context.getUrlMapper(); | ||
Iterable<Tag> extraTags = context.getExtraTags(); | ||
Iterable<BiFunction<Request, Response, Tag>> contextSpecificTags = context.getContextSpecificTags(); | ||
Iterable<Tag> unknownRequestTags = context.getUnknownRequestTags(); | ||
boolean includeHostTag = context.isIncludeHostTag(); | ||
KeyValues keyValues = KeyValues.of("method", requestAvailable ? request.method() : TAG_VALUE_UNKNOWN, "uri", | ||
getUriTag(urlMapper, state, request), "status", getStatusMessage(state.response, state.exception)) | ||
.and(tagsToKeyValues(stream(extraTags.spliterator(), false))) | ||
.and(stream(contextSpecificTags.spliterator(), false) | ||
.map(contextTag -> contextTag.apply(request, state.response)) | ||
.map(tag -> KeyValue.of(tag.getKey(), tag.getValue())).collect(toList())) | ||
.and(getRequestTags(request, tagsToKeyValues(stream(unknownRequestTags.spliterator(), false)))) | ||
.and(generateTagsForRoute(request)); | ||
if (includeHostTag) { | ||
keyValues = KeyValues.of(keyValues).and("host", | ||
requestAvailable ? request.url().host() : TAG_VALUE_UNKNOWN); | ||
} | ||
return keyValues; | ||
} | ||
|
||
private String getUriTag(Function<Request, String> urlMapper, OkHttpMetricsEventListener.CallState state, | ||
@Nullable Request request) { | ||
if (request == null) { | ||
return TAG_VALUE_UNKNOWN; | ||
} | ||
return state.response != null && (state.response.code() == 404 || state.response.code() == 301) ? "NOT_FOUND" | ||
: urlMapper.apply(request); | ||
} | ||
|
||
private String getStatusMessage(@Nullable Response response, @Nullable IOException exception) { | ||
if (exception != null) { | ||
return "IO_ERROR"; | ||
} | ||
|
||
if (response == null) { | ||
return "CLIENT_ERROR"; | ||
} | ||
|
||
return Integer.toString(response.code()); | ||
} | ||
|
||
private Iterable<KeyValue> getRequestTags(@Nullable Request request, Iterable<KeyValue> unknownRequestTags) { | ||
if (request == null) { | ||
return unknownRequestTags; | ||
} | ||
if (REQUEST_TAG_CLASS_EXISTS) { | ||
Tags requestTag = request.tag(Tags.class); | ||
if (requestTag != null) { | ||
return tagsToKeyValues(requestTag.stream()); | ||
} | ||
} | ||
Object requestTag = request.tag(); | ||
if (requestTag instanceof Tags) { | ||
return tagsToKeyValues(((Tags) requestTag).stream()); | ||
} | ||
return KeyValues.empty(); | ||
} | ||
|
||
private List<KeyValue> tagsToKeyValues(Stream<Tag> requestTag) { | ||
return requestTag.map(tag -> KeyValue.of(tag.getKey(), tag.getValue())).collect(Collectors.toList()); | ||
} | ||
|
||
private KeyValues generateTagsForRoute(@Nullable Request request) { | ||
if (request == null) { | ||
return TAGS_TARGET_UNKNOWN; | ||
} | ||
return KeyValues.of(TAG_TARGET_SCHEME, request.url().scheme(), TAG_TARGET_HOST, request.url().host(), | ||
TAG_TARGET_PORT, Integer.toString(request.url().port())); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EmptyBlockTag: A block tag (@param, @return, @throws, @deprecated) has an empty description. Block tags without descriptions don't add much value for future readers of the code; consider removing the tag entirely or adding a description.
Reply with "@sonatype-lift help" for more info.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.
When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.
Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]