You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(java): Add scope attributes docs for logs and metrics (#16639)
Add scope attributes sections to Java and Android logs/metrics usage
docs, showing how to set attributes on the scope that are automatically
included in all logs/metrics.
Covers `setAttribute` (with auto type inference), `SentryAttribute`
factory methods (for explicit typing), `setAttributes` (for multiple at
once), and `removeAttribute`. Examples are consistent across all four
platform-includes files (logs/metrics x java/android).
Also adds scope attributes references in the default-attributes docs for
Java logs and Android/Java metrics.
Co-Authored-By: Claude <noreply@anthropic.com>
PR Stack in SDK repo:
getsentry/sentry-java#5118 (comment)
Co-authored-by: Claude <noreply@anthropic.com>
Any attributes set on the current scope via `Sentry.setAttribute()` or `Sentry.setAttributes()` are automatically included on all log entries. See [Usage](#usage) above for details.
Copy file name to clipboardExpand all lines: platform-includes/logs/usage/android.mdx
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,3 +65,59 @@ Sentry.logger().log(
65
65
"param1"
66
66
)
67
67
```
68
+
69
+
### Scope Attributes
70
+
71
+
You can set attributes on the scope that will be automatically included in all log entries captured within that scope. This is useful for attaching contextual information like request IDs or user properties that should appear on every log.
72
+
73
+
```java {tabTitle: Java}
74
+
importio.sentry.Sentry;
75
+
importio.sentry.SentryAttribute;
76
+
importio.sentry.SentryAttributes;
77
+
78
+
// Set a single attribute with automatic type inference
79
+
Sentry.setAttribute("request.id", "abc-123");
80
+
81
+
// Or use a factory method to set the type explicitly
// All subsequent logs will include these attributes
115
+
Sentry.logger().info("Processing request")
116
+
117
+
// Remove an attribute when it's no longer relevant
118
+
Sentry.removeAttribute("request.id")
119
+
```
120
+
121
+
Attribute types are inferred automatically from the value: `String` maps to `string`, `Boolean` to `boolean`, integer types (`Integer`, `Long`, `Short`, `Byte`, `BigInteger`, `AtomicInteger`, `AtomicLong`) to `integer`, floating-point types (`Float`, `Double`, `BigDecimal`) to `double`, and `Collection` or array types to `array`. You can also use typed factory methods like `SentryAttribute.stringAttribute()` to set the type explicitly.
122
+
123
+
Attributes passed directly to a log call override scope attributes with the same key.
Copy file name to clipboardExpand all lines: platform-includes/logs/usage/java.mdx
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,6 +66,74 @@ Sentry.logger().log(
66
66
)
67
67
```
68
68
69
+
### Scope Attributes
70
+
71
+
You can set attributes on the scope that will be automatically included in all log entries captured within that scope. This is useful for attaching contextual information like request IDs or user properties that should appear on every log.
72
+
73
+
```java {tabTitle: Java}
74
+
importio.sentry.Sentry;
75
+
importio.sentry.ScopeType;
76
+
importio.sentry.SentryAttribute;
77
+
importio.sentry.SentryAttributes;
78
+
79
+
// Set an attribute on the global scope so it applies to all logs
// All subsequent logs will include these attributes
127
+
Sentry.logger().info("Processing request")
128
+
129
+
// Remove an attribute when it's no longer relevant
130
+
Sentry.removeAttribute("request.id")
131
+
```
132
+
133
+
Attribute types are inferred automatically from the value: `String` maps to `string`, `Boolean` to `boolean`, integer types (`Integer`, `Long`, `Short`, `Byte`, `BigInteger`, `AtomicInteger`, `AtomicLong`) to `integer`, floating-point types (`Float`, `Double`, `BigDecimal`) to `double`, and `Collection` or array types to `array`. You can also use typed factory methods like `SentryAttribute.stringAttribute()` to set the type explicitly.
134
+
135
+
Attributes passed directly to a log call override scope attributes with the same key.
136
+
69
137
<PlatformSectionsupported={["java.spring-boot"]}>
70
138
It is also possible to use Logback and Spring Boot together to have logs going through Logback sent to Sentry. Take a look at the <PlatformLinkto="/logging-frameworks/">Logging Framework Integrations</PlatformLink> page.
Copy file name to clipboardExpand all lines: platform-includes/metrics/default-attributes/android.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,3 +18,7 @@ If user information is available in the current scope, the following attributes
18
18
If replay information is available in the current scope, the following attributes are added to the log:
19
19
20
20
-`sentry.replay_id`: The ID of the replay.
21
+
22
+
### Scope Attributes
23
+
24
+
Any attributes set on the current scope via `Sentry.setAttribute()` or `Sentry.setAttributes()` are automatically included on all metrics. See [Usage](#usage) above for details.
Copy file name to clipboardExpand all lines: platform-includes/metrics/default-attributes/java.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,3 +18,7 @@ If user information is available in the current scope, the following attributes
18
18
The SDK will attach the following:
19
19
20
20
-`server.address`: The address of the server that sent the metric. Equivalent to `server_name` that gets attached to Sentry errors.
21
+
22
+
### Scope Attributes
23
+
24
+
Any attributes set on the current scope via `Sentry.setAttribute()` or `Sentry.setAttributes()` are automatically included on all metrics. See [Usage](#usage) above for details.
You can set attributes on the scope that will be automatically included in all metrics captured within that scope. This is useful for attaching contextual information that should appear on every metric.
114
+
115
+
```java {tabTitle: Java}
116
+
importio.sentry.Sentry;
117
+
importio.sentry.SentryAttribute;
118
+
importio.sentry.SentryAttributes;
119
+
120
+
// Set a single attribute with automatic type inference
121
+
Sentry.setAttribute("request.id", "abc-123");
122
+
123
+
// Or use a factory method to set the type explicitly
// All subsequent metrics will include these attributes
157
+
Sentry.metrics().count("order_placed", 1.0)
158
+
159
+
// Remove an attribute when it's no longer relevant
160
+
Sentry.removeAttribute("canary")
161
+
```
162
+
163
+
Attributes passed directly to a metric call override scope attributes with the same key.
164
+
111
165
### Specifying Units
112
166
113
167
For `gauge` and `distribution` metrics, you can specify a unit using the `unit` option. This helps Sentry display the metric value in a human-readable format. `MetricsUnit` offers constants for units supported by Sentry. Sending in custom units is not supported.
You can set attributes on the scope that will be automatically included in all metrics captured within that scope. This is useful for attaching contextual information that should appear on every metric.
114
+
115
+
```java {tabTitle: Java}
116
+
importio.sentry.Sentry;
117
+
importio.sentry.ScopeType;
118
+
importio.sentry.SentryAttribute;
119
+
importio.sentry.SentryAttributes;
120
+
121
+
// Set an attribute on the global scope so it applies to all metrics
// All subsequent metrics will include these attributes
169
+
Sentry.metrics().count("order_placed", 1.0)
170
+
171
+
// Remove an attribute when it's no longer relevant
172
+
Sentry.removeAttribute("canary")
173
+
```
174
+
175
+
Attributes passed directly to a metric call override scope attributes with the same key.
176
+
111
177
### Specifying Units
112
178
113
179
For `gauge` and `distribution` metrics, you can specify a unit using the `unit` option. This helps Sentry display the metric value in a human-readable format. `MetricsUnit` offers constants for units supported by Sentry. Sending in custom units is not supported.
0 commit comments