Skip to content

Commit

Permalink
Merge pull request #44016 from brunobat/micrometer-exemplars-on-http
Browse files Browse the repository at this point in the history
Micrometer exemplars on HTTP
  • Loading branch information
brunobat authored Nov 13, 2024
2 parents 173e489 + fffb6cb commit f02d1ad
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import io.quarkus.micrometer.runtime.MicrometerRecorder;
import io.quarkus.micrometer.runtime.MicrometerTimedInterceptor;
import io.quarkus.micrometer.runtime.config.MicrometerConfig;
import io.quarkus.micrometer.runtime.export.exemplars.NoopOpenTelemetryExemplarContextUnwrapper;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.metrics.MetricsFactory;
import io.quarkus.vertx.http.deployment.NonApplicationRootPathBuildItem;
Expand Down Expand Up @@ -93,6 +94,15 @@ MetricsCapabilityBuildItem metricsCapabilityBuildItem() {
null);
}

@BuildStep(onlyIfNot = PrometheusRegistryProcessor.PrometheusEnabled.class)
void registerEmptyExamplarProvider(
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(NoopOpenTelemetryExemplarContextUnwrapper.class)
.setUnremovable()
.build());
}

@BuildStep(onlyIf = { PrometheusRegistryProcessor.PrometheusEnabled.class })
MetricsCapabilityBuildItem metricsCapabilityPrometheusBuildItem(
NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import io.quarkus.micrometer.runtime.MicrometerRecorder;
import io.quarkus.micrometer.runtime.config.MicrometerConfig;
import io.quarkus.micrometer.runtime.config.PrometheusConfigGroup;
import io.quarkus.micrometer.runtime.export.EmptyExemplarSamplerProvider;
import io.quarkus.micrometer.runtime.export.OpentelemetryExemplarSamplerProvider;
import io.quarkus.micrometer.runtime.export.PrometheusRecorder;
import io.quarkus.micrometer.runtime.export.exemplars.EmptyExemplarSamplerProvider;
import io.quarkus.micrometer.runtime.export.exemplars.NoopOpenTelemetryExemplarContextUnwrapper;
import io.quarkus.micrometer.runtime.export.exemplars.OpenTelemetryExemplarContextUnwrapper;
import io.quarkus.micrometer.runtime.export.exemplars.OpentelemetryExemplarSamplerProvider;
import io.quarkus.vertx.http.deployment.NonApplicationRootPathBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.management.ManagementInterfaceBuildTimeConfig;
Expand Down Expand Up @@ -73,6 +75,7 @@ void registerOpentelemetryExemplarSamplerProvider(
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(OpentelemetryExemplarSamplerProvider.class)
.addBeanClass(OpenTelemetryExemplarContextUnwrapper.class)
.setUnremovable()
.build());
}
Expand All @@ -82,6 +85,7 @@ void registerEmptyExamplarProvider(
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(EmptyExemplarSamplerProvider.class)
.addBeanClass(NoopOpenTelemetryExemplarContextUnwrapper.class)
.setUnremovable()
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.quarkus.micrometer.runtime.HttpServerMetricsTagsContributor;
import io.quarkus.micrometer.runtime.binder.HttpBinderConfiguration;
import io.quarkus.micrometer.runtime.binder.HttpCommonTags;
import io.quarkus.micrometer.runtime.export.exemplars.OpenTelemetryContextUnwrapper;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.ServerWebSocket;
Expand All @@ -40,6 +41,7 @@ public class VertxHttpServerMetrics extends VertxTcpServerMetrics
static final Logger log = Logger.getLogger(VertxHttpServerMetrics.class);

HttpBinderConfiguration config;
OpenTelemetryContextUnwrapper openTelemetryContextUnwrapper;

final LongAdder activeRequests;

Expand All @@ -49,9 +51,12 @@ public class VertxHttpServerMetrics extends VertxTcpServerMetrics

private final List<HttpServerMetricsTagsContributor> httpServerMetricsTagsContributors;

VertxHttpServerMetrics(MeterRegistry registry, HttpBinderConfiguration config) {
VertxHttpServerMetrics(MeterRegistry registry,
HttpBinderConfiguration config,
OpenTelemetryContextUnwrapper openTelemetryContextUnwrapper) {
super(registry, "http.server", null);
this.config = config;
this.openTelemetryContextUnwrapper = openTelemetryContextUnwrapper;

activeRequests = new LongAdder();
Gauge.builder(config.getHttpServerActiveRequestsName(), activeRequests, LongAdder::doubleValue)
Expand Down Expand Up @@ -164,12 +169,14 @@ public void requestReset(HttpRequestMetric requestMetric) {
if (path != null) {
Timer.Sample sample = requestMetric.getSample();

sample.stop(requestsTimer
.withTags(Tags.of(
openTelemetryContextUnwrapper.executeInContext(
sample::stop,
requestsTimer.withTags(Tags.of(
VertxMetricsTags.method(requestMetric.request().method()),
HttpCommonTags.uri(path, requestMetric.initialPath, 0),
Outcome.CLIENT_ERROR.asTag(),
HttpCommonTags.STATUS_RESET)));
HttpCommonTags.STATUS_RESET)),
requestMetric.request().context());
}
requestMetric.requestEnded();
}
Expand Down Expand Up @@ -207,7 +214,10 @@ public void responseEnd(HttpRequestMetric requestMetric, HttpResponse response,
}
}

sample.stop(requestsTimer.withTags(allTags));
openTelemetryContextUnwrapper.executeInContext(
sample::stop,
requestsTimer.withTags(allTags),
requestMetric.request().context());
}
requestMetric.requestEnded();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.quarkus.micrometer.runtime.binder.HttpBinderConfiguration;
import io.quarkus.micrometer.runtime.export.exemplars.OpenTelemetryContextUnwrapper;
import io.quarkus.vertx.http.runtime.ExtendedQuarkusVertxHttpMetrics;
import io.vertx.core.VertxOptions;
import io.vertx.core.datagram.DatagramSocketOptions;
Expand All @@ -37,11 +38,14 @@ public class VertxMeterBinderAdapter extends MetricsOptions
public static final String METRIC_NAME_SEPARATOR = "|";

private HttpBinderConfiguration httpBinderConfiguration;
private OpenTelemetryContextUnwrapper openTelemetryContextUnwrapper;

public VertxMeterBinderAdapter() {
}

void setHttpConfig(HttpBinderConfiguration httpBinderConfiguration) {
void initBinder(HttpBinderConfiguration httpBinderConfiguration,
OpenTelemetryContextUnwrapper openTelemetryContextUnwrapper) {
this.openTelemetryContextUnwrapper = openTelemetryContextUnwrapper;
this.httpBinderConfiguration = httpBinderConfiguration;
}

Expand Down Expand Up @@ -70,9 +74,12 @@ public MetricsOptions newOptions() {
if (httpBinderConfiguration == null) {
throw new NoStackTraceException("HttpBinderConfiguration was not found");
}
if (openTelemetryContextUnwrapper == null) {
throw new NoStackTraceException("OpenTelemetryContextUnwrapper was not found");
}
if (httpBinderConfiguration.isServerEnabled()) {
log.debugf("Create HttpServerMetrics with options %s and address %s", options, localAddress);
return new VertxHttpServerMetrics(Metrics.globalRegistry, httpBinderConfiguration);
return new VertxHttpServerMetrics(Metrics.globalRegistry, httpBinderConfiguration, openTelemetryContextUnwrapper);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import io.quarkus.arc.Arc;
import io.quarkus.micrometer.runtime.binder.HttpBinderConfiguration;
import io.quarkus.micrometer.runtime.export.exemplars.OpenTelemetryContextUnwrapper;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.annotations.Recorder;
import io.vertx.core.VertxOptions;
Expand All @@ -30,18 +31,20 @@ public void accept(VertxOptions vertxOptions) {
/* RUNTIME_INIT */
public void configureBinderAdapter() {
HttpBinderConfiguration httpConfig = Arc.container().instance(HttpBinderConfiguration.class).get();
OpenTelemetryContextUnwrapper openTelemetryContextUnwrapper = Arc.container()
.instance(OpenTelemetryContextUnwrapper.class).get();
if (LaunchMode.current() == LaunchMode.DEVELOPMENT) {
if (devModeConfig == null) {
// Create an object whose attributes we can update
devModeConfig = httpConfig.unwrap();
binderAdapter.setHttpConfig(devModeConfig);
binderAdapter.initBinder(devModeConfig, openTelemetryContextUnwrapper);
} else {
// update config attributes
devModeConfig.update(httpConfig);
}
} else {
// unwrap the CDI bean (use POJO)
binderAdapter.setHttpConfig(httpConfig.unwrap());
binderAdapter.initBinder(httpConfig.unwrap(), openTelemetryContextUnwrapper);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.micrometer.runtime.export;
package io.quarkus.micrometer.runtime.export.exemplars;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.micrometer.runtime.export.exemplars;

import java.util.function.Function;

import jakarta.enterprise.context.Dependent;

import io.vertx.core.Context;

@Dependent
public class NoopOpenTelemetryExemplarContextUnwrapper implements OpenTelemetryContextUnwrapper {

@Override
public <P, R> R executeInContext(Function<P, R> methodReference, P parameter, Context requestContext) {
return methodReference.apply(parameter);// pass through
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.micrometer.runtime.export.exemplars;

import java.util.function.Function;

public interface OpenTelemetryContextUnwrapper {
/**
* Called when an HTTP server response has ended.
* Makes sure exemplars are produced because they have an OTel context.
*
* @param methodReference Ex: Sample stop method reference
* @param parameter The parameter to pass to the method
* @param requestContext The request context
* @param <P> The parameter type is a type of metric, ex: Timer
* @param <R> The return type of the method pointed by the methodReference
* @return The result of the method
*/
<P, R> R executeInContext(Function<P, R> methodReference, P parameter, io.vertx.core.Context requestContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.micrometer.runtime.export.exemplars;

import java.util.function.Function;

import jakarta.enterprise.context.Dependent;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.quarkus.opentelemetry.runtime.QuarkusContextStorage;

@Dependent
public class OpenTelemetryExemplarContextUnwrapper implements OpenTelemetryContextUnwrapper {

@Override
public <P, R> R executeInContext(Function<P, R> methodReference, P parameter, io.vertx.core.Context requestContext) {
if (requestContext == null) {
return methodReference.apply(parameter);
}

Context newContext = QuarkusContextStorage.getContext(requestContext);

if (newContext == null) {
return methodReference.apply(parameter);
}

io.opentelemetry.context.Context oldContext = QuarkusContextStorage.INSTANCE.current();
try (Scope scope = QuarkusContextStorage.INSTANCE.attach(newContext)) {
return methodReference.apply(parameter);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.micrometer.runtime.export;
package io.quarkus.micrometer.runtime.export.exemplars;

import java.util.Optional;
import java.util.function.Function;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkus.it.micrometer.prometheus;

import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.when;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;

/**
* See Micrometer Guide
*/
@QuarkusTest
@TestProfile(OtelOffProfile.class)
public class ExemplarOffTest {

@Test
void testExemplar() {
when().get("/example/prime/257").then().statusCode(200);
when().get("/example/prime/7919").then().statusCode(200);

String metricMatch = "http_server_requests_seconds_count{dummy=\"value\",env=\"test\"," +
"env2=\"test\",foo=\"UNSET\",method=\"GET\",outcome=\"SUCCESS\"," +
"registry=\"prometheus\",status=\"200\",uri=\"/example/prime/{number}\"} 2.0 # {span_id=\"";

await().atMost(5, SECONDS).untilAsserted(() -> {
assertFalse(get("/q/metrics").then().extract().asString().contains(metricMatch));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.it.micrometer.prometheus;

import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.when;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;

/**
* See Micrometer Guide
*/
@QuarkusTest
@TestProfile(OtelOnProfile.class)
public class ExemplarTest {

@Test
void testExemplar() {
when().get("/example/prime/257").then().statusCode(200);
when().get("/example/prime/7919").then().statusCode(200);

String metricMatch = "http_server_requests_seconds_count{dummy=\"value\",env=\"test\"," +
"env2=\"test\",foo=\"UNSET\",method=\"GET\",outcome=\"SUCCESS\"," +
"registry=\"prometheus\",status=\"200\",uri=\"/example/prime/{number}\"} 2.0 # {span_id=\"";

await().atMost(5, SECONDS).untilAsserted(() -> {
String body = get("/q/metrics").then().extract().asString();
assertTrue(body.contains(metricMatch), body);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.it.micrometer.prometheus;

import java.util.HashMap;
import java.util.Map;

import io.quarkus.test.junit.QuarkusTestProfile;

public class OtelOffProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
Map<String, String> config = new HashMap<>(Map.of(
"quarkus.otel.enabled", "false"));
return config;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.it.micrometer.prometheus;

import java.util.HashMap;
import java.util.Map;

import io.quarkus.test.junit.QuarkusTestProfile;

public class OtelOnProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
Map<String, String> config = new HashMap<>(Map.of(
"quarkus.otel.enabled", "true"));
return config;
}
}

0 comments on commit f02d1ad

Please sign in to comment.