Skip to content

Commit 6bca264

Browse files
author
m.zharinova
committed
Merge branch 'issue-146' of https://github.com/mfvanek/spring-boot-open-telemetry-demo into issue-146
2 parents bacffa9 + 80ce50e commit 6bca264

File tree

8 files changed

+196
-12
lines changed

8 files changed

+196
-12
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

common-internal-bom/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ javaPlatform {
1818
dependencies {
1919
api(platform("org.assertj:assertj-bom:3.27.3"))
2020
api(platform("org.testcontainers:testcontainers-bom:1.21.3"))
21-
api(platform("org.junit:junit-bom:5.13.3"))
21+
api(platform("org.junit:junit-bom:5.13.4"))
2222
api(platform("io.github.mfvanek:pg-index-health-bom:0.20.2"))
2323

2424
constraints {
2525
api("org.liquibase:liquibase-core:4.33.0")
2626
api("com.github.blagerweij:liquibase-sessionlock:1.6.9")
2727
api("org.awaitility:awaitility:4.3.0")
28-
api("com.zaxxer:HikariCP:6.3.0")
28+
api("com.zaxxer:HikariCP:7.0.0")
2929
api("org.postgresql:postgresql:42.7.7")
3030
}
3131
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
detekt = "1.23.8"
3-
spring-boot-v3 = "3.5.3"
3+
spring-boot-v3 = "3.5.4"
44

55
[libraries]
66
detekt = { group = "io.gitlab.arturbosch.detekt", name = "detekt-gradle-plugin", version.ref = "detekt" }

spring-boot-3-demo-app-kotlin/src/test/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/ApplicationTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import org.junit.jupiter.api.DisplayName
1414
import org.junit.jupiter.api.Test
1515
import org.springframework.beans.factory.annotation.Autowired
1616
import org.springframework.context.ApplicationContext
17-
import org.springframework.dao.DataAccessResourceFailureException
18-
import java.util.Locale
17+
import org.springframework.dao.QueryTimeoutException
18+
import java.util.*
1919
import java.util.function.Consumer
2020

2121
class ApplicationTests : TestBase() {
@@ -70,7 +70,7 @@ class ApplicationTests : TestBase() {
7070
@DisplayName("Throws exception when query exceeds timeout")
7171
fun exceptionWithLongQuery() {
7272
assertThatThrownBy { jdbcTemplate.execute("select pg_sleep(1.1);") }
73-
.isInstanceOf(DataAccessResourceFailureException::class.java)
73+
.isInstanceOf(QueryTimeoutException::class.java)
7474
.hasMessageContaining("ERROR: canceling statement due to user request")
7575
}
7676

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+
}

spring-boot-3-demo-app/build.gradle.kts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ dependencies {
2727
implementation("org.springframework.boot:spring-boot-starter-jdbc")
2828
implementation("org.postgresql:postgresql")
2929
implementation("com.zaxxer:HikariCP")
30-
implementation(project(":db-migrations")) {
31-
exclude(group = "io.gitlab.arturbosch.detekt")
32-
}
30+
implementation(project(":db-migrations"))
3331
implementation("org.liquibase:liquibase-core")
3432
implementation("com.github.blagerweij:liquibase-sessionlock")
3533
implementation(libs.datasource.micrometer)

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/ApplicationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.junit.jupiter.api.Test;
1919
import org.springframework.beans.factory.annotation.Autowired;
2020
import org.springframework.context.ApplicationContext;
21-
import org.springframework.dao.DataAccessResourceFailureException;
21+
import org.springframework.dao.QueryTimeoutException;
2222

2323
import java.util.Locale;
2424

@@ -66,7 +66,7 @@ void jdbcQueryTimeoutFromProperties() {
6666
@DisplayName("Throws exception when query exceeds timeout")
6767
void exceptionWithLongQuery() {
6868
assertThatThrownBy(() -> jdbcTemplate.execute("select pg_sleep(1.1);"))
69-
.isInstanceOf(DataAccessResourceFailureException.class)
69+
.isInstanceOf(QueryTimeoutException.class)
7070
.hasMessageContaining("ERROR: canceling statement due to user request");
7171
}
7272

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)