|
1 | 1 | package org.zalando.riptide.autoconfigure; |
2 | 2 |
|
3 | | -import org.junit.jupiter.api.Test; |
4 | 3 | import org.junit.jupiter.params.ParameterizedTest; |
5 | 4 | import org.junit.jupiter.params.provider.CsvSource; |
6 | 5 | import org.springframework.util.ReflectionUtils; |
7 | 6 | import org.zalando.riptide.Plugin; |
8 | 7 | import org.zalando.riptide.opentelemetry.OpenTelemetryPlugin; |
9 | 8 | import org.zalando.riptide.opentelemetry.span.CompositeSpanDecorator; |
| 9 | +import org.zalando.riptide.opentelemetry.span.FlowIdSpanDecorator; |
10 | 10 | import org.zalando.riptide.opentelemetry.span.RetrySpanDecorator; |
11 | 11 | import org.zalando.riptide.opentelemetry.span.SpanDecorator; |
12 | 12 |
|
|
16 | 16 | import java.util.stream.StreamSupport; |
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
19 | | -import static org.junit.jupiter.api.Assertions.*; |
20 | 19 |
|
21 | 20 | public class OpenTelemetryPluginFactoryTest { |
22 | 21 |
|
23 | 22 | private static final Field SPAN_DECORATOR_FIELD = ReflectionUtils.findField(OpenTelemetryPlugin.class, "spanDecorator"); |
24 | 23 | private static final Field DECORATORS_FIELD = ReflectionUtils.findField(CompositeSpanDecorator.class, "decorators"); |
| 24 | + private static final Field PROPAGATE_FLOW_ID_FIELD = ReflectionUtils.findField(FlowIdSpanDecorator.class, "propagateFlowId"); |
25 | 25 |
|
26 | 26 | static { |
27 | 27 | SPAN_DECORATOR_FIELD.setAccessible(true); |
28 | 28 | DECORATORS_FIELD.setAccessible(true); |
| 29 | + PROPAGATE_FLOW_ID_FIELD.setAccessible(true); |
29 | 30 | } |
30 | 31 |
|
31 | 32 | @ParameterizedTest |
32 | 33 | @CsvSource({ |
33 | 34 | "true, RetrySpanDecorator should be added when retry is enabled", |
34 | 35 | "false, RetrySpanDecorator should not be added when retry is disabled" |
35 | 36 | }) |
36 | | - void shouldCreatePluginWithRetrySpanDecoratorWhenClientRetryEnabled( |
37 | | - final boolean isRetryEnabled, |
38 | | - final String message |
| 37 | + void shouldAddRetrySpanDecoratorWhenEnabled( |
| 38 | + final boolean isRetryEnabled, |
| 39 | + final String message |
39 | 40 | ) throws IllegalAccessException { |
40 | | - final Plugin plugin = OpenTelemetryPluginFactory.create(createTestClient(isRetryEnabled)); |
| 41 | + final Plugin plugin = OpenTelemetryPluginFactory.create(createTestClient(isRetryEnabled, false)); |
41 | 42 |
|
42 | | - assertNotNull(plugin); |
43 | | - assertInstanceOf(OpenTelemetryPlugin.class, plugin); |
| 43 | + assertThat(plugin) |
| 44 | + .isNotNull() |
| 45 | + .isInstanceOf(OpenTelemetryPlugin.class); |
44 | 46 |
|
45 | | - final Optional<SpanDecorator> innerCompositeDecorator = StreamSupport.stream( |
46 | | - ((Iterable<SpanDecorator>) DECORATORS_FIELD.get(SPAN_DECORATOR_FIELD.get(plugin))).spliterator(), false |
47 | | - ).filter(decorator -> decorator instanceof CompositeSpanDecorator).findFirst(); |
| 47 | + final Optional<CompositeSpanDecorator> innerCompositeDecorator = getDecorator( |
| 48 | + (SpanDecorator) SPAN_DECORATOR_FIELD.get(plugin), |
| 49 | + CompositeSpanDecorator.class |
| 50 | + ); |
48 | 51 |
|
49 | 52 | assertThat(innerCompositeDecorator).isPresent(); |
50 | | - assertThat( |
51 | | - StreamSupport.stream( |
52 | | - ((Iterable<SpanDecorator>) DECORATORS_FIELD.get(innerCompositeDecorator.get())).spliterator(), false |
53 | | - ).anyMatch(decorator -> decorator instanceof RetrySpanDecorator) |
54 | | - ).withFailMessage(message).isEqualTo(isRetryEnabled); |
| 53 | + assertThat(getDecorator(innerCompositeDecorator.get(), RetrySpanDecorator.class).isPresent()) |
| 54 | + .withFailMessage(message) |
| 55 | + .isEqualTo(isRetryEnabled); |
55 | 56 | } |
56 | 57 |
|
57 | | - @Test |
58 | | - void shouldCreatePluginWithoutRetrySpanDecorator() throws IllegalAccessException { |
59 | | - final Plugin plugin = OpenTelemetryPluginFactory.create(createTestClient(false)); |
60 | 58 |
|
61 | | - assertNotNull(plugin); |
62 | | - assertInstanceOf(OpenTelemetryPlugin.class, plugin); |
| 59 | + @ParameterizedTest |
| 60 | + @CsvSource({ |
| 61 | + "true, FlowIdSpanDecorator should be added when client telemetry is enabled", |
| 62 | + "false, FlowIdSpanDecorator should be added when client telemetry is disabled" |
| 63 | + }) |
| 64 | + void shouldAlwaysAddFlowIdSpanDecorator( |
| 65 | + final boolean isFlowIdPropagationEnabled, |
| 66 | + final String message |
| 67 | + ) throws IllegalAccessException { |
| 68 | + final Plugin plugin = OpenTelemetryPluginFactory.create(createTestClient(false, isFlowIdPropagationEnabled)); |
63 | 69 |
|
64 | | - final Optional<SpanDecorator> innerCompositeDecorator = StreamSupport.stream( |
65 | | - ((Iterable<SpanDecorator>) DECORATORS_FIELD.get(SPAN_DECORATOR_FIELD.get(plugin))).spliterator(), false |
66 | | - ).filter(decorator -> decorator instanceof CompositeSpanDecorator).findFirst(); |
| 70 | + assertThat(plugin) |
| 71 | + .isNotNull() |
| 72 | + .isInstanceOf(OpenTelemetryPlugin.class); |
| 73 | + |
| 74 | + final Optional<CompositeSpanDecorator> innerCompositeDecorator = getDecorator( |
| 75 | + (SpanDecorator) SPAN_DECORATOR_FIELD.get(plugin), |
| 76 | + CompositeSpanDecorator.class |
| 77 | + ); |
67 | 78 |
|
68 | 79 | assertThat(innerCompositeDecorator).isPresent(); |
69 | | - assertTrue( |
70 | | - StreamSupport.stream( |
71 | | - ((Iterable<SpanDecorator>) DECORATORS_FIELD.get(innerCompositeDecorator.get())).spliterator(), false |
72 | | - ).noneMatch(decorator -> decorator instanceof RetrySpanDecorator), |
73 | | - "RetrySpanDecorator should not be added when retry is disabled" |
| 80 | + assertThat(getDecorator(innerCompositeDecorator.get(), FlowIdSpanDecorator.class)) |
| 81 | + .withFailMessage(message) |
| 82 | + .isPresent(); |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest |
| 86 | + @CsvSource({ |
| 87 | + "true, flow id propagation should be enabled", |
| 88 | + "false, flow id propagation should be enabled be disabled" |
| 89 | + }) |
| 90 | + void shouldAddFlowIdHeaderWhenPropagateFlowIdEnabled( |
| 91 | + final boolean isFlowIdPropagationEnabled, |
| 92 | + final String message |
| 93 | + ) throws IllegalAccessException { |
| 94 | + final Plugin plugin = OpenTelemetryPluginFactory.create(createTestClient(false, isFlowIdPropagationEnabled)); |
| 95 | + |
| 96 | + assertThat(plugin) |
| 97 | + .isNotNull() |
| 98 | + .isInstanceOf(OpenTelemetryPlugin.class); |
| 99 | + |
| 100 | + final Optional<CompositeSpanDecorator> innerCompositeDecorator = getDecorator( |
| 101 | + (SpanDecorator) SPAN_DECORATOR_FIELD.get(plugin), |
| 102 | + CompositeSpanDecorator.class |
74 | 103 | ); |
| 104 | + |
| 105 | + assertThat(innerCompositeDecorator).isPresent(); |
| 106 | + |
| 107 | + final Optional<FlowIdSpanDecorator> decoratorCandidate = getDecorator(innerCompositeDecorator.get(), FlowIdSpanDecorator.class); |
| 108 | + assertThat(decoratorCandidate).isNotEmpty(); |
| 109 | + |
| 110 | + final FlowIdSpanDecorator decorator = decoratorCandidate.get(); |
| 111 | + assertThat(PROPAGATE_FLOW_ID_FIELD.getBoolean(decorator)) |
| 112 | + .withFailMessage(message) |
| 113 | + .isEqualTo(isFlowIdPropagationEnabled); |
| 114 | + } |
| 115 | + |
| 116 | + private static <T extends SpanDecorator> Optional<T> getDecorator( |
| 117 | + final SpanDecorator decorator, |
| 118 | + final Class<T> decoratorType |
| 119 | + ) throws IllegalAccessException { |
| 120 | + return StreamSupport.stream(((Iterable<SpanDecorator>) DECORATORS_FIELD.get(decorator)).spliterator(), false) |
| 121 | + .filter(decoratorType::isInstance) |
| 122 | + .map(decoratorType::cast) |
| 123 | + .findFirst(); |
75 | 124 | } |
76 | 125 |
|
77 | | - private static RiptideProperties.Client createTestClient(final boolean retryEnabled) { |
| 126 | + private static RiptideProperties.Client createTestClient(final boolean retryEnabled, |
| 127 | + final boolean propagateFlowId) { |
78 | 128 | final RiptideProperties.Client client = new RiptideProperties.Client(); |
79 | 129 | final RiptideProperties.Telemetry telemetry = new RiptideProperties.Telemetry(); |
80 | 130 | telemetry.setAttributes(Map.of("service.name", "test-service")); |
| 131 | + telemetry.setPropagateFlowId(propagateFlowId); |
81 | 132 | client.setTelemetry(telemetry); |
82 | 133 |
|
83 | 134 | final RiptideProperties.Retry retry = new RiptideProperties.Retry(); |
|
0 commit comments