-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(apple): Add documentation for metrics #15822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
597b5f7
feat(apple): Add documentation for metrics
philprime 20e68fd
expanded metrics documentation
philprime 45026a5
Merge remote-tracking branch 'origin/master' into philprime/cocoa-met…
philprime e96674e
updated docs with latest changes
philprime cfc1676
update min available and default values
philprime df06783
Merge branch 'master' into philprime/cocoa-metrics
philprime 2e7ded0
Merge branch 'master' into philprime/cocoa-metrics
philprime 17b2664
Merge remote-tracking branch 'origin/master' into philprime/cocoa-met…
philprime 92b211a
docs(apple): update note to alert for trace_id usage in metrics
philprime c7ed218
Add Apple platforms to product explore
philprime d27f752
Merge remote-tracking branch 'origin/master' into philprime/cocoa-met…
philprime e17df5a
apply feedback from pull request review
philprime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --- | ||
| title: Set Up Metrics | ||
| sidebar_title: Metrics | ||
| description: "Metrics allow you to send, view and query counters, gauges, and distributions from your Sentry-configured apps to track application health and drill down into related traces, logs, and errors." | ||
| sidebar_order: 7 | ||
| sidebar_section: features | ||
| beta: true | ||
| --- | ||
|
|
||
| With Sentry Metrics, you can send counters, gauges, and distributions from your applications to Sentry. Once in Sentry, these metrics can be viewed alongside relevant errors, traces, and logs, and searched using their individual attributes. | ||
|
|
||
| <Alert> | ||
| This feature is currently in open beta. Please reach out on [GitHub](https://github.com/getsentry/sentry-cocoa/discussions) if you have feedback or questions. Features in beta are still in-progress and may have bugs. We recognize the irony. | ||
| </Alert> | ||
|
|
||
| ## Requirements | ||
|
|
||
| <PlatformContent includePath="metrics/requirements" /> | ||
|
|
||
| ## Usage | ||
|
|
||
| <PlatformContent includePath="metrics/usage" /> | ||
|
|
||
| ## Options | ||
|
|
||
| <PlatformContent includePath="metrics/options" /> | ||
|
|
||
| ## Default Attributes | ||
|
|
||
| <PlatformContent includePath="metrics/default-attributes" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| By default the SDK will attach the following attributes to a metric: | ||
|
|
||
| ### Device and OS Attributes | ||
|
|
||
| - `device.brand`: Always "Apple" for Apple devices. | ||
| - `device.model`: The device model (e.g., "iPhone14,2"). | ||
| - `device.family`: The device family (e.g., "iPhone"). | ||
| - `os.name`: The operating system name (e.g., "iOS"). | ||
| - `os.version`: The operating system version. | ||
|
|
||
| ### User Attributes | ||
|
|
||
| User attributes are only added when `options.sendDefaultPii` is set to `true`: | ||
|
|
||
| - `user.id`: The user ID from the current scope. Falls back to the installation ID if no user is set. | ||
| - `user.name`: The username from the current scope. | ||
| - `user.email`: The email address from the current scope. | ||
philprime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### SDK Attributes | ||
|
|
||
| - `sentry.sdk.name`: The name of the SDK that sent the metric (e.g., "sentry.cocoa"). | ||
| - `sentry.sdk.version`: The version of the SDK that sent the metric. | ||
| - `sentry.environment`: The environment set in the SDK. | ||
| - `sentry.release`: The release set in the SDK, if defined. | ||
|
|
||
| ### Trace Attributes | ||
|
|
||
| If there is an active span when the metric is recorded, the following attribute is added: | ||
|
|
||
| - `span_id`: The span ID of the active span. | ||
|
|
||
| <Alert type="info"> | ||
| The `trace_id` is set as a top-level field on the metric (not as an attribute) to enable distributed tracing correlation. When a span is active, the SDK uses that span's trace ID; otherwise, it falls back to the propagation context's trace ID. | ||
| </Alert> | ||
|
|
||
| ### Session Replay Attributes | ||
|
|
||
| If Session Replay is enabled and active (iOS and tvOS only): | ||
|
|
||
| - `sentry.replay_id`: The current replay session ID. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| The Sentry Cocoa SDK provides several options to configure how metrics are captured and sent to Sentry. | ||
|
|
||
| ### Enabling/Disabling Metrics | ||
|
|
||
| Metrics are enabled by default, but are not collected automatically. You must manually call the `SentrySDK.metrics` API to record metrics. If you need to disable metrics entirely, set `options.experimental.enableMetrics` to `false` when initializing the SDK: | ||
|
|
||
| ```swift {tabTitle:Swift} | ||
| import Sentry | ||
|
|
||
| SentrySDK.start { options in | ||
| options.dsn = "___PUBLIC_DSN___" | ||
| options.experimental.enableMetrics = false // Metrics are enabled by default | ||
| } | ||
| ``` | ||
|
|
||
| ```objc {tabTitle:Objective-C} | ||
| @import Sentry; | ||
|
|
||
| [SentrySDK startWithConfigureOptions:^(SentryOptions *options) { | ||
| options.dsn = @"___PUBLIC_DSN___"; | ||
| options.experimental.enableMetrics = NO; // Metrics are enabled by default | ||
| }]; | ||
| ``` | ||
|
|
||
| ### Filtering and Modifying Metrics | ||
|
|
||
| Use the `beforeSendMetric` callback to filter or modify metrics before they're sent to Sentry. This is useful for: | ||
|
|
||
| - Removing sensitive data from metric attributes | ||
| - Dropping metrics you don't want to send | ||
| - Adding or modifying attributes | ||
| - Changing metric names or units | ||
|
|
||
| The callback receives a `SentryMetric` struct and must return either a modified metric or `nil` to drop it. | ||
|
|
||
| **Available `SentryMetric` properties:** | ||
| - `name`: The metric name (e.g., "api.response_time") | ||
| - `value`: The metric value (`SentryMetricValue` - counter, distribution, or gauge) | ||
| - `unit`: The unit of measurement (`SentryUnit?`) | ||
| - `attributes`: Dictionary of attributes (`[String: SentryAttributeContent]`) | ||
| - `timestamp`: When the metric was recorded | ||
| - `traceId`: Associated trace ID for distributed tracing correlation | ||
|
|
||
| When modifying `attributes`, you can assign values directly using literals thanks to Swift's `ExpressibleBy...Literal` protocols. The SDK supports `String`, `Bool`, `Int`, `Double`, and arrays of these types. | ||
|
|
||
| ```swift | ||
| import Sentry | ||
|
|
||
| SentrySDK.start { options in | ||
| options.dsn = "___PUBLIC_DSN___" | ||
| // Metrics are enabled by default, no need to set enableMetrics = true | ||
| options.experimental.beforeSendMetric = { metric in | ||
| // Create a mutable copy (SentryMetric is a struct) | ||
| var metric = metric | ||
|
|
||
| // Drop metrics based on attributes | ||
| if case .boolean(let dropMe) = metric.attributes["dropMe"], dropMe { | ||
| return nil | ||
| } | ||
philprime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Drop metrics by name | ||
| if metric.name.hasPrefix("internal.") { | ||
| return nil | ||
| } | ||
|
|
||
| // Modify or add attributes using literals (recommended) | ||
| metric.attributes["processed"] = true // Boolean literal | ||
| metric.attributes["environment"] = "production" // String literal | ||
| metric.attributes["retry_count"] = 3 // Integer literal | ||
| metric.attributes["latency"] = 42.5 // Double literal | ||
|
|
||
| // You can also use enum cases for explicitness | ||
| metric.attributes["explicit"] = .boolean(true) | ||
| metric.attributes["tags"] = .stringArray(["tag1", "tag2"]) | ||
|
|
||
| // Change the metric name | ||
| if metric.name == "legacy_metric" { | ||
| metric.name = "new_metric_name" | ||
| } | ||
|
|
||
| return metric | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Flushing Metrics | ||
|
|
||
| By default, metrics are buffered and flushed depending on buffer size and time. If you need to manually flush metrics before the automatic interval, you can use the `flush` method. Note that this is a **blocking** call that will wait until all pending data is sent or the timeout is reached: | ||
|
|
||
| ```swift | ||
| import Sentry | ||
|
|
||
| SentrySDK.flush(timeout: 5.0) | ||
| ``` | ||
|
|
||
| This will flush all pending metrics and events to Sentry. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Metrics are supported in Sentry Cocoa SDK version `9.2.0` and above. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.