Skip to content
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 @@ -244,7 +244,7 @@ interface Builder<T, R> {
* <p>
* The description may be an arbitrary string. Duplicates are permitted.
* <p>
* If no description is set, a random UUID is used.
* If no description is set, no metrics are emitted and a random UUID is used for other purposes.
*
* @param value a description, must not be {@code null}
* @return this fault tolerance builder
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/ROOT/pages/reference/programmatic-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ private static final FaultTolerance<String> guarded = FaultTolerance.<String>cre
It is possible to create multiple `FaultTolerance` objects with the same description.
In this case, it won't be possbile to distinguish the different `FaultTolerance` objects in metrics; their values will be aggregated.

If no description is provided, a random UUID is used.
If no description is provided, metrics are not emitted.

== Integration Concerns

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public static final class BuilderImpl<T, R> implements Builder<T, R> {
private final Function<FaultTolerance<T>, R> finisher;

private String description;
private boolean descriptionSet;
private BulkheadBuilderImpl<T, R> bulkheadBuilder;
private CircuitBreakerBuilderImpl<T, R> circuitBreakerBuilder;
private FallbackBuilderImpl<T, R> fallbackBuilder;
Expand All @@ -156,11 +157,13 @@ public BuilderImpl(BuilderEagerDependencies eagerDependencies, Supplier<BuilderL
this.finisher = finisher;

this.description = UUID.randomUUID().toString();
this.descriptionSet = false;
}

@Override
public Builder<T, R> withDescription(String value) {
this.description = Preconditions.checkNotNull(value, "Description must be set");
this.descriptionSet = true;
return this;
}

Expand Down Expand Up @@ -324,7 +327,7 @@ private FaultToleranceStrategy<T> buildSyncStrategy(BuilderLazyDependencies lazy
fallbackBuilder.whenPredicate));
}

if (lazyDependencies.metricsProvider().isEnabled()) {
if (lazyDependencies.metricsProvider().isEnabled() && descriptionSet) {
MeteredOperation meteredOperation = buildMeteredOperation();
result = new MetricsCollector<>(result, lazyDependencies.metricsProvider().create(meteredOperation),
meteredOperation);
Expand Down Expand Up @@ -406,7 +409,7 @@ private <V> FaultToleranceStrategy<CompletionStage<V>> buildAsyncStrategy(Builde
fallbackBuilder.whenPredicate));
}

if (lazyDependencies.metricsProvider().isEnabled()) {
if (lazyDependencies.metricsProvider().isEnabled() && descriptionSet) {
MeteredOperation meteredOperation = buildMeteredOperation();
result = new CompletionStageMetricsCollector<>(result,
lazyDependencies.metricsProvider().create(meteredOperation), meteredOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
Expand Down Expand Up @@ -62,7 +63,9 @@ public static void tearDown() throws InterruptedException {
}

@Test
public void test() throws Exception {
public void metricsWithDescription() throws Exception {
long oldCounters = metrics.getMeters().stream().filter(it -> it instanceof Counter).count();

Callable<String> guarded = FaultTolerance.createCallable(this::action)
.withDescription(NAME)
.withFallback().handler(this::fallback).done()
Expand All @@ -71,6 +74,8 @@ public void test() throws Exception {

assertThat(guarded.call()).isEqualTo("fallback");

assertThat(metrics.getMeters().stream().filter(it -> it instanceof Counter).count()).isGreaterThan(oldCounters);

assertThat(metrics.counter(MetricsConstants.INVOCATIONS_TOTAL, List.of(
Tag.of("method", NAME),
Tag.of("result", "valueReturned"),
Expand All @@ -87,6 +92,20 @@ public void test() throws Exception {
.count()).isEqualTo(1.0);
}

@Test
public void noMetricsWithoutDescription() throws Exception {
long oldCounters = metrics.getMeters().stream().filter(it -> it instanceof Counter).count();

Callable<String> guarded = FaultTolerance.createCallable(this::action)
.withFallback().handler(this::fallback).done()
.withRetry().maxRetries(3).done()
.build();

assertThat(guarded.call()).isEqualTo("fallback");

assertThat(metrics.getMeters().stream().filter(it -> it instanceof Counter).count()).isEqualTo(oldCounters);
}

public String action() throws TestException {
throw new TestException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class CdiMetricsTest {
private static final String NAME = CdiMetricsTest.class.getName() + " programmatic usage";

@Test
public void test(@RegistryType(type = MetricRegistry.Type.BASE) MetricRegistry metrics) throws Exception {
public void metricsWithDescription(@RegistryType(type = MetricRegistry.Type.BASE) MetricRegistry metrics) throws Exception {
int oldCounters = metrics.getCounters().size();

Callable<String> guarded = FaultTolerance.createCallable(this::action)
.withDescription(NAME)
.withFallback().handler(this::fallback).done()
Expand All @@ -29,6 +31,8 @@ public void test(@RegistryType(type = MetricRegistry.Type.BASE) MetricRegistry m

assertThat(guarded.call()).isEqualTo("fallback");

assertThat(metrics.getCounters().size()).isGreaterThan(oldCounters);

assertThat(metrics.counter(MetricsConstants.INVOCATIONS_TOTAL,
new Tag("method", NAME),
new Tag("result", "valueReturned"),
Expand All @@ -45,6 +49,21 @@ public void test(@RegistryType(type = MetricRegistry.Type.BASE) MetricRegistry m
.getCount()).isEqualTo(1);
}

@Test
public void noMetricsWithoutDescription(@RegistryType(type = MetricRegistry.Type.BASE) MetricRegistry metrics)
throws Exception {
int oldCounters = metrics.getCounters().size();

Callable<String> guarded = FaultTolerance.createCallable(this::action)
.withFallback().handler(this::fallback).done()
.withRetry().maxRetries(3).done()
.build();

assertThat(guarded.call()).isEqualTo("fallback");

assertThat(metrics.getCounters().size()).isEqualTo(oldCounters);
}

public String action() throws TestException {
throw new TestException();
}
Expand Down