|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.failsafe.v3_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 9 | + |
| 10 | +import dev.failsafe.CircuitBreakerConfig; |
| 11 | +import dev.failsafe.event.CircuitBreakerStateChangedEvent; |
| 12 | +import dev.failsafe.event.EventListener; |
| 13 | +import dev.failsafe.event.ExecutionCompletedEvent; |
| 14 | +import io.opentelemetry.api.common.AttributeKey; |
| 15 | +import io.opentelemetry.api.common.Attributes; |
| 16 | +import io.opentelemetry.api.metrics.LongCounter; |
| 17 | + |
| 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 | + |
| 23 | + private CircuitBreakerEventListenerBuilders() {} |
| 24 | + |
| 25 | + static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedFailureListener( |
| 26 | + CircuitBreakerConfig<R> userConfig, |
| 27 | + LongCounter executionCounter, |
| 28 | + Attributes commonAttributes) { |
| 29 | + Attributes attributes = commonAttributes.toBuilder().put(OUTCOME_KEY, "failure").build(); |
| 30 | + return count(executionCounter, attributes, userConfig.getFailureListener()); |
| 31 | + } |
| 32 | + |
| 33 | + static <R> EventListener<ExecutionCompletedEvent<R>> buildInstrumentedSuccessListener( |
| 34 | + CircuitBreakerConfig<R> userConfig, |
| 35 | + LongCounter executionCounter, |
| 36 | + Attributes commonAttributes) { |
| 37 | + Attributes attributes = commonAttributes.toBuilder().put(OUTCOME_KEY, "success").build(); |
| 38 | + return count(executionCounter, attributes, userConfig.getSuccessListener()); |
| 39 | + } |
| 40 | + |
| 41 | + static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedOpenListener( |
| 42 | + CircuitBreakerConfig<R> userConfig, |
| 43 | + LongCounter stateChangesCounter, |
| 44 | + Attributes commonAttributes) { |
| 45 | + Attributes attributes = commonAttributes.toBuilder().put(STATE_KEY, "open").build(); |
| 46 | + return count(stateChangesCounter, attributes, userConfig.getOpenListener()); |
| 47 | + } |
| 48 | + |
| 49 | + static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedHalfOpenListener( |
| 50 | + CircuitBreakerConfig<R> userConfig, |
| 51 | + LongCounter stateChangesCounter, |
| 52 | + Attributes commonAttributes) { |
| 53 | + Attributes attributes = commonAttributes.toBuilder().put(STATE_KEY, "half_open").build(); |
| 54 | + return count(stateChangesCounter, attributes, userConfig.getHalfOpenListener()); |
| 55 | + } |
| 56 | + |
| 57 | + static <R> EventListener<CircuitBreakerStateChangedEvent> buildInstrumentedCloseListener( |
| 58 | + CircuitBreakerConfig<R> userConfig, |
| 59 | + LongCounter stateChangesCounter, |
| 60 | + Attributes commonAttributes) { |
| 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) { |
| 67 | + return e -> { |
| 68 | + counter.add(1, attributes); |
| 69 | + if (delegate != null) { |
| 70 | + delegate.accept(e); |
| 71 | + } |
| 72 | + }; |
| 73 | + } |
| 74 | +} |
0 commit comments