|
5 | 5 |
|
6 | 6 | package io.opentelemetry.instrumentation.failsafe.v3_0; |
7 | 7 |
|
| 8 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 9 | + |
8 | 10 | import dev.failsafe.CircuitBreakerConfig; |
9 | 11 | import dev.failsafe.event.CircuitBreakerStateChangedEvent; |
10 | 12 | import dev.failsafe.event.EventListener; |
11 | 13 | import dev.failsafe.event.ExecutionCompletedEvent; |
| 14 | +import io.opentelemetry.api.common.AttributeKey; |
12 | 15 | import io.opentelemetry.api.common.Attributes; |
13 | 16 | import io.opentelemetry.api.metrics.LongCounter; |
14 | 17 |
|
15 | 18 | 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 | + |
16 | 23 | private CircuitBreakerEventListenerBuilders() {} |
17 | 24 |
|
18 | 25 | static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedFailureListener( |
19 | 26 | CircuitBreakerConfig<R> userConfig, |
20 | 27 | LongCounter executionCounter, |
21 | 28 | 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()); |
31 | 31 | } |
32 | 32 |
|
33 | 33 | static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedSuccessListener( |
34 | 34 | CircuitBreakerConfig<R> userConfig, |
35 | 35 | LongCounter executionCounter, |
36 | 36 | 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()); |
46 | 39 | } |
47 | 40 |
|
48 | 41 | static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedOpenListener( |
49 | 42 | CircuitBreakerConfig<R> userConfig, |
50 | 43 | LongCounter stateChangesCounter, |
51 | 44 | 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()); |
61 | 47 | } |
62 | 48 |
|
63 | 49 | static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedHalfOpenListener( |
64 | 50 | CircuitBreakerConfig<R> userConfig, |
65 | 51 | LongCounter stateChangesCounter, |
66 | 52 | 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()); |
77 | 55 | } |
78 | 56 |
|
79 | 57 | static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedCloseListener( |
80 | 58 | CircuitBreakerConfig<R> userConfig, |
81 | 59 | LongCounter stateChangesCounter, |
82 | 60 | 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) { |
86 | 67 | 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); |
90 | 71 | } |
91 | 72 | }; |
92 | 73 | } |
|
0 commit comments