|
11 | 11 | import dev.failsafe.event.ExecutionCompletedEvent; |
12 | 12 | import io.opentelemetry.api.common.Attributes; |
13 | 13 | import io.opentelemetry.api.metrics.LongCounter; |
14 | | -import io.opentelemetry.api.metrics.Meter; |
15 | 14 |
|
16 | 15 | final class CircuitBreakerEventListenerBuilders { |
17 | 16 | private CircuitBreakerEventListenerBuilders() {} |
18 | 17 |
|
19 | 18 | static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedFailureListener( |
20 | | - CircuitBreakerConfig<R> userConfig, Meter meter, Attributes attributes) { |
21 | | - LongCounter failureCounter = |
22 | | - meter |
23 | | - .counterBuilder("failsafe.circuit_breaker.failure.count") |
24 | | - .setDescription("Count of failed circuit breaker executions.") |
25 | | - .build(); |
| 19 | + CircuitBreakerConfig<R> userConfig, |
| 20 | + LongCounter executionCounter, |
| 21 | + Attributes commonAttributes) { |
| 22 | + Attributes attributes = |
| 23 | + commonAttributes.toBuilder().put("failsafe.circuit_breaker.outcome", "failure").build(); |
26 | 24 | EventListener<ExecutionCompletedEvent<R>> failureListener = userConfig.getFailureListener(); |
27 | 25 | return e -> { |
28 | | - failureCounter.add(1, attributes); |
| 26 | + executionCounter.add(1, attributes); |
29 | 27 | if (failureListener != null) { |
30 | 28 | failureListener.accept(e); |
31 | 29 | } |
32 | 30 | }; |
33 | 31 | } |
34 | 32 |
|
35 | 33 | static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedSuccessListener( |
36 | | - CircuitBreakerConfig<R> userConfig, Meter meter, Attributes attributes) { |
37 | | - LongCounter successCounter = |
38 | | - meter |
39 | | - .counterBuilder("failsafe.circuit_breaker.success.count") |
40 | | - .setDescription("Count of successful circuit breaker executions.") |
41 | | - .build(); |
| 34 | + CircuitBreakerConfig<R> userConfig, |
| 35 | + LongCounter executionCounter, |
| 36 | + Attributes commonAttributes) { |
| 37 | + Attributes attributes = |
| 38 | + commonAttributes.toBuilder().put("failsafe.circuit_breaker.outcome", "success").build(); |
42 | 39 | EventListener<ExecutionCompletedEvent<R>> successListener = userConfig.getSuccessListener(); |
43 | 40 | return e -> { |
44 | | - successCounter.add(1, attributes); |
| 41 | + executionCounter.add(1, attributes); |
45 | 42 | if (successListener != null) { |
46 | 43 | successListener.accept(e); |
47 | 44 | } |
48 | 45 | }; |
49 | 46 | } |
50 | 47 |
|
51 | 48 | static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedOpenListener( |
52 | | - CircuitBreakerConfig<R> userConfig, Meter meter, Attributes attributes) { |
53 | | - LongCounter openCircuitBreakerCounter = |
54 | | - meter |
55 | | - .counterBuilder("failsafe.circuit_breaker.open.count") |
56 | | - .setDescription("Count of times that circuit breaker was opened.") |
57 | | - .build(); |
| 49 | + CircuitBreakerConfig<R> userConfig, |
| 50 | + LongCounter stateChangesCounter, |
| 51 | + Attributes commonAttributes) { |
| 52 | + Attributes attributes = |
| 53 | + commonAttributes.toBuilder().put("failsafe.circuit_breaker.state", "open").build(); |
58 | 54 | EventListener<CircuitBreakerStateChangedEvent> openListener = userConfig.getOpenListener(); |
59 | 55 | return e -> { |
60 | | - openCircuitBreakerCounter.add(1, attributes); |
61 | | - openListener.accept(e); |
| 56 | + stateChangesCounter.add(1, attributes); |
| 57 | + if (openListener != null) { |
| 58 | + openListener.accept(e); |
| 59 | + } |
62 | 60 | }; |
63 | 61 | } |
64 | 62 |
|
65 | 63 | static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedHalfOpenListener( |
66 | | - CircuitBreakerConfig<R> userConfig, Meter meter, Attributes attributes) { |
67 | | - LongCounter halfOpenCircuitBreakerCounter = |
68 | | - meter |
69 | | - .counterBuilder("failsafe.circuit_breaker.half_open.count") |
70 | | - .setDescription("Count of times that circuit breaker was half-opened.") |
71 | | - .build(); |
| 64 | + CircuitBreakerConfig<R> userConfig, |
| 65 | + LongCounter stateChangesCounter, |
| 66 | + Attributes commonAttributes) { |
| 67 | + Attributes attributes = |
| 68 | + commonAttributes.toBuilder().put("failsafe.circuit_breaker.state", "half_open").build(); |
72 | 69 | EventListener<CircuitBreakerStateChangedEvent> halfOpenListener = |
73 | 70 | userConfig.getHalfOpenListener(); |
74 | 71 | return e -> { |
75 | | - halfOpenCircuitBreakerCounter.add(1, attributes); |
76 | | - halfOpenListener.accept(e); |
| 72 | + stateChangesCounter.add(1, attributes); |
| 73 | + if (halfOpenListener != null) { |
| 74 | + halfOpenListener.accept(e); |
| 75 | + } |
77 | 76 | }; |
78 | 77 | } |
79 | 78 |
|
80 | 79 | static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedCloseListener( |
81 | | - CircuitBreakerConfig<R> userConfig, Meter meter, Attributes attributes) { |
82 | | - LongCounter closedCircuitBreakerCounter = |
83 | | - meter |
84 | | - .counterBuilder("failsafe.circuit_breaker.closed.count") |
85 | | - .setDescription("Count of times that circuit breaker was closed.") |
86 | | - .build(); |
87 | | - EventListener<CircuitBreakerStateChangedEvent> closeListener = userConfig.getCloseListener(); |
| 80 | + CircuitBreakerConfig<R> userConfig, |
| 81 | + LongCounter stateChangesCounter, |
| 82 | + Attributes commonAttributes) { |
| 83 | + Attributes attributes = |
| 84 | + commonAttributes.toBuilder().put("failsafe.circuit_breaker.state", "closed").build(); |
| 85 | + EventListener<CircuitBreakerStateChangedEvent> closedListener = userConfig.getCloseListener(); |
88 | 86 | return e -> { |
89 | | - closedCircuitBreakerCounter.add(1, attributes); |
90 | | - closeListener.accept(e); |
| 87 | + stateChangesCounter.add(1, attributes); |
| 88 | + if (closedListener != null) { |
| 89 | + closedListener.accept(e); |
| 90 | + } |
91 | 91 | }; |
92 | 92 | } |
93 | 93 | } |
0 commit comments