|
20 | 20 | import io.opentelemetry.api.GlobalOpenTelemetry; |
21 | 21 | import io.opentelemetry.api.common.Attributes; |
22 | 22 | import io.opentelemetry.api.trace.Span; |
| 23 | +import io.opentelemetry.api.trace.SpanContext; |
23 | 24 | import io.opentelemetry.api.trace.Tracer; |
24 | 25 | import io.opentelemetry.context.Context; |
25 | 26 | import io.opentelemetry.context.Scope; |
26 | 27 | import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
27 | 28 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
28 | 29 | import io.opentelemetry.sdk.common.InstrumentationScopeInfo; |
| 30 | +import io.opentelemetry.sdk.trace.data.LinkData; |
29 | 31 | import io.opentelemetry.sdk.trace.data.StatusData; |
30 | 32 | import java.io.PrintWriter; |
31 | 33 | import java.io.StringWriter; |
@@ -322,4 +324,95 @@ void testTracerBuilder() { |
322 | 324 | .hasInstrumentationScopeInfo( |
323 | 325 | InstrumentationScopeInfo.builder("test").setVersion("1.2.3").build()))); |
324 | 326 | } |
| 327 | + |
| 328 | + @Test |
| 329 | + @DisplayName("capture span link without attributes") |
| 330 | + void captureSpanLinkWithoutAttributes() { |
| 331 | + // When |
| 332 | + Tracer tracer = GlobalOpenTelemetry.getTracer("test"); |
| 333 | + Span linkedSpan = tracer.spanBuilder("linked").startSpan(); |
| 334 | + linkedSpan.end(); |
| 335 | + SpanContext linkedSpanContext = linkedSpan.getSpanContext(); |
| 336 | + |
| 337 | + Span testSpan = tracer.spanBuilder("test").addLink(linkedSpanContext).startSpan(); |
| 338 | + testSpan.end(); |
| 339 | + |
| 340 | + // Then |
| 341 | + testing.waitAndAssertTraces( |
| 342 | + trace -> |
| 343 | + trace.hasSpansSatisfyingExactly( |
| 344 | + span -> span.hasName("linked").hasNoParent().hasTotalAttributeCount(0)), |
| 345 | + trace -> |
| 346 | + trace.hasSpansSatisfyingExactly( |
| 347 | + span -> |
| 348 | + span.hasName("test") |
| 349 | + .hasNoParent() |
| 350 | + .hasTotalAttributeCount(0) |
| 351 | + .hasLinksSatisfying( |
| 352 | + links -> { |
| 353 | + assertThat(links).hasSize(1); |
| 354 | + LinkData link = links.get(0); |
| 355 | + assertThat(link.getSpanContext().getTraceId()) |
| 356 | + .isEqualTo(linkedSpanContext.getTraceId()); |
| 357 | + assertThat(link.getSpanContext().getSpanId()) |
| 358 | + .isEqualTo(linkedSpanContext.getSpanId()); |
| 359 | + assertThat(link.getSpanContext().getTraceFlags().asByte()) |
| 360 | + .isEqualTo(linkedSpanContext.getTraceFlags().asByte()); |
| 361 | + assertThat(link.getSpanContext().isRemote()) |
| 362 | + .isEqualTo(linkedSpanContext.isRemote()); |
| 363 | + assertThat(link.getAttributes().size()).isEqualTo(0); |
| 364 | + }))); |
| 365 | + } |
| 366 | + |
| 367 | + @Test |
| 368 | + @DisplayName("capture span link with attributes") |
| 369 | + void captureSpanLinkWithAttributes() { |
| 370 | + // When |
| 371 | + Tracer tracer = GlobalOpenTelemetry.getTracer("test"); |
| 372 | + Span linkedSpan = tracer.spanBuilder("linked").startSpan(); |
| 373 | + linkedSpan.end(); |
| 374 | + SpanContext linkedSpanContext = linkedSpan.getSpanContext(); |
| 375 | + |
| 376 | + Attributes linkAttributes = |
| 377 | + Attributes.builder() |
| 378 | + .put("string", "1") |
| 379 | + .put("long", 2L) |
| 380 | + .put("double", 3.0) |
| 381 | + .put("boolean", true) |
| 382 | + .build(); |
| 383 | + Span testSpan = |
| 384 | + tracer.spanBuilder("test").addLink(linkedSpanContext, linkAttributes).startSpan(); |
| 385 | + testSpan.end(); |
| 386 | + |
| 387 | + // Then |
| 388 | + testing.waitAndAssertTraces( |
| 389 | + trace -> |
| 390 | + trace.hasSpansSatisfyingExactly( |
| 391 | + span -> span.hasName("linked").hasNoParent().hasTotalAttributeCount(0)), |
| 392 | + trace -> |
| 393 | + trace.hasSpansSatisfyingExactly( |
| 394 | + span -> |
| 395 | + span.hasName("test") |
| 396 | + .hasNoParent() |
| 397 | + .hasTotalAttributeCount(0) |
| 398 | + .hasLinksSatisfying( |
| 399 | + links -> { |
| 400 | + assertThat(links).hasSize(1); |
| 401 | + LinkData link = links.get(0); |
| 402 | + assertThat(link.getSpanContext().getTraceId()) |
| 403 | + .isEqualTo(linkedSpanContext.getTraceId()); |
| 404 | + assertThat(link.getSpanContext().getSpanId()) |
| 405 | + .isEqualTo(linkedSpanContext.getSpanId()); |
| 406 | + assertThat(link.getSpanContext().getTraceFlags().asByte()) |
| 407 | + .isEqualTo(linkedSpanContext.getTraceFlags().asByte()); |
| 408 | + assertThat(link.getSpanContext().isRemote()) |
| 409 | + .isEqualTo(linkedSpanContext.isRemote()); |
| 410 | + assertThat(link.getTotalAttributeCount()).isEqualTo(4); |
| 411 | + Attributes attrs = link.getAttributes(); |
| 412 | + assertThat(attrs.get(stringKey("string"))).isEqualTo("1"); |
| 413 | + assertThat(attrs.get(longKey("long"))).isEqualTo(2L); |
| 414 | + assertThat(attrs.get(doubleKey("double"))).isEqualTo(3.0); |
| 415 | + assertThat(attrs.get(booleanKey("boolean"))).isEqualTo(true); |
| 416 | + }))); |
| 417 | + } |
325 | 418 | } |
0 commit comments