|
5 | 5 |
|
6 | 6 | package io.opentelemetry.instrumentation.failsafe.v3_0; |
7 | 7 |
|
8 | | -import static org.hamcrest.CoreMatchers.equalTo; |
9 | | -import static org.hamcrest.MatcherAssert.assertThat; |
10 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
11 | 11 |
|
| 12 | +import dev.failsafe.CircuitBreaker; |
12 | 13 | import dev.failsafe.CircuitBreakerOpenException; |
13 | 14 | import dev.failsafe.Failsafe; |
14 | | -import io.opentelemetry.api.common.AttributeKey; |
15 | | -import io.opentelemetry.api.common.Attributes; |
16 | 15 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
17 | 16 | import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; |
18 | | -import io.opentelemetry.sdk.metrics.data.LongPointData; |
19 | | -import io.opentelemetry.sdk.metrics.data.MetricData; |
20 | 17 | import io.opentelemetry.sdk.testing.assertj.MetricAssert; |
21 | 18 | import java.time.Duration; |
22 | 19 | import java.util.Objects; |
23 | | -import org.junit.jupiter.api.Nested; |
24 | 20 | import org.junit.jupiter.api.Test; |
25 | 21 | import org.junit.jupiter.api.extension.RegisterExtension; |
26 | 22 |
|
27 | 23 | final class FailsafeTelemetryTest { |
28 | 24 | @RegisterExtension |
29 | 25 | static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); |
30 | 26 |
|
31 | | - @Nested |
32 | | - final class CircuitBreaker { |
33 | | - @Test |
34 | | - void should_Capture_CircuitBreaker_Metrics() { |
35 | | - // given |
36 | | - dev.failsafe.CircuitBreaker<Object> userCircuitBreaker = |
37 | | - dev.failsafe.CircuitBreaker.builder() |
38 | | - .handleResultIf(Objects::isNull) |
39 | | - .withFailureThreshold(2) |
40 | | - .withDelay(Duration.ZERO) |
41 | | - .withSuccessThreshold(2) |
42 | | - .build(); |
43 | | - FailsafeTelemetry failsafeTelemetry = FailsafeTelemetry.create(testing.getOpenTelemetry()); |
44 | | - dev.failsafe.CircuitBreaker<Object> instrumentedCircuitBreaker = |
45 | | - failsafeTelemetry.createCircuitBreaker(userCircuitBreaker, "testing"); |
| 27 | + @Test |
| 28 | + void captureCircuitBreakerMetrics() { |
| 29 | + // given |
| 30 | + CircuitBreaker<Object> userCircuitBreaker = |
| 31 | + dev.failsafe.CircuitBreaker.builder() |
| 32 | + .handleResultIf(Objects::isNull) |
| 33 | + .withFailureThreshold(2) |
| 34 | + .withDelay(Duration.ZERO) |
| 35 | + .withSuccessThreshold(2) |
| 36 | + .build(); |
| 37 | + FailsafeTelemetry failsafeTelemetry = FailsafeTelemetry.create(testing.getOpenTelemetry()); |
| 38 | + CircuitBreaker<Object> instrumentedCircuitBreaker = |
| 39 | + failsafeTelemetry.createCircuitBreaker(userCircuitBreaker, "testing"); |
46 | 40 |
|
47 | | - // when |
48 | | - for (int i = 0; i < 5; i++) { |
49 | | - try { |
50 | | - int temp = i; |
51 | | - Failsafe.with(instrumentedCircuitBreaker) |
52 | | - .get( |
53 | | - () -> { |
54 | | - if (temp < 2) { |
55 | | - return null; |
56 | | - } else { |
57 | | - return new Object(); |
58 | | - } |
59 | | - }); |
60 | | - } catch (CircuitBreakerOpenException e) { |
61 | | - assertThat(i, equalTo(2)); |
62 | | - } |
| 41 | + // when |
| 42 | + for (int i = 0; i < 5; i++) { |
| 43 | + try { |
| 44 | + int temp = i; |
| 45 | + Failsafe.with(instrumentedCircuitBreaker) |
| 46 | + .get( |
| 47 | + () -> { |
| 48 | + if (temp < 2) { |
| 49 | + return null; |
| 50 | + } else { |
| 51 | + return new Object(); |
| 52 | + } |
| 53 | + }); |
| 54 | + } catch (CircuitBreakerOpenException e) { |
| 55 | + assertThat(i).isEqualTo(2); |
63 | 56 | } |
64 | | - |
65 | | - // then |
66 | | - testing.waitAndAssertMetrics( |
67 | | - "io.opentelemetry.failsafe-3.0", |
68 | | - metricAssert -> |
69 | | - assertCircuitBreakerMetric(metricAssert, "failsafe.circuitbreaker.failure.count", 2), |
70 | | - metricAssert -> |
71 | | - assertCircuitBreakerMetric(metricAssert, "failsafe.circuitbreaker.success.count", 3), |
72 | | - metricAssert -> |
73 | | - assertCircuitBreakerMetric(metricAssert, "failsafe.circuitbreaker.open.count", 1), |
74 | | - metricAssert -> |
75 | | - assertCircuitBreakerMetric(metricAssert, "failsafe.circuitbreaker.halfopen.count", 1), |
76 | | - metricAssert -> |
77 | | - assertCircuitBreakerMetric(metricAssert, "failsafe.circuitbreaker.closed.count", 1)); |
78 | 57 | } |
| 58 | + |
| 59 | + // then |
| 60 | + testing.waitAndAssertMetrics( |
| 61 | + "io.opentelemetry.failsafe-3.0", |
| 62 | + metricAssert -> |
| 63 | + assertCircuitBreakerMetric(metricAssert, "failsafe.circuit_breaker.failure.count", 2), |
| 64 | + metricAssert -> |
| 65 | + assertCircuitBreakerMetric(metricAssert, "failsafe.circuit_breaker.success.count", 3), |
| 66 | + metricAssert -> |
| 67 | + assertCircuitBreakerMetric(metricAssert, "failsafe.circuit_breaker.open.count", 1), |
| 68 | + metricAssert -> |
| 69 | + assertCircuitBreakerMetric(metricAssert, "failsafe.circuit_breaker.half_open.count", 1), |
| 70 | + metricAssert -> |
| 71 | + assertCircuitBreakerMetric(metricAssert, "failsafe.circuit_breaker.closed.count", 1)); |
79 | 72 | } |
80 | 73 |
|
81 | 74 | private static void assertCircuitBreakerMetric( |
82 | 75 | MetricAssert metricAssert, String counterName, long expectedValue) { |
83 | | - MetricData closeCountData = metricAssert.actual(); |
84 | | - assertThat(closeCountData.getName(), equalTo(counterName)); |
85 | | - assertTrue(closeCountData.getData().getPoints().stream().findFirst().isPresent()); |
86 | | - LongPointData closeCountLongPointData = |
87 | | - (LongPointData) closeCountData.getData().getPoints().stream().findFirst().get(); |
88 | | - assertThat( |
89 | | - closeCountLongPointData.getAttributes(), |
90 | | - equalTo(Attributes.of(AttributeKey.stringKey("name"), "testing"))); |
91 | | - assertThat(closeCountLongPointData.getValue(), equalTo(expectedValue)); |
| 76 | + metricAssert |
| 77 | + .hasName(counterName) |
| 78 | + .hasLongSumSatisfying( |
| 79 | + sum -> |
| 80 | + sum.isMonotonic() |
| 81 | + .hasPointsSatisfying( |
| 82 | + point -> |
| 83 | + point |
| 84 | + .hasValue(expectedValue) |
| 85 | + .hasAttributesSatisfyingExactly( |
| 86 | + equalTo(stringKey("name"), "testing")))); |
92 | 87 | } |
93 | 88 | } |
0 commit comments