diff --git a/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/MicrometerProcessor.java b/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/MicrometerProcessor.java index f3c45726769d0..e3e2e6e8076e2 100644 --- a/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/MicrometerProcessor.java +++ b/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/MicrometerProcessor.java @@ -75,6 +75,14 @@ public boolean getAsBoolean() { MicrometerConfig mConfig; + /** + * config objects are beans, but they are not unremoveable by default + */ + @BuildStep + UnremovableBeanBuildItem mpConfigAsBean() { + return UnremovableBeanBuildItem.beanTypes(MicrometerConfig.class); + } + @BuildStep(onlyIf = MicrometerEnabled.class) FeatureBuildItem feature() { return new FeatureBuildItem(Feature.MICROMETER); diff --git a/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/RestClientMetricsListener.java b/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/RestClientMetricsListener.java index 82457606eb9e5..5a67b2d89d59c 100644 --- a/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/RestClientMetricsListener.java +++ b/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/RestClientMetricsListener.java @@ -17,6 +17,7 @@ import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Timer; import io.quarkus.arc.Arc; +import io.quarkus.micrometer.runtime.config.MicrometerConfig; /** * This is initialized via ServiceFactory (static/non-CDI initialization) @@ -25,16 +26,19 @@ public class RestClientMetricsListener implements RestClientListener { private final static String REQUEST_METRIC_PROPERTY = "restClientMetrics"; - final MeterRegistry registry = Metrics.globalRegistry; - boolean initialized = false; - boolean clientMetricsEnabled = false; + private final MeterRegistry registry = Metrics.globalRegistry; + private boolean initialized = false; + private boolean clientMetricsEnabled = false; - HttpBinderConfiguration httpMetricsConfig; - MetricsClientRequestFilter clientRequestFilter; - MetricsClientResponseFilter clientResponseFilter; + private MetricsClientRequestFilter clientRequestFilter; + private MetricsClientResponseFilter clientResponseFilter; @Override public void onNewClient(Class serviceInterface, RestClientBuilder builder) { + MicrometerConfig micrometerConfig = Arc.container().instance(MicrometerConfig.class).get(); + if (!micrometerConfig.enabled) { + return; + } if (prepClientMetrics()) { // This must run AFTER the OpenTelmetry client request filter builder.register(this.clientRequestFilter, Priorities.HEADER_DECORATOR + 1); @@ -46,11 +50,11 @@ public void onNewClient(Class serviceInterface, RestClientBuilder builder) { boolean prepClientMetrics() { boolean clientMetricsEnabled = this.clientMetricsEnabled; if (!this.initialized) { - this.httpMetricsConfig = Arc.container().instance(HttpBinderConfiguration.class).get(); + HttpBinderConfiguration httpMetricsConfig = Arc.container().instance(HttpBinderConfiguration.class).get(); clientMetricsEnabled = httpMetricsConfig.isClientEnabled(); if (clientMetricsEnabled) { - this.clientRequestFilter = new MetricsClientRequestFilter(httpMetricsConfig); - this.clientResponseFilter = new MetricsClientResponseFilter(); + this.clientRequestFilter = new MetricsClientRequestFilter(registry); + this.clientResponseFilter = new MetricsClientResponseFilter(registry, httpMetricsConfig); } this.clientMetricsEnabled = clientMetricsEnabled; this.initialized = true; @@ -58,11 +62,11 @@ boolean prepClientMetrics() { return clientMetricsEnabled; } - class MetricsClientRequestFilter implements ClientRequestFilter { - HttpBinderConfiguration binderConfiguration; + static class MetricsClientRequestFilter implements ClientRequestFilter { + private final MeterRegistry registry; - MetricsClientRequestFilter(HttpBinderConfiguration binderConfiguration) { - this.binderConfiguration = binderConfiguration; + MetricsClientRequestFilter(MeterRegistry registry) { + this.registry = registry; } @Override @@ -73,7 +77,16 @@ public void filter(ClientRequestContext requestContext) throws IOException { } } - class MetricsClientResponseFilter implements ClientResponseFilter { + static class MetricsClientResponseFilter implements ClientResponseFilter { + private final MeterRegistry registry; + private final HttpBinderConfiguration httpMetricsConfig; + + MetricsClientResponseFilter(MeterRegistry registry, + HttpBinderConfiguration httpMetricsConfig) { + this.registry = registry; + this.httpMetricsConfig = httpMetricsConfig; + } + @Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { RequestMetricInfo requestMetric = getRequestMetric(requestContext); @@ -115,7 +128,7 @@ private Tag clientName(ClientRequestContext requestContext) { } } - class RestClientMetricInfo extends RequestMetricInfo { + static class RestClientMetricInfo extends RequestMetricInfo { ClientRequestContext requestContext; RestClientMetricInfo(ClientRequestContext requestContext) {