Skip to content

Commit 5974800

Browse files
adinauerclaude
andcommitted
feat(samples): Showcase scope attributes in Spring Boot 4 samples
Add Sentry.setAttribute() calls to PersonController and MetricController across all Spring Boot 4 sample variants to demonstrate scope attributes being auto-attached to logs and metrics. Add e2e test assertions and TestHelper methods to verify scope attributes appear on captured log and metric events. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d535d44 commit 5974800

File tree

18 files changed

+162
-8
lines changed

18 files changed

+162
-8
lines changed

sentry-samples/sentry-samples-spring-boot-4-opentelemetry-noagent/src/main/java/io/sentry/samples/spring/boot4/MetricController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class MetricController {
1616

1717
@GetMapping("count")
1818
String count() {
19+
// Set scope attributes - these are automatically attached to metrics
20+
Sentry.setAttribute("user.type", "admin");
21+
Sentry.setAttribute("feature.version", 2);
1922
Sentry.metrics().count("countMetric");
2023
return "count metric increased";
2124
}

sentry-samples/sentry-samples-spring-boot-4-opentelemetry-noagent/src/main/java/io/sentry/samples/spring/boot4/PersonController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Person person(@PathVariable Long id) {
3434
Sentry.addFeatureFlag("outer-feature-flag", true);
3535
Span span = tracer.spanBuilder("spanCreatedThroughOtelApi").startSpan();
3636
try (final @NotNull Scope spanScope = span.makeCurrent()) {
37+
// Set scope attributes - these are automatically attached to logs and metrics
38+
Sentry.setAttribute("user.type", "admin");
39+
Sentry.setAttribute("feature.version", 2);
40+
Sentry.setAttribute("debug.enabled", true);
41+
3742
Sentry.logger().warn("warn Sentry logging");
3843
Sentry.logger().error("error Sentry logging");
3944
Sentry.logger().info("hello %s %s", "there", "world!");

sentry-samples/sentry-samples-spring-boot-4-opentelemetry-noagent/src/test/kotlin/io/sentry/systemtest/MetricsSystemTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class MetricsSystemTest {
2121
assertEquals(200, restClient.lastKnownStatusCode)
2222

2323
testHelper.ensureMetricsReceived { event, header ->
24-
testHelper.doesContainMetric(event, "countMetric", "counter", 1.0)
24+
testHelper.doesContainMetric(event, "countMetric", "counter", 1.0) &&
25+
testHelper.doesMetricHaveAttribute(event, "countMetric", "user.type", "admin") &&
26+
testHelper.doesMetricHaveAttribute(event, "countMetric", "feature.version", 2)
2527
}
2628
}
2729

sentry-samples/sentry-samples-spring-boot-4-opentelemetry-noagent/src/test/kotlin/io/sentry/systemtest/PersonSystemTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,20 @@ class PersonSystemTest {
5656
testHelper.ensureLogsReceived { logs, envelopeHeader ->
5757
testHelper.doesContainLogWithBody(logs, "warn Sentry logging") &&
5858
testHelper.doesContainLogWithBody(logs, "error Sentry logging") &&
59-
testHelper.doesContainLogWithBody(logs, "hello there world!")
59+
testHelper.doesContainLogWithBody(logs, "hello there world!") &&
60+
testHelper.doesLogWithBodyHaveAttribute(
61+
logs,
62+
"warn Sentry logging",
63+
"user.type",
64+
"admin",
65+
) &&
66+
testHelper.doesLogWithBodyHaveAttribute(
67+
logs,
68+
"warn Sentry logging",
69+
"feature.version",
70+
2,
71+
) &&
72+
testHelper.doesLogWithBodyHaveAttribute(logs, "warn Sentry logging", "debug.enabled", true)
6073
}
6174
}
6275

sentry-samples/sentry-samples-spring-boot-4-opentelemetry/src/main/java/io/sentry/samples/spring/boot4/MetricController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class MetricController {
1616

1717
@GetMapping("count")
1818
String count() {
19+
// Set scope attributes - these are automatically attached to metrics
20+
Sentry.setAttribute("user.type", "admin");
21+
Sentry.setAttribute("feature.version", 2);
1922
Sentry.metrics().count("countMetric");
2023
return "count metric increased";
2124
}

sentry-samples/sentry-samples-spring-boot-4-opentelemetry/src/main/java/io/sentry/samples/spring/boot4/PersonController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Person person(@PathVariable Long id) {
3232
Sentry.addFeatureFlag("transaction-feature-flag", true);
3333
Span span = tracer.spanBuilder("spanCreatedThroughOtelApi").startSpan();
3434
try (final @NotNull Scope spanScope = span.makeCurrent()) {
35+
// Set scope attributes - these are automatically attached to logs and metrics
36+
Sentry.setAttribute("user.type", "admin");
37+
Sentry.setAttribute("feature.version", 2);
38+
Sentry.setAttribute("debug.enabled", true);
39+
3540
Sentry.logger().warn("warn Sentry logging");
3641
Sentry.logger().error("error Sentry logging");
3742
Sentry.logger().info("hello %s %s", "there", "world!");

sentry-samples/sentry-samples-spring-boot-4-opentelemetry/src/test/kotlin/io/sentry/systemtest/MetricsSystemTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class MetricsSystemTest {
2121
assertEquals(200, restClient.lastKnownStatusCode)
2222

2323
testHelper.ensureMetricsReceived { event, header ->
24-
testHelper.doesContainMetric(event, "countMetric", "counter", 1.0)
24+
testHelper.doesContainMetric(event, "countMetric", "counter", 1.0) &&
25+
testHelper.doesMetricHaveAttribute(event, "countMetric", "user.type", "admin") &&
26+
testHelper.doesMetricHaveAttribute(event, "countMetric", "feature.version", 2)
2527
}
2628
}
2729

sentry-samples/sentry-samples-spring-boot-4-opentelemetry/src/test/kotlin/io/sentry/systemtest/PersonSystemTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@ class PersonSystemTest {
5151
testHelper.ensureLogsReceived { logs, envelopeHeader ->
5252
testHelper.doesContainLogWithBody(logs, "warn Sentry logging") &&
5353
testHelper.doesContainLogWithBody(logs, "error Sentry logging") &&
54-
testHelper.doesContainLogWithBody(logs, "hello there world!")
54+
testHelper.doesContainLogWithBody(logs, "hello there world!") &&
55+
testHelper.doesLogWithBodyHaveAttribute(
56+
logs,
57+
"warn Sentry logging",
58+
"user.type",
59+
"admin",
60+
) &&
61+
testHelper.doesLogWithBodyHaveAttribute(
62+
logs,
63+
"warn Sentry logging",
64+
"feature.version",
65+
2,
66+
) &&
67+
testHelper.doesLogWithBodyHaveAttribute(logs, "warn Sentry logging", "debug.enabled", true)
5568
}
5669
}
5770

sentry-samples/sentry-samples-spring-boot-4-webflux/src/main/java/io/sentry/samples/spring/boot4/MetricController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class MetricController {
1616

1717
@GetMapping("count")
1818
String count() {
19+
// Set scope attributes - these are automatically attached to metrics
20+
Sentry.setAttribute("user.type", "admin");
21+
Sentry.setAttribute("feature.version", 2);
1922
Sentry.metrics().count("countMetric");
2023
return "count metric increased";
2124
}

sentry-samples/sentry-samples-spring-boot-4-webflux/src/main/java/io/sentry/samples/spring/boot4/PersonController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public PersonController(PersonService personService) {
2323

2424
@GetMapping("{id}")
2525
Person person(@PathVariable Long id) {
26+
// Set scope attributes - these are automatically attached to logs and metrics
27+
Sentry.setAttribute("user.type", "admin");
28+
Sentry.setAttribute("feature.version", 2);
29+
Sentry.setAttribute("debug.enabled", true);
30+
2631
Sentry.logger().warn("warn Sentry logging");
2732
Sentry.logger().error("error Sentry logging");
2833
Sentry.logger().info("hello %s %s", "there", "world!");

0 commit comments

Comments
 (0)