diff --git a/platform-includes/logs/default-attributes/java.mdx b/platform-includes/logs/default-attributes/java.mdx index d365d61d50f12..615bd5d148e6b 100644 --- a/platform-includes/logs/default-attributes/java.mdx +++ b/platform-includes/logs/default-attributes/java.mdx @@ -11,3 +11,7 @@ The Java SDK automatically sets several default attributes on all log entries to + +### Scope Attributes + +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. diff --git a/platform-includes/logs/usage/android.mdx b/platform-includes/logs/usage/android.mdx index 7f8e73911203c..3991f3a7af959 100644 --- a/platform-includes/logs/usage/android.mdx +++ b/platform-includes/logs/usage/android.mdx @@ -65,3 +65,59 @@ Sentry.logger().log( "param1" ) ``` + +### Scope Attributes + +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. + +```java {tabTitle: Java} +import io.sentry.Sentry; +import io.sentry.SentryAttribute; +import io.sentry.SentryAttributes; + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123"); + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)); + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("tenant", "acme-corp"), + SentryAttribute.booleanAttribute("is_admin", true) +)); + +// All subsequent logs will include these attributes +Sentry.logger().info("Processing request"); + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("request.id"); +``` + +```kotlin {tabTitle: Kotlin} +import io.sentry.Sentry +import io.sentry.SentryAttribute +import io.sentry.SentryAttributes + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123") + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)) + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("tenant", "acme-corp"), + SentryAttribute.booleanAttribute("is_admin", true) +)) + +// All subsequent logs will include these attributes +Sentry.logger().info("Processing request") + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("request.id") +``` + +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. + +Attributes passed directly to a log call override scope attributes with the same key. diff --git a/platform-includes/logs/usage/java.mdx b/platform-includes/logs/usage/java.mdx index 11fd1fbf362d4..a0233f9b5deb6 100644 --- a/platform-includes/logs/usage/java.mdx +++ b/platform-includes/logs/usage/java.mdx @@ -66,6 +66,74 @@ Sentry.logger().log( ) ``` +### Scope Attributes + +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. + +```java {tabTitle: Java} +import io.sentry.Sentry; +import io.sentry.ScopeType; +import io.sentry.SentryAttribute; +import io.sentry.SentryAttributes; + +// Set an attribute on the global scope so it applies to all logs +Sentry.configureScope(ScopeType.GLOBAL, scope -> { + scope.setAttribute("region", "us-east-1"); +}); + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123"); + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)); + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("tenant", "acme-corp"), + SentryAttribute.booleanAttribute("is_admin", true) +)); + +// All subsequent logs will include these attributes +Sentry.logger().info("Processing request"); + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("request.id"); +``` + +```kotlin {tabTitle: Kotlin} +import io.sentry.Sentry +import io.sentry.ScopeType +import io.sentry.SentryAttribute +import io.sentry.SentryAttributes + +// Set an attribute on the global scope so it applies to all logs +Sentry.configureScope(ScopeType.GLOBAL) { scope -> + scope.setAttribute("region", "us-east-1") +} + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123") + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)) + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("tenant", "acme-corp"), + SentryAttribute.booleanAttribute("is_admin", true) +)) + +// All subsequent logs will include these attributes +Sentry.logger().info("Processing request") + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("request.id") +``` + +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. + +Attributes passed directly to a log call override scope attributes with the same key. + 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 Logging Framework Integrations page. \ No newline at end of file diff --git a/platform-includes/metrics/default-attributes/android.mdx b/platform-includes/metrics/default-attributes/android.mdx index be18c1b0ad4a9..86efee35ae4f3 100644 --- a/platform-includes/metrics/default-attributes/android.mdx +++ b/platform-includes/metrics/default-attributes/android.mdx @@ -18,3 +18,7 @@ If user information is available in the current scope, the following attributes If replay information is available in the current scope, the following attributes are added to the log: - `sentry.replay_id`: The ID of the replay. + +### Scope Attributes + +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. diff --git a/platform-includes/metrics/default-attributes/java.mdx b/platform-includes/metrics/default-attributes/java.mdx index 9eae8daf8a4b5..bb52cdecab94e 100644 --- a/platform-includes/metrics/default-attributes/java.mdx +++ b/platform-includes/metrics/default-attributes/java.mdx @@ -18,3 +18,7 @@ If user information is available in the current scope, the following attributes The SDK will attach the following: - `server.address`: The address of the server that sent the metric. Equivalent to `server_name` that gets attached to Sentry errors. + +### Scope Attributes + +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. diff --git a/platform-includes/metrics/usage/android.mdx b/platform-includes/metrics/usage/android.mdx index 21a2fd7525ae0..4310f443d1216 100644 --- a/platform-includes/metrics/usage/android.mdx +++ b/platform-includes/metrics/usage/android.mdx @@ -108,6 +108,60 @@ Sentry.metrics().distribution( ) ``` +### Scope Attributes + +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. + +```java {tabTitle: Java} +import io.sentry.Sentry; +import io.sentry.SentryAttribute; +import io.sentry.SentryAttributes; + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123"); + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)); + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("service", "checkout"), + SentryAttribute.booleanAttribute("canary", false) +)); + +// All subsequent metrics will include these attributes +Sentry.metrics().count("order_placed", 1.0); + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("canary"); +``` + +```kotlin {tabTitle: Kotlin} +import io.sentry.Sentry +import io.sentry.SentryAttribute +import io.sentry.SentryAttributes + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123") + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)) + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("service", "checkout"), + SentryAttribute.booleanAttribute("canary", false) +)) + +// All subsequent metrics will include these attributes +Sentry.metrics().count("order_placed", 1.0) + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("canary") +``` + +Attributes passed directly to a metric call override scope attributes with the same key. + ### Specifying Units 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. diff --git a/platform-includes/metrics/usage/java.mdx b/platform-includes/metrics/usage/java.mdx index 21a2fd7525ae0..003cc7097c2af 100644 --- a/platform-includes/metrics/usage/java.mdx +++ b/platform-includes/metrics/usage/java.mdx @@ -108,6 +108,72 @@ Sentry.metrics().distribution( ) ``` +### Scope Attributes + +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. + +```java {tabTitle: Java} +import io.sentry.Sentry; +import io.sentry.ScopeType; +import io.sentry.SentryAttribute; +import io.sentry.SentryAttributes; + +// Set an attribute on the global scope so it applies to all metrics +Sentry.configureScope(ScopeType.GLOBAL, scope -> { + scope.setAttribute("region", "us-east-1"); +}); + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123"); + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)); + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("service", "checkout"), + SentryAttribute.booleanAttribute("canary", false) +)); + +// All subsequent metrics will include these attributes +Sentry.metrics().count("order_placed", 1.0); + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("canary"); +``` + +```kotlin {tabTitle: Kotlin} +import io.sentry.Sentry +import io.sentry.ScopeType +import io.sentry.SentryAttribute +import io.sentry.SentryAttributes + +// Set an attribute on the global scope so it applies to all metrics +Sentry.configureScope(ScopeType.GLOBAL) { scope -> + scope.setAttribute("region", "us-east-1") +} + +// Set a single attribute with automatic type inference +Sentry.setAttribute("request.id", "abc-123") + +// Or use a factory method to set the type explicitly +Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150)) + +// Set multiple attributes at once +Sentry.setAttributes(SentryAttributes.of( + SentryAttribute.stringAttribute("service", "checkout"), + SentryAttribute.booleanAttribute("canary", false) +)) + +// All subsequent metrics will include these attributes +Sentry.metrics().count("order_placed", 1.0) + +// Remove an attribute when it's no longer relevant +Sentry.removeAttribute("canary") +``` + +Attributes passed directly to a metric call override scope attributes with the same key. + ### Specifying Units 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.