Skip to content

Commit 2083f7f

Browse files
authored
New trace id creation examples (#294)
* Kotlin example * Java example * Fix comments
1 parent 29efd28 commit 2083f7f

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed

buildSrc/src/main/kotlin/sb-ot-demo.java-conventions.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ tasks {
7979
withType<JavaCompile>().configureEach {
8080
options.errorprone {
8181
disableWarningsInGeneratedCode.set(true)
82-
disable("Slf4jLoggerShouldBeNonStatic")
82+
disable("Slf4jLoggerShouldBeNonStatic", "BooleanLiteral")
8383
}
8484
}
8585

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.github.mfvanek.spring.boot3.kotlin.test
2+
3+
import io.github.mfvanek.spring.boot3.kotlin.test.support.TestBase
4+
import io.micrometer.tracing.Tracer
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.junit.jupiter.api.DisplayName
7+
import org.junit.jupiter.api.Test
8+
import org.springframework.beans.factory.annotation.Autowired
9+
10+
internal class NewTraceIdTest : TestBase() {
11+
12+
@Autowired
13+
private lateinit var tracer: Tracer
14+
15+
@Suppress("NestedBlockDepth")
16+
@DisplayName("Demonstration of how to create a new traceId using the span API")
17+
@Test
18+
fun canCreateNewTraceIdViaSpan() {
19+
val firstSpan = tracer.startScopedSpan("first")
20+
try {
21+
val previousTraceId = tracer.currentSpan()?.context()?.traceId()
22+
assertThat(previousTraceId)
23+
.isEqualTo(firstSpan.context().traceId())
24+
25+
tracer.withSpan(null).use {
26+
val newSpan = tracer.nextSpan().name("new")
27+
try {
28+
tracer.withSpan(newSpan.start()).use {
29+
val newTraceId = tracer.currentSpan()?.context()?.traceId()
30+
assertThat(newTraceId)
31+
.isNotEqualTo(previousTraceId)
32+
}
33+
} catch (e: Exception) {
34+
newSpan.error(e)
35+
throw e
36+
} finally {
37+
newSpan.end()
38+
}
39+
}
40+
41+
val lastTraceId = tracer.currentSpan()?.context()?.traceId()
42+
assertThat(lastTraceId)
43+
.isEqualTo(previousTraceId)
44+
} catch (e: Exception) {
45+
firstSpan.error(e)
46+
throw e
47+
} finally {
48+
firstSpan.end()
49+
}
50+
}
51+
52+
@DisplayName("Demonstration of creating a new traceId using the API traceContext")
53+
@Test
54+
@Suppress("NestedBlockDepth")
55+
fun canCreateNewTraceIdViaContext() {
56+
val firstSpan = tracer.startScopedSpan("first")
57+
try {
58+
val previousTraceId = tracer.currentSpan()?.context()?.traceId()
59+
assertThat(previousTraceId)
60+
.isEqualTo(firstSpan.context().traceId())
61+
62+
tracer.withSpan(null).use {
63+
val newSpan = tracer.nextSpan()
64+
val traceContext = tracer.traceContextBuilder()
65+
.traceId(newSpan.context().traceId())
66+
.spanId(newSpan.context().spanId())
67+
.sampled(true) // Important!
68+
.build()
69+
tracer.currentTraceContext().newScope(
70+
traceContext
71+
).use { // Scope from newScope must be closed
72+
val newTraceId = tracer.currentSpan()?.context()?.traceId()
73+
assertThat(newTraceId)
74+
.isNotEqualTo(previousTraceId)
75+
}
76+
}
77+
78+
val lastTraceId = tracer.currentSpan()?.context()?.traceId()
79+
assertThat(lastTraceId)
80+
.isEqualTo(previousTraceId)
81+
} catch (e: Exception) {
82+
firstSpan.error(e)
83+
throw e
84+
} finally {
85+
firstSpan.end()
86+
}
87+
}
88+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)