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

Prevent NPE when micrometer is disabled #25169

Merged
merged 2 commits into from
Apr 26, 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
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