Skip to content

Commit ee611d9

Browse files
adinauerclaudegetsentry-bot
authored
feat(samples): [Global Attributes 6] Showcase scope attributes in all sample modules (#5149)
* feat(core): Add scope-level attributes API Add setAttribute, setAttributes, removeAttribute, and getAttributes to IScope/IScopes/Sentry so users can set attributes on the scope that are automatically included in logs and metrics events. Also refactor type inference logic into SentryAttributeType.inferFrom and add SentryLogEventAttributeValue.fromAttribute factory method, removing duplicate getType helpers from LoggerApi and MetricsApi. Co-Authored-By: Claude <noreply@anthropic.com> * changelog * ref: Split out LoggerApi/MetricsApi changes for stacked PR Move factory method extractions (SentryAttributeType.inferFrom, SentryLogEventAttributeValue.fromAttribute) and LoggerApi/MetricsApi scope attribute integration to a separate stacked PR. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(core): Wire scope attributes into LoggerApi and MetricsApi Extract factory methods SentryAttributeType.inferFrom and SentryLogEventAttributeValue.fromAttribute to reduce duplication. Apply scope attributes to log and metric events automatically. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * changelog * 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> * changelog * Revert "changelog" This reverts commit 7189bdc. * ref: Remove redundant comments from variant controllers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * ref: Limit scope attributes sample to base Spring Boot 4 variant Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: Detect integer attribute type correctly for all integer Number subtypes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * changelog * feat: Support collections and arrays in log attribute type inference Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * changelog * test: Add coverage for arrayAttribute factory method Add arrayAttribute and named array attribute usage to the four attribute tests in ScopesTest (log, count metric, distribution metric, gauge metric) to verify the factory method works end-to-end. Co-Authored-By: Claude <noreply@anthropic.com> * feat(samples): Showcase scope attributes in all sample modules Add Sentry.setAttribute() calls to all sample source files and corresponding attribute assertions to all system tests. This extends the scope attributes showcase from sentry-samples-spring-boot-4 to all 19 remaining sample modules with system tests. Co-Authored-By: Claude <noreply@anthropic.com> * Format code * ci: trigger CI re-run Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * ci: retrigger CI Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * remove duplicate changelog --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
1 parent b1045ed commit ee611d9

File tree

66 files changed

+369
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+369
-33
lines changed

sentry-samples/sentry-samples-console-opentelemetry-noagent/src/main/java/io/sentry/samples/console/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public static void main(String[] args) throws InterruptedException {
6363

6464
Sentry.addFeatureFlag("my-feature-flag", true);
6565

66+
Sentry.setAttribute("user.type", "admin");
67+
Sentry.setAttribute("feature.version", 2);
6668
captureMetrics();
6769

6870
// Sending exception:

sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ class ConsoleApplicationSystemTest {
111111
testHelper.ensureMetricsReceived { metricsEvents, sentryEnvelopeHeader ->
112112
testHelper.doesContainMetric(metricsEvents, "countMetric", "counter", 1.0) &&
113113
testHelper.doesContainMetric(metricsEvents, "gaugeMetric", "gauge", 5.0) &&
114-
testHelper.doesContainMetric(metricsEvents, "distributionMetric", "distribution", 7.0)
114+
testHelper.doesContainMetric(metricsEvents, "distributionMetric", "distribution", 7.0) &&
115+
testHelper.doesMetricHaveAttribute(metricsEvents, "countMetric", "user.type", "admin") &&
116+
testHelper.doesMetricHaveAttribute(metricsEvents, "countMetric", "feature.version", 2)
115117
}
116118
}
117119
}

sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public static void main(String[] args) throws InterruptedException {
128128

129129
Sentry.addFeatureFlag("my-feature-flag", true);
130130

131+
Sentry.setAttribute("user.type", "admin");
132+
Sentry.setAttribute("feature.version", 2);
131133
captureMetrics();
132134

133135
// Sending exception:

sentry-samples/sentry-samples-console/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ class ConsoleApplicationSystemTest {
106106
testHelper.ensureMetricsReceived { metricsEvents, sentryEnvelopeHeader ->
107107
testHelper.doesContainMetric(metricsEvents, "countMetric", "counter", 1.0) &&
108108
testHelper.doesContainMetric(metricsEvents, "gaugeMetric", "gauge", 5.0) &&
109-
testHelper.doesContainMetric(metricsEvents, "distributionMetric", "distribution", 7.0)
109+
testHelper.doesContainMetric(metricsEvents, "distributionMetric", "distribution", 7.0) &&
110+
testHelper.doesMetricHaveAttribute(metricsEvents, "countMetric", "user.type", "admin") &&
111+
testHelper.doesMetricHaveAttribute(metricsEvents, "countMetric", "feature.version", 2)
110112
}
111113
}
112114
}

sentry-samples/sentry-samples-jul/src/main/java/io/sentry/samples/jul/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public static void main(String[] args) throws Exception {
2323
MDC.put("userId", UUID.randomUUID().toString());
2424
MDC.put("requestId", UUID.randomUUID().toString());
2525

26+
Sentry.setAttribute("user.type", "admin");
27+
Sentry.setAttribute("feature.version", 2);
28+
Sentry.setAttribute("debug.enabled", true);
2629
Sentry.addFeatureFlag("my-feature-flag", true);
2730

2831
LOGGER.warning("important warning");

sentry-samples/sentry-samples-jul/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,20 @@ class ConsoleApplicationSystemTest {
6464

6565
testHelper.ensureLogsReceived { logs, _ ->
6666
testHelper.doesContainLogWithBody(logs, "User has made a purchase of product: 445") &&
67-
testHelper.doesContainLogWithBody(logs, "Something went wrong")
67+
testHelper.doesContainLogWithBody(logs, "Something went wrong") &&
68+
testHelper.doesLogWithBodyHaveAttribute(
69+
logs,
70+
"Something went wrong",
71+
"user.type",
72+
"admin",
73+
) &&
74+
testHelper.doesLogWithBodyHaveAttribute(
75+
logs,
76+
"Something went wrong",
77+
"feature.version",
78+
2,
79+
) &&
80+
testHelper.doesLogWithBodyHaveAttribute(logs, "Something went wrong", "debug.enabled", true)
6881
}
6982
}
7083
}

sentry-samples/sentry-samples-log4j2/src/main/java/io/sentry/samples/log4j2/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public static void main(String[] args) {
2020
// ThreadContext tag not listed in log4j2.xml
2121
ThreadContext.put("context-tag", "context-tag-value");
2222

23+
Sentry.setAttribute("user.type", "admin");
24+
Sentry.setAttribute("feature.version", 2);
25+
Sentry.setAttribute("debug.enabled", true);
2326
Sentry.addFeatureFlag("my-feature-flag", true);
2427

2528
// logging arguments are converted to Sentry Event parameters

sentry-samples/sentry-samples-log4j2/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,20 @@ class ConsoleApplicationSystemTest {
6666

6767
testHelper.ensureLogsReceived { logs, _ ->
6868
testHelper.doesContainLogWithBody(logs, "User has made a purchase of product: 445") &&
69-
testHelper.doesContainLogWithBody(logs, "Something went wrong")
69+
testHelper.doesContainLogWithBody(logs, "Something went wrong") &&
70+
testHelper.doesLogWithBodyHaveAttribute(
71+
logs,
72+
"Something went wrong",
73+
"user.type",
74+
"admin",
75+
) &&
76+
testHelper.doesLogWithBodyHaveAttribute(
77+
logs,
78+
"Something went wrong",
79+
"feature.version",
80+
2,
81+
) &&
82+
testHelper.doesLogWithBodyHaveAttribute(logs, "Something went wrong", "debug.enabled", true)
7083
}
7184
}
7285
}

sentry-samples/sentry-samples-logback/src/main/java/io/sentry/samples/logback/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static void main(String[] args) {
1818
// MDC tag not listed in logback.xml
1919
MDC.put("context-tag", "context-tag-value");
2020

21+
Sentry.setAttribute("user.type", "admin");
22+
Sentry.setAttribute("feature.version", 2);
23+
Sentry.setAttribute("debug.enabled", true);
2124
Sentry.addFeatureFlag("my-feature-flag", true);
2225
LOGGER.warn("important warning");
2326

sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,20 @@ class ConsoleApplicationSystemTest {
6666

6767
testHelper.ensureLogsReceived { logs, _ ->
6868
testHelper.doesContainLogWithBody(logs, "User has made a purchase of product: 445") &&
69-
testHelper.doesContainLogWithBody(logs, "Something went wrong")
69+
testHelper.doesContainLogWithBody(logs, "Something went wrong") &&
70+
testHelper.doesLogWithBodyHaveAttribute(
71+
logs,
72+
"Something went wrong",
73+
"user.type",
74+
"admin",
75+
) &&
76+
testHelper.doesLogWithBodyHaveAttribute(
77+
logs,
78+
"Something went wrong",
79+
"feature.version",
80+
2,
81+
) &&
82+
testHelper.doesLogWithBodyHaveAttribute(logs, "Something went wrong", "debug.enabled", true)
7083
}
7184
}
7285
}

0 commit comments

Comments
 (0)