Skip to content

Commit

Permalink
Merge pull request #25169 from geoand/#25099
Browse files Browse the repository at this point in the history
Prevent NPE when micrometer is disabled
  • Loading branch information
ebullient authored Apr 26, 2022
2 parents 43afda6 + d594781 commit ab11fae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -46,23 +50,23 @@ 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;
}
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
Expand All @@ -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);
Expand Down Expand Up @@ -115,7 +128,7 @@ private Tag clientName(ClientRequestContext requestContext) {
}
}

class RestClientMetricInfo extends RequestMetricInfo {
static class RestClientMetricInfo extends RequestMetricInfo {
ClientRequestContext requestContext;

RestClientMetricInfo(ClientRequestContext requestContext) {
Expand Down

0 comments on commit ab11fae

Please sign in to comment.