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 19691dd6..cd90aa44 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,15 +24,15 @@ @Alternative @Priority(1) public class CompoundMetricsProvider implements MetricsProvider { - @Inject - @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") - boolean metricsEnabled; - - private final Map cache = new ConcurrentHashMap<>(); + private final boolean metricsEnabled; private final MetricsProvider[] providers; - CompoundMetricsProvider() { + private final Map cache = new ConcurrentHashMap<>(); + + @Inject + CompoundMetricsProvider( + @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled) { CDI cdi = CDI.current(); List> allProviders = List.of(MicroProfileMetricsProvider.class, OpenTelemetryProvider.class, MicrometerProvider.class); @@ -42,9 +42,10 @@ public class CompoundMetricsProvider implements MetricsProvider { try { providers.add(cdi.select(clazz).get()); } catch (Exception ignored) { - // either the bean does not exist, or some of its dependencies are not injectable + // either the bean does not exist, or some of its dependencies does not exist } } + this.metricsEnabled = providers.isEmpty() ? false : metricsEnabled; this.providers = providers.toArray(new MetricsProvider[0]); } @@ -57,6 +58,10 @@ public boolean isEnabled() { public MetricsRecorder create(MeteredOperation operation) { if (metricsEnabled) { return cache.computeIfAbsent(operation.cacheKey(), ignored -> { + if (providers.length == 1) { + return providers[0].create(operation); + } + MetricsRecorder[] recorders = new MetricsRecorder[providers.length]; for (int i = 0; i < providers.length; i++) { recorders[i] = providers[i].create(operation); 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 720faeb1..9153bf15 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 @@ -3,8 +3,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import jakarta.annotation.PostConstruct; import jakarta.inject.Inject; +import jakarta.inject.Provider; import jakarta.inject.Singleton; import org.eclipse.microprofile.config.inject.ConfigProperty; @@ -24,31 +24,27 @@ @Singleton public class MicroProfileMetricsProvider implements MetricsProvider { - @Inject - @RegistryType(type = MetricRegistry.Type.BASE) - MetricRegistry registry; - - @Inject - @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") - boolean metricsEnabled; + private final boolean metricsEnabled; - @Inject - ExecutorHolder executorHolder; + private final MetricRegistry registry; private final Map cache = new ConcurrentHashMap<>(); - @PostConstruct - void init() { - if (!metricsEnabled) { - return; - } + @Inject + MicroProfileMetricsProvider( + // lazy for `CompoundMetricsProvider` + @RegistryType(type = MetricRegistry.Type.BASE) Provider registry, + @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled, + ExecutorHolder executorHolder) { + this.metricsEnabled = metricsEnabled; + this.registry = registry.get(); Metadata metadata = Metadata.builder() .withName(MetricsConstants.TIMER_SCHEDULED) .withUnit(MetricUnits.NONE) .build(); Timer timer = executorHolder.getTimer(); - registry.gauge(metadata, timer, Timer::countScheduledTasks, new Tag("id", "" + timer.getId())); + this.registry.gauge(metadata, timer, Timer::countScheduledTasks, new Tag("id", "" + timer.getId())); } @Override 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 45695d76..f969fa24 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 @@ -4,8 +4,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import jakarta.annotation.PostConstruct; import jakarta.inject.Inject; +import jakarta.inject.Provider; import jakarta.inject.Singleton; import org.eclipse.microprofile.config.inject.ConfigProperty; @@ -22,26 +22,23 @@ @Singleton public class MicrometerProvider implements MetricsProvider { - @Inject - MeterRegistry registry; - - @Inject - @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") - boolean metricsEnabled; + private final boolean metricsEnabled; - @Inject - ExecutorHolder executorHolder; + private final MeterRegistry registry; private final Map cache = new ConcurrentHashMap<>(); - @PostConstruct - void init() { - if (!metricsEnabled) { - return; - } + @Inject + MicrometerProvider( + // lazy for `CompoundMetricsProvider` + Provider registry, + @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled, + ExecutorHolder executorHolder) { + this.metricsEnabled = metricsEnabled; + this.registry = registry.get(); Timer timer = executorHolder.getTimer(); - registry.gauge(MetricsConstants.TIMER_SCHEDULED, Collections.singletonList(Tag.of("id", "" + timer.getId())), + this.registry.gauge(MetricsConstants.TIMER_SCHEDULED, Collections.singletonList(Tag.of("id", "" + timer.getId())), timer, Timer::countScheduledTasks); } 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 cb163834..3a34a7e1 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 @@ -3,8 +3,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import jakarta.annotation.PostConstruct; import jakarta.inject.Inject; +import jakarta.inject.Provider; import jakarta.inject.Singleton; import org.eclipse.microprofile.config.inject.ConfigProperty; @@ -22,27 +22,24 @@ @Singleton public class OpenTelemetryProvider implements MetricsProvider { - @Inject - Meter meter; - - @Inject - @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") - boolean metricsEnabled; + private final boolean metricsEnabled; - @Inject - ExecutorHolder executorHolder; + private final Meter meter; private final Map cache = new ConcurrentHashMap<>(); - @PostConstruct - void init() { - if (!metricsEnabled) { - return; - } + @Inject + OpenTelemetryProvider( + // lazy for `CompoundMetricsProvider` + Provider meter, + @ConfigProperty(name = "MP_Fault_Tolerance_Metrics_Enabled", defaultValue = "true") boolean metricsEnabled, + ExecutorHolder executorHolder) { + this.metricsEnabled = metricsEnabled; + this.meter = meter.get(); Timer timer = executorHolder.getTimer(); Attributes attributes = Attributes.of(AttributeKey.stringKey("id"), "" + timer.getId()); - meter.upDownCounterBuilder(MetricsConstants.TIMER_SCHEDULED) + this.meter.upDownCounterBuilder(MetricsConstants.TIMER_SCHEDULED) .buildWithCallback(m -> m.record(timer.countScheduledTasks(), attributes)); }