|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2025. Ivan Vakhrushev and others. |
| 3 | + * https://github.com/mfvanek/spring-boot-open-telemetry-demo |
| 4 | + * |
| 5 | + * Licensed under the Apache License 2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +package io.github.mfvanek.spring.boot3.test; |
| 9 | + |
| 10 | +import io.github.mfvanek.spring.boot3.test.support.TestBase; |
| 11 | +import io.micrometer.tracing.CurrentTraceContext; |
| 12 | +import io.micrometer.tracing.ScopedSpan; |
| 13 | +import io.micrometer.tracing.Span; |
| 14 | +import io.micrometer.tracing.TraceContext; |
| 15 | +import io.micrometer.tracing.Tracer; |
| 16 | +import org.junit.jupiter.api.DisplayName; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | + |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +@SuppressWarnings({"PMD.AvoidCatchingThrowable", "PMD.ExceptionAsFlowControl", "checkstyle:NestedTryDepth", "checkstyle:IllegalCatch"}) |
| 25 | +class NewTraceIdTest extends TestBase { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private Tracer tracer; |
| 29 | + |
| 30 | + @Test |
| 31 | + @DisplayName("Demonstration of how to create a new traceId using the span API") |
| 32 | + void canCreateNewTraceIdViaSpan() { |
| 33 | + final ScopedSpan firstSpan = tracer.startScopedSpan("first"); |
| 34 | + try { |
| 35 | + final String previousTraceId = Objects.requireNonNull(tracer.currentSpan()).context().traceId(); |
| 36 | + assertThat(previousTraceId) |
| 37 | + .isEqualTo(firstSpan.context().traceId()); |
| 38 | + |
| 39 | + try (Tracer.SpanInScope ignored = tracer.withSpan(null)) { |
| 40 | + final Span newSpan = tracer.nextSpan().name("new"); |
| 41 | + try (Tracer.SpanInScope ignored2 = tracer.withSpan(newSpan.start())) { |
| 42 | + final String newTraceId = Objects.requireNonNull(tracer.currentSpan()).context().traceId(); |
| 43 | + assertThat(newTraceId) |
| 44 | + .isNotEqualTo(previousTraceId); |
| 45 | + } catch (Throwable e) { |
| 46 | + newSpan.error(e); |
| 47 | + throw e; |
| 48 | + } finally { |
| 49 | + newSpan.end(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + final String lastTraceId = Objects.requireNonNull(tracer.currentSpan()).context().traceId(); |
| 54 | + assertThat(lastTraceId) |
| 55 | + .isEqualTo(previousTraceId); |
| 56 | + } catch (Throwable e) { |
| 57 | + firstSpan.error(e); |
| 58 | + throw e; |
| 59 | + } finally { |
| 60 | + firstSpan.end(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("Demonstration of creating a new traceId using the API traceContext") |
| 66 | + void canCreateNewTraceIdViaContext() { |
| 67 | + final ScopedSpan firstSpan = tracer.startScopedSpan("first"); |
| 68 | + try { |
| 69 | + final String previousTraceId = Objects.requireNonNull(tracer.currentSpan()).context().traceId(); |
| 70 | + assertThat(previousTraceId) |
| 71 | + .isEqualTo(firstSpan.context().traceId()); |
| 72 | + |
| 73 | + try (Tracer.SpanInScope ignored = tracer.withSpan(null)) { |
| 74 | + final Span newSpan = tracer.nextSpan(); |
| 75 | + final TraceContext traceContext = tracer.traceContextBuilder() |
| 76 | + .traceId(newSpan.context().traceId()) |
| 77 | + .spanId(newSpan.context().spanId()) |
| 78 | + .sampled(Boolean.TRUE) // Important! |
| 79 | + .build(); |
| 80 | + |
| 81 | + try (CurrentTraceContext.Scope unused = tracer.currentTraceContext().newScope(traceContext)) { |
| 82 | + final String newTraceId = Objects.requireNonNull(tracer.currentSpan()).context().traceId(); |
| 83 | + assertThat(newTraceId) |
| 84 | + .isNotEqualTo(previousTraceId); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + final String lastTraceId = Objects.requireNonNull(tracer.currentSpan()).context().traceId(); |
| 89 | + assertThat(lastTraceId) |
| 90 | + .isEqualTo(previousTraceId); |
| 91 | + } catch (Throwable e) { |
| 92 | + firstSpan.error(e); |
| 93 | + throw e; |
| 94 | + } finally { |
| 95 | + firstSpan.end(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments