diff --git a/doc/modules/ROOT/pages/reference/metrics.adoc b/doc/modules/ROOT/pages/reference/metrics.adoc index dea275cd..ee026c6d 100644 --- a/doc/modules/ROOT/pages/reference/metrics.adoc +++ b/doc/modules/ROOT/pages/reference/metrics.adoc @@ -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. diff --git a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/CompoundMetricsProvider.java b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/CompoundMetricsProvider.java index 56e05c0f..246da18f 100644 --- a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/CompoundMetricsProvider.java +++ b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/CompoundMetricsProvider.java @@ -24,7 +24,7 @@ @Alternative @Priority(1) public class CompoundMetricsProvider implements MetricsProvider { - private final boolean metricsEnabled; + private final boolean enabled; private final MetricsProvider[] providers; @@ -33,30 +33,45 @@ public class CompoundMetricsProvider implements MetricsProvider { @Inject CompoundMetricsProvider( Instance lookup, - @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled) { - List> 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 providers = new ArrayList<>(); - for (Class 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); diff --git a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/Constants.java b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/Constants.java new file mode 100644 index 00000000..8084688b --- /dev/null +++ b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/Constants.java @@ -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"; +} diff --git a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicroProfileMetricsProvider.java b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicroProfileMetricsProvider.java index 9153bf15..7eabe6f2 100644 --- a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicroProfileMetricsProvider.java +++ b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicroProfileMetricsProvider.java @@ -24,7 +24,7 @@ @Singleton public class MicroProfileMetricsProvider implements MetricsProvider { - private final boolean metricsEnabled; + private final boolean enabled; private final MetricRegistry registry; @@ -34,27 +34,30 @@ public class MicroProfileMetricsProvider implements MetricsProvider { MicroProfileMetricsProvider( // lazy for `CompoundMetricsProvider` @RegistryType(type = MetricRegistry.Type.BASE) Provider 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 { diff --git a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicrometerProvider.java b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicrometerProvider.java index f969fa24..40aa089a 100644 --- a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicrometerProvider.java +++ b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/MicrometerProvider.java @@ -22,7 +22,7 @@ @Singleton public class MicrometerProvider implements MetricsProvider { - private final boolean metricsEnabled; + private final boolean enabled; private final MeterRegistry registry; @@ -32,24 +32,27 @@ public class MicrometerProvider implements MetricsProvider { MicrometerProvider( // lazy for `CompoundMetricsProvider` Provider 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 { diff --git a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/OpenTelemetryProvider.java b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/OpenTelemetryProvider.java index 3a34a7e1..86e40368 100644 --- a/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/OpenTelemetryProvider.java +++ b/implementation/fault-tolerance/src/main/java/io/smallrye/faulttolerance/metrics/OpenTelemetryProvider.java @@ -22,7 +22,7 @@ @Singleton public class OpenTelemetryProvider implements MetricsProvider { - private final boolean metricsEnabled; + private final boolean enabled; private final Meter meter; @@ -32,25 +32,28 @@ public class OpenTelemetryProvider implements MetricsProvider { OpenTelemetryProvider( // lazy for `CompoundMetricsProvider` Provider 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 {