Skip to content
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

Receiver and SenderContext #3293

Merged
merged 6 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions micrometer-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies {
optionalApi 'org.jetbrains.kotlinx:kotlinx-coroutines-core'

testImplementation 'io.projectreactor:reactor-test'
testImplementation project(":micrometer-observation-test")

// dependency injection tests
testImplementation 'javax.inject:javax.inject'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public DefaultOkHttpObservationConvention(String metricName) {

@Override
public KeyValues getLowCardinalityKeyValues(OkHttpContext context) {
OkHttpMetricsEventListener.CallState state = context.getState();
OkHttpObservationInterceptor.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();
Iterable<KeyValue> extraTags = context.getExtraTags();
Iterable<BiFunction<Request, Response, KeyValue>> contextSpecificTags = context.getContextSpecificTags();
Iterable<KeyValue> unknownRequestTags = context.getUnknownRequestTags();
boolean includeHostTag = context.isIncludeHostTag();
// TODO: Tags to key values and back - maybe we can improve this?
KeyValues keyValues = KeyValues.of(
Expand All @@ -90,20 +90,19 @@ public KeyValues getLowCardinalityKeyValues(OkHttpContext context) {
OkHttpDocumentedObservation.OkHttpLegacyLowCardinalityTags.URI.of(getUriTag(urlMapper, state, request)),
OkHttpDocumentedObservation.OkHttpLegacyLowCardinalityTags.STATUS
.of(getStatusMessage(state.response, state.exception)))
.and(tagsToKeyValues(stream(extraTags.spliterator(), false)))
.and(extraTags)
.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));
.and(getRequestTags(request, unknownRequestTags)).and(generateTagsForRoute(request));
if (includeHostTag) {
keyValues = KeyValues.of(keyValues).and(OkHttpDocumentedObservation.OkHttpLegacyLowCardinalityTags.HOST
.of(requestAvailable ? request.url().host() : TAG_VALUE_UNKNOWN));
}
return keyValues;
}

private String getUriTag(Function<Request, String> urlMapper, OkHttpMetricsEventListener.CallState state,
private String getUriTag(Function<Request, String> urlMapper, OkHttpObservationInterceptor.CallState state,
@Nullable Request request) {
if (request == null) {
return TAG_VALUE_UNKNOWN;
Expand Down Expand Up @@ -133,11 +132,18 @@ private Iterable<KeyValue> getRequestTags(@Nullable Request request, Iterable<Ke
if (requestTag != null) {
return tagsToKeyValues(requestTag.stream());
}
KeyValues keyValues = request.tag(KeyValues.class);
if (keyValues != null) {
return keyValues;
}
}
Object requestTag = request.tag();
if (requestTag instanceof Tags) {
return tagsToKeyValues(((Tags) requestTag).stream());
}
else if (requestTag instanceof KeyValues) {
return (Iterable<KeyValue>) requestTag;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unchecked: unchecked cast

Reply with "@sonatype-lift help" for info about LiftBot commands.
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 ]

}
return KeyValues.empty();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,72 @@
*/
package io.micrometer.core.instrument.binder.okhttp3;

import io.micrometer.core.instrument.Tag;
import io.micrometer.observation.transport.http.context.HttpClientContext;
import io.micrometer.common.KeyValue;
import io.micrometer.observation.transport.SenderContext;
import io.micrometer.observation.transport.Kind;
import io.micrometer.observation.transport.RequestReplySenderContext;
import okhttp3.Request;
import okhttp3.Response;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
* A {@link HttpClientContext} for OkHttp3.
* A {@link SenderContext} for OkHttp3.
*
* @author Marcin Grzejszczak
* @since 1.10.0
*/
public class OkHttpContext extends HttpClientContext {

private final OkHttpMetricsEventListener.CallState state;
public class OkHttpContext extends RequestReplySenderContext<Request.Builder, Response> {

private final Function<Request, String> urlMapper;

private final Iterable<Tag> extraTags;
private final Iterable<KeyValue> extraTags;

private final Iterable<BiFunction<Request, Response, Tag>> contextSpecificTags;
private final Iterable<BiFunction<Request, Response, KeyValue>> contextSpecificTags;

private final Iterable<Tag> unknownRequestTags;
private final Iterable<KeyValue> unknownRequestTags;

private final boolean includeHostTag;

public OkHttpContext(OkHttpMetricsEventListener.CallState state, Function<Request, String> urlMapper,
Iterable<Tag> extraTags, Iterable<BiFunction<Request, Response, Tag>> contextSpecificTags,
Iterable<Tag> unknownRequestTags, boolean includeHostTag) {
this.state = state;
private OkHttpObservationInterceptor.CallState state;

public OkHttpContext(Function<Request, String> urlMapper, Iterable<KeyValue> extraTags,
Iterable<BiFunction<Request, Response, KeyValue>> contextSpecificTags,
Iterable<KeyValue> unknownRequestTags, boolean includeHostTag) {
super((carrier, key, value) -> {
if (carrier != null) {
carrier.header(key, value);
}
}, Kind.CLIENT);
this.urlMapper = urlMapper;
this.extraTags = extraTags;
this.contextSpecificTags = contextSpecificTags;
this.unknownRequestTags = unknownRequestTags;
this.includeHostTag = includeHostTag;
}

public OkHttpMetricsEventListener.CallState getState() {
public void setState(OkHttpObservationInterceptor.CallState state) {
this.state = state;
}

public OkHttpObservationInterceptor.CallState getState() {
return state;
}

public Function<Request, String> getUrlMapper() {
return urlMapper;
}

public Iterable<Tag> getExtraTags() {
public Iterable<KeyValue> getExtraTags() {
jonatan-ivanov marked this conversation as resolved.
Show resolved Hide resolved
return extraTags;
}

public Iterable<BiFunction<Request, Response, Tag>> getContextSpecificTags() {
public Iterable<BiFunction<Request, Response, KeyValue>> getContextSpecificTags() {
return contextSpecificTags;
}

public Iterable<Tag> getUnknownRequestTags() {
public Iterable<KeyValue> getUnknownRequestTags() {
return unknownRequestTags;
}

Expand Down
Loading