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

Adds a client tag to the default micrometer observation #2333

Merged
merged 3 commits into from
Feb 29, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 The Feign Authors
*
* 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
Expand All @@ -14,15 +14,16 @@
package feign.micrometer;

import feign.Request;
import feign.RequestTemplate;
import feign.Response;
import io.micrometer.common.KeyValues;
import io.micrometer.common.lang.Nullable;

/**
* Default implementation of {@link FeignObservationConvention}.
*
* @since 12.1
* @see FeignObservationConvention
* @since 12.1
*/
public class DefaultFeignObservationConvention implements FeignObservationConvention {

Expand All @@ -48,14 +49,16 @@ public String getContextualName(FeignContext context) {

@Override
public KeyValues getLowCardinalityKeyValues(FeignContext context) {
String templatedUrl = context.getCarrier().requestTemplate().methodMetadata().template().url();
RequestTemplate requestTemplate = context.getCarrier().requestTemplate();

Choose a reason for hiding this comment

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

Hey guys, just came across this PR, as I needed to include the client name and was writing a similar implementation, but noticed something in this change. Isn't context.getCarrier() nullable and shouldn't we check for null like it is done in getMethodString(@Nullable Request request) a bit further down?

return KeyValues.of(
FeignObservationDocumentation.HttpClientTags.METHOD
.withValue(getMethodString(context.getCarrier())),
FeignObservationDocumentation.HttpClientTags.URI
.withValue(templatedUrl),
.withValue(requestTemplate.methodMetadata().template().url()),
FeignObservationDocumentation.HttpClientTags.STATUS
.withValue(getStatusValue(context.getResponse())));
.withValue(getStatusValue(context.getResponse())),
FeignObservationDocumentation.HttpClientTags.CLIENT_NAME
.withValue(requestTemplate.feignTarget().type().getName()));
}

String getStatusValue(@Nullable Response response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 The Feign Authors
*
* 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
Expand All @@ -20,7 +20,7 @@

/**
* {@link ObservationDocumentation} for Feign.
*
*
* @since 12.1
*/
public enum FeignObservationDocumentation implements ObservationDocumentation {
Expand All @@ -37,6 +37,7 @@ public KeyName[] getLowCardinalityKeyNames() {
}
};


enum HttpClientTags implements KeyName {

STATUS {
Expand Down Expand Up @@ -74,6 +75,12 @@ public String asString() {
public String asString() {
return "net.peer.port";
}
},
CLIENT_NAME {
@Override
public String asString() {
return "clientName";
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 The Feign Authors
*
* 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
Expand All @@ -22,11 +22,6 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import feign.AsyncFeign;
Expand All @@ -35,6 +30,7 @@
import feign.Request;
import feign.RequestLine;
import feign.Response;
import io.micrometer.core.instrument.Meter.Id;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
Expand All @@ -43,11 +39,18 @@
import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.transport.RequestReplySenderContext;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@WireMockTest
class FeignHeaderInstrumentationTest {

static final String METER_NAME = "http.client.requests";

MeterRegistry meterRegistry = new SimpleMeterRegistry();

ObservationRegistry observationRegistry = ObservationRegistry.create();
Expand All @@ -67,9 +70,11 @@ void getTemplatedPathForUri(WireMockRuntimeInfo wmRuntimeInfo) {
testClient.templated("1", "2");

verify(getRequestedFor(urlEqualTo("/customers/1/carts/2")).withHeader("foo", equalTo("bar")));
Timer timer = meterRegistry.get("http.client.requests").timer();
Timer timer = meterRegistry.get(METER_NAME).timer();
assertThat(timer.count()).isEqualTo(1);
assertThat(timer.totalTime(TimeUnit.NANOSECONDS)).isPositive();

assertTags();
}

@Test
Expand All @@ -82,9 +87,49 @@ void getTemplatedPathForUriForAsync(WireMockRuntimeInfo wmRuntimeInfo)
testClient.templated("1", "2").get();

verify(getRequestedFor(urlEqualTo("/customers/1/carts/2")).withHeader("foo", equalTo("bar")));
Timer timer = meterRegistry.get("http.client.requests").timer();
Timer timer = meterRegistry.get(METER_NAME).timer();
assertThat(timer.count()).isEqualTo(1);
assertThat(timer.totalTime(TimeUnit.NANOSECONDS)).isPositive();

assertTags();
}

private void assertTags() {
Id requestsId = meterRegistry.get(METER_NAME).meter().getId();

assertMetricIdIncludesMethod(requestsId);
assertMetricIdIncludesURI(requestsId);
assertMetricIdIncludesStatus(requestsId);
assertsMetricIdIncludesClientName(requestsId);
}

private void assertMetricIdIncludesMethod(Id metricId) {
String tag = metricId.getTag("http.method");
assertThat(tag).as("Expect all metric names to have tag 'http.method': " + metricId)
.isNotNull();
assertThat(tag).as("Expect method to be GET: " + metricId).isEqualTo("GET");
}

private void assertMetricIdIncludesURI(Id metricId) {
String tag = metricId.getTag("http.url");
assertThat(tag).as("Expect all metric names to have tag 'http.url': " + metricId).isNotNull();
assertThat(tag).as("Expect url to match path template: " + metricId)
.isEqualTo("/customers/{customerId}/carts/{cartId}");
}

private void assertMetricIdIncludesStatus(Id metricId) {
String tag = metricId.getTag("http.status_code");
assertThat(tag).as("Expect all metric names to have tag 'http.status_code': " + metricId)
.isNotNull();
assertThat(tag).as("Expect status to be 200: " + metricId).isEqualTo("200");
}

private void assertsMetricIdIncludesClientName(Id metricId) {
String tag = metricId.getTag("clientName");
assertThat(tag).as("Expect all metric names to have tag 'clientName': " + metricId).isNotNull();
assertThat(tag).as("Expect class to be present: " + metricId)
.startsWith("feign.micrometer.FeignHeaderInstrumentationTest$");
assertThat(tag).endsWith("TestClient");
}

private TestClient clientInstrumentedWithObservations(String url) {
Expand All @@ -107,13 +152,15 @@ public interface TestClient {
String templated(@Param("customerId") String customerId, @Param("cartId") String cartId);
}


public interface AsyncTestClient {

@RequestLine("GET /customers/{customerId}/carts/{cartId}")
CompletableFuture<String> templated(@Param("customerId") String customerId,
@Param("cartId") String cartId);
}


static class HeaderMutatingHandler
implements ObservationHandler<RequestReplySenderContext<Request, Response>> {

Expand Down