|
21 | 21 |
|
22 | 22 | import io.opentelemetry.api.trace.SpanKind; |
23 | 23 | import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; |
| 24 | +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; |
24 | 25 | import io.opentelemetry.sdk.trace.data.SpanData; |
25 | 26 | import io.opentelemetry.sdk.trace.data.StatusData; |
26 | 27 | import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes; |
| 28 | +import java.util.ArrayList; |
27 | 29 | import java.util.Arrays; |
28 | 30 | import java.util.HashMap; |
29 | 31 | import java.util.List; |
30 | 32 | import java.util.Map; |
31 | 33 | import java.util.concurrent.atomic.AtomicReference; |
| 34 | +import java.util.function.Consumer; |
32 | 35 | import org.assertj.core.api.AbstractLongAssert; |
33 | 36 | import org.assertj.core.api.AbstractStringAssert; |
34 | 37 | import org.junit.jupiter.api.Test; |
@@ -127,51 +130,74 @@ void shouldHandleFailureInKafkaListener() { |
127 | 130 |
|
128 | 131 | testing() |
129 | 132 | .waitAndAssertTraces( |
130 | | - trace -> |
131 | | - trace.hasSpansSatisfyingExactly( |
132 | | - span -> span.hasName("producer"), |
133 | | - span -> |
134 | | - span.hasName("testSingleTopic publish") |
135 | | - .hasKind(SpanKind.PRODUCER) |
136 | | - .hasParent(trace.getSpan(0)) |
137 | | - .hasAttributesSatisfyingExactly( |
138 | | - equalTo(MESSAGING_SYSTEM, "kafka"), |
139 | | - equalTo(MESSAGING_DESTINATION_NAME, "testSingleTopic"), |
140 | | - equalTo(MESSAGING_OPERATION, "publish"), |
141 | | - satisfies( |
142 | | - MESSAGING_CLIENT_ID, |
143 | | - stringAssert -> stringAssert.startsWith("producer")), |
144 | | - satisfies( |
145 | | - MessagingIncubatingAttributes |
146 | | - .MESSAGING_DESTINATION_PARTITION_ID, |
147 | | - AbstractStringAssert::isNotEmpty), |
148 | | - satisfies( |
149 | | - MESSAGING_KAFKA_MESSAGE_OFFSET, |
150 | | - AbstractLongAssert::isNotNegative), |
151 | | - equalTo(MESSAGING_KAFKA_MESSAGE_KEY, "10")), |
152 | | - span -> |
153 | | - span.hasName("testSingleTopic process") |
154 | | - .hasKind(SpanKind.CONSUMER) |
155 | | - .hasParent(trace.getSpan(1)) |
156 | | - .hasStatus(StatusData.error()) |
157 | | - .hasException(new IllegalArgumentException("boom")) |
158 | | - .hasAttributesSatisfyingExactly(processAttributes), |
159 | | - span -> span.hasName("consumer").hasParent(trace.getSpan(2)), |
160 | | - span -> |
161 | | - span.hasName("testSingleTopic process") |
162 | | - .hasKind(SpanKind.CONSUMER) |
163 | | - .hasParent(trace.getSpan(1)) |
164 | | - .hasStatus(StatusData.error()) |
165 | | - .hasException(new IllegalArgumentException("boom")) |
166 | | - .hasAttributesSatisfyingExactly(processAttributes), |
167 | | - span -> span.hasName("consumer").hasParent(trace.getSpan(4)), |
168 | | - span -> |
169 | | - span.hasName("testSingleTopic process") |
170 | | - .hasKind(SpanKind.CONSUMER) |
171 | | - .hasParent(trace.getSpan(1)) |
172 | | - .hasStatus(StatusData.unset()) |
173 | | - .hasAttributesSatisfyingExactly(processAttributes), |
174 | | - span -> span.hasName("consumer").hasParent(trace.getSpan(6)))); |
| 133 | + trace -> { |
| 134 | + List<Consumer<SpanDataAssert>> assertions = |
| 135 | + new ArrayList<>( |
| 136 | + Arrays.asList( |
| 137 | + span -> span.hasName("producer"), |
| 138 | + span -> |
| 139 | + span.hasName("testSingleTopic publish") |
| 140 | + .hasKind(SpanKind.PRODUCER) |
| 141 | + .hasParent(trace.getSpan(0)) |
| 142 | + .hasAttributesSatisfyingExactly( |
| 143 | + equalTo(MESSAGING_SYSTEM, "kafka"), |
| 144 | + equalTo(MESSAGING_DESTINATION_NAME, "testSingleTopic"), |
| 145 | + equalTo(MESSAGING_OPERATION, "publish"), |
| 146 | + satisfies( |
| 147 | + MESSAGING_CLIENT_ID, |
| 148 | + stringAssert -> stringAssert.startsWith("producer")), |
| 149 | + satisfies( |
| 150 | + MessagingIncubatingAttributes |
| 151 | + .MESSAGING_DESTINATION_PARTITION_ID, |
| 152 | + AbstractStringAssert::isNotEmpty), |
| 153 | + satisfies( |
| 154 | + MESSAGING_KAFKA_MESSAGE_OFFSET, |
| 155 | + AbstractLongAssert::isNotNegative), |
| 156 | + equalTo(MESSAGING_KAFKA_MESSAGE_KEY, "10")), |
| 157 | + span -> |
| 158 | + span.hasName("testSingleTopic process") |
| 159 | + .hasKind(SpanKind.CONSUMER) |
| 160 | + .hasParent(trace.getSpan(1)) |
| 161 | + .hasStatus(StatusData.error()) |
| 162 | + .hasException(new IllegalArgumentException("boom")) |
| 163 | + .hasAttributesSatisfyingExactly(processAttributes), |
| 164 | + span -> span.hasName("consumer").hasParent(trace.getSpan(2)))); |
| 165 | + if (Boolean.getBoolean("testLatestDeps")) { |
| 166 | + assertions.add( |
| 167 | + span -> span.hasName("handle exception").hasParent(trace.getSpan(2))); |
| 168 | + } |
| 169 | + assertions.addAll( |
| 170 | + Arrays.asList( |
| 171 | + span -> |
| 172 | + span.hasName("testSingleTopic process") |
| 173 | + .hasKind(SpanKind.CONSUMER) |
| 174 | + .hasParent(trace.getSpan(1)) |
| 175 | + .hasStatus(StatusData.error()) |
| 176 | + .hasException(new IllegalArgumentException("boom")) |
| 177 | + .hasAttributesSatisfyingExactly(processAttributes), |
| 178 | + span -> |
| 179 | + span.hasName("consumer") |
| 180 | + .hasParent( |
| 181 | + trace.getSpan(Boolean.getBoolean("testLatestDeps") ? 5 : 4)))); |
| 182 | + if (Boolean.getBoolean("testLatestDeps")) { |
| 183 | + assertions.add( |
| 184 | + span -> span.hasName("handle exception").hasParent(trace.getSpan(5))); |
| 185 | + } |
| 186 | + assertions.addAll( |
| 187 | + Arrays.asList( |
| 188 | + span -> |
| 189 | + span.hasName("testSingleTopic process") |
| 190 | + .hasKind(SpanKind.CONSUMER) |
| 191 | + .hasParent(trace.getSpan(1)) |
| 192 | + .hasStatus(StatusData.unset()) |
| 193 | + .hasAttributesSatisfyingExactly(processAttributes), |
| 194 | + span -> |
| 195 | + span.hasName("consumer") |
| 196 | + .hasParent( |
| 197 | + trace.getSpan(Boolean.getBoolean("testLatestDeps") ? 8 : 6)))); |
| 198 | + |
| 199 | + trace.hasSpansSatisfyingExactly(assertions); |
| 200 | + }); |
175 | 201 | } |
176 | 202 |
|
177 | 203 | @Test |
|
0 commit comments