Skip to content

Commit 2d60508

Browse files
committed
review
1 parent af6013f commit 2d60508

File tree

2 files changed

+25
-52
lines changed

2 files changed

+25
-52
lines changed

instrumentation/failsafe-3.0/library/src/main/java/io/opentelemetry/instrumentation/failsafe/v3_0/CircuitBreakerEventListenerBuilders.java

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,88 +5,69 @@
55

66
package io.opentelemetry.instrumentation.failsafe.v3_0;
77

8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
810
import dev.failsafe.CircuitBreakerConfig;
911
import dev.failsafe.event.CircuitBreakerStateChangedEvent;
1012
import dev.failsafe.event.EventListener;
1113
import dev.failsafe.event.ExecutionCompletedEvent;
14+
import io.opentelemetry.api.common.AttributeKey;
1215
import io.opentelemetry.api.common.Attributes;
1316
import io.opentelemetry.api.metrics.LongCounter;
1417

1518
final class CircuitBreakerEventListenerBuilders {
19+
private static final AttributeKey<String> OUTCOME_KEY =
20+
stringKey("failsafe.circuit_breaker.outcome");
21+
private static final AttributeKey<String> STATE_KEY = stringKey("failsafe.circuit_breaker.state");
22+
1623
private CircuitBreakerEventListenerBuilders() {}
1724

1825
static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedFailureListener(
1926
CircuitBreakerConfig<R> userConfig,
2027
LongCounter executionCounter,
2128
Attributes commonAttributes) {
22-
Attributes attributes =
23-
commonAttributes.toBuilder().put("failsafe.circuit_breaker.outcome", "failure").build();
24-
EventListener<ExecutionCompletedEvent<R>> failureListener = userConfig.getFailureListener();
25-
return e -> {
26-
executionCounter.add(1, attributes);
27-
if (failureListener != null) {
28-
failureListener.accept(e);
29-
}
30-
};
29+
Attributes attributes = commonAttributes.toBuilder().put(OUTCOME_KEY, "failure").build();
30+
return count(executionCounter, attributes, userConfig.getFailureListener());
3131
}
3232

3333
static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedSuccessListener(
3434
CircuitBreakerConfig<R> userConfig,
3535
LongCounter executionCounter,
3636
Attributes commonAttributes) {
37-
Attributes attributes =
38-
commonAttributes.toBuilder().put("failsafe.circuit_breaker.outcome", "success").build();
39-
EventListener<ExecutionCompletedEvent<R>> successListener = userConfig.getSuccessListener();
40-
return e -> {
41-
executionCounter.add(1, attributes);
42-
if (successListener != null) {
43-
successListener.accept(e);
44-
}
45-
};
37+
Attributes attributes = commonAttributes.toBuilder().put(OUTCOME_KEY, "success").build();
38+
return count(executionCounter, attributes, userConfig.getSuccessListener());
4639
}
4740

4841
static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedOpenListener(
4942
CircuitBreakerConfig<R> userConfig,
5043
LongCounter stateChangesCounter,
5144
Attributes commonAttributes) {
52-
Attributes attributes =
53-
commonAttributes.toBuilder().put("failsafe.circuit_breaker.state", "open").build();
54-
EventListener<CircuitBreakerStateChangedEvent> openListener = userConfig.getOpenListener();
55-
return e -> {
56-
stateChangesCounter.add(1, attributes);
57-
if (openListener != null) {
58-
openListener.accept(e);
59-
}
60-
};
45+
Attributes attributes = commonAttributes.toBuilder().put(STATE_KEY, "open").build();
46+
return count(stateChangesCounter, attributes, userConfig.getOpenListener());
6147
}
6248

6349
static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedHalfOpenListener(
6450
CircuitBreakerConfig<R> userConfig,
6551
LongCounter stateChangesCounter,
6652
Attributes commonAttributes) {
67-
Attributes attributes =
68-
commonAttributes.toBuilder().put("failsafe.circuit_breaker.state", "half_open").build();
69-
EventListener<CircuitBreakerStateChangedEvent> halfOpenListener =
70-
userConfig.getHalfOpenListener();
71-
return e -> {
72-
stateChangesCounter.add(1, attributes);
73-
if (halfOpenListener != null) {
74-
halfOpenListener.accept(e);
75-
}
76-
};
53+
Attributes attributes = commonAttributes.toBuilder().put(STATE_KEY, "half_open").build();
54+
return count(stateChangesCounter, attributes, userConfig.getHalfOpenListener());
7755
}
7856

7957
static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedCloseListener(
8058
CircuitBreakerConfig<R> userConfig,
8159
LongCounter stateChangesCounter,
8260
Attributes commonAttributes) {
83-
Attributes attributes =
84-
commonAttributes.toBuilder().put("failsafe.circuit_breaker.state", "closed").build();
85-
EventListener<CircuitBreakerStateChangedEvent> closedListener = userConfig.getCloseListener();
61+
Attributes attributes = commonAttributes.toBuilder().put(STATE_KEY, "closed").build();
62+
return count(stateChangesCounter, attributes, userConfig.getCloseListener());
63+
}
64+
65+
private static <T> EventListener<T> count(
66+
LongCounter counter, Attributes attributes, EventListener<T> delegate) {
8667
return e -> {
87-
stateChangesCounter.add(1, attributes);
88-
if (closedListener != null) {
89-
closedListener.accept(e);
68+
counter.add(1, attributes);
69+
if (delegate != null) {
70+
delegate.accept(e);
9071
}
9172
};
9273
}

instrumentation/failsafe-3.0/library/src/test/java/io/opentelemetry/instrumentation/failsafe/v3_0/FailsafeTelemetryTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,7 @@ void captureCircuitBreakerMetrics() {
4343
for (int i = 0; i < 5; i++) {
4444
try {
4545
int temp = i;
46-
Failsafe.with(instrumentedCircuitBreaker)
47-
.get(
48-
() -> {
49-
if (temp < 2) {
50-
return null;
51-
} else {
52-
return new Object();
53-
}
54-
});
46+
Failsafe.with(instrumentedCircuitBreaker).get(() -> temp < 2 ? null : new Object());
5547
} catch (CircuitBreakerOpenException e) {
5648
assertThat(i).isEqualTo(2);
5749
}

0 commit comments

Comments
 (0)