Skip to content

Commit

Permalink
add ability to disable individual metric providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Nov 19, 2024
1 parent d28e662 commit d8a4c03
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 37 deletions.
16 changes: 16 additions & 0 deletions doc/modules/ROOT/pages/reference/metrics.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ It is possible to completely disable fault tolerance metrics using MicroProfile
----
MP_Fault_Tolerance_Metrics_Enabled=false
----

It is also possible to disable individual metric providers:

[source,properties]
----
# to disable MP Metrics
smallrye.faulttolerance.mpmetrics.disabled=true
# to disable OpenTelemetry Metrics
smallrye.faulttolerance.opentelemetry.disabled=true
# to disable Micrometer
smallrye.faulttolerance.micrometer.disabled=true
----

This applies when only one metric provider is present as well as when multiple metric providers are present.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@Alternative
@Priority(1)
public class CompoundMetricsProvider implements MetricsProvider {
private final boolean metricsEnabled;
private final boolean enabled;

private final MetricsProvider[] providers;

Expand All @@ -33,30 +33,45 @@ public class CompoundMetricsProvider implements MetricsProvider {
@Inject
CompoundMetricsProvider(
Instance<MetricsProvider> lookup,
@ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled) {
List<Class<? extends MetricsProvider>> allProviders = List.of(MicroProfileMetricsProvider.class,
OpenTelemetryProvider.class, MicrometerProvider.class);
@ConfigProperty(name = Constants.METRICS_ENABLED, defaultValue = "true") boolean metricsEnabled,
@ConfigProperty(name = Constants.MPMETRICS_DISABLED, defaultValue = "false") boolean mpMetricsDisabled,
@ConfigProperty(name = Constants.OPENTELEMETRY_DISABLED, defaultValue = "false") boolean openTelemetryDisabled,
@ConfigProperty(name = Constants.MICROMETER_DISABLED, defaultValue = "false") boolean micrometerDisabled) {

List<MetricsProvider> providers = new ArrayList<>();
for (Class<? extends MetricsProvider> clazz : allProviders) {
if (!mpMetricsDisabled) {
try {
providers.add(lookup.select(clazz).get());
providers.add(lookup.select(MicroProfileMetricsProvider.class).get());
} catch (Exception ignored) {
// either the bean does not exist, or some of its dependencies does not exist
}
}
this.metricsEnabled = providers.isEmpty() ? false : metricsEnabled;
if (!openTelemetryDisabled) {
try {
providers.add(lookup.select(OpenTelemetryProvider.class).get());
} catch (Exception ignored) {
// either the bean does not exist, or some of its dependencies does not exist
}
}
if (!micrometerDisabled) {
try {
providers.add(lookup.select(MicrometerProvider.class).get());
} catch (Exception ignored) {
// either the bean does not exist, or some of its dependencies does not exist
}
}
this.enabled = providers.isEmpty() ? false : metricsEnabled;
this.providers = providers.toArray(new MetricsProvider[0]);
}

@Override
public boolean isEnabled() {
return metricsEnabled;
return enabled;
}

@Override
public MetricsRecorder create(MeteredOperation operation) {
if (metricsEnabled) {
if (enabled) {
return cache.computeIfAbsent(operation.cacheKey(), ignored -> {
if (providers.length == 1) {
return providers[0].create(operation);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.smallrye.faulttolerance.metrics;

final class Constants {
static final String METRICS_ENABLED = "MP_Fault_Tolerance_Metrics_Enabled";
static final String MPMETRICS_DISABLED = "smallrye.faulttolerance.mpmetrics.disabled";
static final String OPENTELEMETRY_DISABLED = "smallrye.faulttolerance.opentelemetry.disabled";
static final String MICROMETER_DISABLED = "smallrye.faulttolerance.micrometer.disabled";
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@Singleton
public class MicroProfileMetricsProvider implements MetricsProvider {
private final boolean metricsEnabled;
private final boolean enabled;

private final MetricRegistry registry;

Expand All @@ -34,27 +34,30 @@ public class MicroProfileMetricsProvider implements MetricsProvider {
MicroProfileMetricsProvider(
// lazy for `CompoundMetricsProvider`
@RegistryType(type = MetricRegistry.Type.BASE) Provider<MetricRegistry> registry,
@ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled,
@ConfigProperty(name = Constants.METRICS_ENABLED, defaultValue = "true") boolean metricsEnabled,
@ConfigProperty(name = Constants.MPMETRICS_DISABLED, defaultValue = "false") boolean mpMetricsDisabled,
ExecutorHolder executorHolder) {
this.metricsEnabled = metricsEnabled;
this.enabled = metricsEnabled && !mpMetricsDisabled;
this.registry = registry.get();

Metadata metadata = Metadata.builder()
.withName(MetricsConstants.TIMER_SCHEDULED)
.withUnit(MetricUnits.NONE)
.build();
Timer timer = executorHolder.getTimer();
this.registry.gauge(metadata, timer, Timer::countScheduledTasks, new Tag("id", "" + timer.getId()));
if (enabled) {
Metadata metadata = Metadata.builder()
.withName(MetricsConstants.TIMER_SCHEDULED)
.withUnit(MetricUnits.NONE)
.build();
Timer timer = executorHolder.getTimer();
this.registry.gauge(metadata, timer, Timer::countScheduledTasks, new Tag("id", "" + timer.getId()));
}
}

@Override
public boolean isEnabled() {
return metricsEnabled;
return enabled;
}

@Override
public MetricsRecorder create(MeteredOperation operation) {
if (metricsEnabled) {
if (enabled) {
return cache.computeIfAbsent(operation.cacheKey(),
ignored -> new MicroProfileMetricsRecorder(registry, operation));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@Singleton
public class MicrometerProvider implements MetricsProvider {
private final boolean metricsEnabled;
private final boolean enabled;

private final MeterRegistry registry;

Expand All @@ -32,24 +32,27 @@ public class MicrometerProvider implements MetricsProvider {
MicrometerProvider(
// lazy for `CompoundMetricsProvider`
Provider<MeterRegistry> registry,
@ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled,
@ConfigProperty(name = Constants.METRICS_ENABLED, defaultValue = "true") boolean metricsEnabled,
@ConfigProperty(name = Constants.MICROMETER_DISABLED, defaultValue = "false") boolean micrometerDisabled,
ExecutorHolder executorHolder) {
this.metricsEnabled = metricsEnabled;
this.enabled = metricsEnabled && !micrometerDisabled;
this.registry = registry.get();

Timer timer = executorHolder.getTimer();
this.registry.gauge(MetricsConstants.TIMER_SCHEDULED, Collections.singletonList(Tag.of("id", "" + timer.getId())),
timer, Timer::countScheduledTasks);
if (enabled) {
Timer timer = executorHolder.getTimer();
this.registry.gauge(MetricsConstants.TIMER_SCHEDULED, Collections.singletonList(Tag.of("id", "" + timer.getId())),
timer, Timer::countScheduledTasks);
}
}

@Override
public boolean isEnabled() {
return metricsEnabled;
return enabled;
}

@Override
public MetricsRecorder create(MeteredOperation operation) {
if (metricsEnabled) {
if (enabled) {
return cache.computeIfAbsent(operation.cacheKey(),
ignored -> new MicrometerRecorder(registry, operation));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@Singleton
public class OpenTelemetryProvider implements MetricsProvider {
private final boolean metricsEnabled;
private final boolean enabled;

private final Meter meter;

Expand All @@ -32,25 +32,28 @@ public class OpenTelemetryProvider implements MetricsProvider {
OpenTelemetryProvider(
// lazy for `CompoundMetricsProvider`
Provider<Meter> meter,
@ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled,
@ConfigProperty(name = Constants.METRICS_ENABLED, defaultValue = "true") boolean metricsEnabled,
@ConfigProperty(name = Constants.OPENTELEMETRY_DISABLED, defaultValue = "false") boolean openTelemetryDisabled,
ExecutorHolder executorHolder) {
this.metricsEnabled = metricsEnabled;
this.enabled = metricsEnabled && !openTelemetryDisabled;
this.meter = meter.get();

Timer timer = executorHolder.getTimer();
Attributes attributes = Attributes.of(AttributeKey.stringKey("id"), "" + timer.getId());
this.meter.upDownCounterBuilder(MetricsConstants.TIMER_SCHEDULED)
.buildWithCallback(m -> m.record(timer.countScheduledTasks(), attributes));
if (enabled) {
Timer timer = executorHolder.getTimer();
Attributes attributes = Attributes.of(AttributeKey.stringKey("id"), "" + timer.getId());
this.meter.upDownCounterBuilder(MetricsConstants.TIMER_SCHEDULED)
.buildWithCallback(m -> m.record(timer.countScheduledTasks(), attributes));
}
}

@Override
public boolean isEnabled() {
return metricsEnabled;
return enabled;
}

@Override
public MetricsRecorder create(MeteredOperation operation) {
if (metricsEnabled) {
if (enabled) {
return cache.computeIfAbsent(operation.cacheKey(),
ignored -> new OpenTelemetryRecorder(meter, operation));
} else {
Expand Down

0 comments on commit d8a4c03

Please sign in to comment.