Skip to content

Commit

Permalink
Few updates based on PR review.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Jan 15, 2025
1 parent 3cf18a1 commit 8bb48cb
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 26 deletions.
7 changes: 3 additions & 4 deletions docs/src/main/asciidoc/se/fault-tolerance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ the application developer or automatically by the API.
|===
^|Name ^|Tags ^|Description
|ft.bulkhead.calls.total | name="<bulkhead-name>" | Counter for all calls entering a bulkhead
|ft.bulkhead.waitingDuration | name="<bulkhead-name>" | Histogram of waiting times to enter a bulkhead
|ft.bulkhead.waitingDuration | name="<bulkhead-name>" | Distribution summary of waiting times to enter a bulkhead
|ft.bulkhead.executionsRunning | name="<bulkhead-name>" | Gauge whose value is the number of executions running in a bulkhead
|ft.bulkhead.executionsWaiting | name="<bulkhead-name>" | Gauge whose value is the number of executions waiting in a bulkhead
|===
Expand All @@ -304,14 +304,14 @@ closed to open state
|===
^|Name ^|Tags ^|Description
|ft.timeout.calls.total | name="<timeout-name>" | Counter for all calls entering a timeout
|ft.timeout.executionDuration | name="<timeout-name>" | Histogram of all execution durations in a timeout
|ft.timeout.executionDuration | name="<timeout-name>" | Distribution summary of all execution durations in a timeout
|===
=== Enabling Metrics Programmatically
Metrics can be enabled programmatically either globally or, if disabled globally, individually for
each command instance. To enable metrics globally, call `FaultTolerance.config(Config)` passing
a Config instance that sets `ft.metrics.enabled=true`. This must be done on application startup,
a Config instance that sets `ft.metrics.default-enabled=true`. This must be done on application startup,
before any command instances are created.
If metrics are not enabled globally, they can be enabled programmatically on each command instance
Expand All @@ -327,7 +327,6 @@ include::{sourcedir}/se/FaultToleranceSnippets.java[tag=snippet_8, indent=0]
NOTE: The global config setting always takes precedence: that is, if metrics are enabled
globally, they *cannot* be disabled individually by calling `enableMetrics(false)`.
== Examples
See <<API>> section for examples.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates.
* Copyright (c) 2020, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface BulkheadConfigBlueprint extends Prototype.Factory<Bulkhead> {
/**
* Flag to enable metrics for this instance. The value of this flag is
* combined with the global config entry
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_ENABLED}.
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_DEFAULT_ENABLED}.
* If either of these flags is {@code true}, then metrics will be enabled
* for the instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BulkheadImpl implements Bulkhead {
this.inProgressLock = new ReentrantLock(true);
this.config = config;

this.metricsEnabled = config.enableMetrics() || MetricsUtils.enableMetrics();
this.metricsEnabled = config.enableMetrics() || MetricsUtils.defaultEnabled();
if (metricsEnabled) {
Tag nameTag = Tag.create("name", name);
callsCounterMetric = MetricsUtils.counterBuilder(FT_BULKHEAD_CALLS_TOTAL, nameTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ interface CircuitBreakerConfigBlueprint extends Prototype.Factory<CircuitBreaker
/**
* Flag to enable metrics for this instance. The value of this flag is
* combined with the global config entry
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_ENABLED}.
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_DEFAULT_ENABLED}.
* If either of these flags is {@code true}, then metrics will be enabled
* for the instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CircuitBreakerImpl implements CircuitBreaker {
this.name = config.name().orElseGet(() -> "circuit-breaker-" + System.identityHashCode(config));
this.config = config;

this.metricsEnabled = config.enableMetrics() || MetricsUtils.enableMetrics();
this.metricsEnabled = config.enableMetrics() || MetricsUtils.defaultEnabled();
if (metricsEnabled) {
Tag nameTag = Tag.create("name", name);
callsCounterMetric = MetricsUtils.counterBuilder(FT_CIRCUITBREAKER_CALLS_TOTAL, nameTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public final class FaultTolerance {

/**
* Config key to enable metrics in Fault Tolerance. This flag can be overridden by
* each FT operation. See for example {@link BulkheadConfigBlueprint#enableMetrics()}.
* each FT command builder. See for example {@link BulkheadConfigBlueprint#enableMetrics()}.
* All metrics are disabled by default.
*/
public static final String FT_METRICS_ENABLED = "ft.metrics.enabled";
public static final String FT_METRICS_DEFAULT_ENABLED = "ft.metrics.default-enabled";

private static final System.Logger LOGGER = System.getLogger(FaultTolerance.class.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.helidon.metrics.api.Tag;
import io.helidon.metrics.api.Timer;

import static io.helidon.faulttolerance.FaultTolerance.FT_METRICS_ENABLED;
import static io.helidon.faulttolerance.FaultTolerance.FT_METRICS_DEFAULT_ENABLED;
import static io.helidon.metrics.api.Meter.Scope.VENDOR;

@SuppressWarnings("unchecked")
Expand All @@ -37,7 +37,7 @@ class MetricsUtils {
private static final LazyValue<MetricsFactory> METRICS_FACTORY = LazyValue.create(MetricsFactory::getInstance);
private static final LazyValue<MeterRegistry> METRICS_REGISTRY = LazyValue.create(Metrics::globalRegistry);

private static volatile Boolean enableMetrics;
private static volatile Boolean defaultEnabled;

private MetricsUtils() {
}
Expand All @@ -48,12 +48,12 @@ private MetricsUtils() {
*
* @return value of metrics flag
*/
static boolean enableMetrics() {
if (enableMetrics == null) {
static boolean defaultEnabled() {
if (defaultEnabled == null) {
Config config = FaultTolerance.config();
enableMetrics = config.get(FT_METRICS_ENABLED).asBoolean().orElse(false);
defaultEnabled = config.get(FT_METRICS_DEFAULT_ENABLED).asBoolean().orElse(false);
}
return enableMetrics;
return defaultEnabled;
}

static <T extends Number> void gaugeBuilder(String name, Supplier<T> supplier, Tag... tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ interface RetryConfigBlueprint extends Prototype.Factory<Retry> {
/**
* Flag to enable metrics for this instance. The value of this flag is
* combined with the global config entry
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_ENABLED}.
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_DEFAULT_ENABLED}.
* If either of these flags is {@code true}, then metrics will be enabled
* for the instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RetryImpl implements Retry {
this.retryPolicy = config.retryPolicy().orElseThrow();
this.retryConfig = config;

this.metricsEnabled = config.enableMetrics() || MetricsUtils.enableMetrics();
this.metricsEnabled = config.enableMetrics() || MetricsUtils.defaultEnabled();
if (metricsEnabled) {
Tag nameTag = Tag.create("name", name);
callsCounterMetric = MetricsUtils.counterBuilder(FT_RETRY_CALLS_TOTAL, nameTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ interface TimeoutConfigBlueprint extends Prototype.Factory<Timeout> {
/**
* Flag to enable metrics for this instance. The value of this flag is
* combined with the global config entry
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_ENABLED}.
* {@link io.helidon.faulttolerance.FaultTolerance#FT_METRICS_DEFAULT_ENABLED}.
* If either of these flags is {@code true}, then metrics will be enabled
* for the instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TimeoutImpl implements Timeout {
this.name = config.name().orElseGet(() -> "timeout-" + System.identityHashCode(config));
this.config = config;

this.metricsEnabled = config.enableMetrics() || MetricsUtils.enableMetrics();
this.metricsEnabled = config.enableMetrics() || MetricsUtils.defaultEnabled();
if (metricsEnabled) {
Tag nameTag = Tag.create("name", name);
callsCounterMetric = MetricsUtils.counterBuilder(FT_TIMEOUT_CALLS_TOTAL, nameTag);
Expand All @@ -74,15 +74,15 @@ public <T> T invoke(Supplier<? extends T> supplier) {
long start = metricsEnabled ? System.nanoTime() : 0L;
if (!currentThread) {
try {
T res = CompletableFuture.supplyAsync(supplier, executor)
return CompletableFuture.supplyAsync(supplier, executor)
.orTimeout(timeoutMillis, TimeUnit.MILLISECONDS)
.get();
} catch (Throwable t) {
throw mapThrowable(t, null);
} finally {
if (metricsEnabled) {
executionDurationMetric.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
}
return res;
} catch (Throwable t) {
throw mapThrowable(t, null);
}
} else {
Thread thisThread = Thread.currentThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

ft:
metrics:
enabled: true
default-enabled: true

0 comments on commit 8bb48cb

Please sign in to comment.