-
-
Notifications
You must be signed in to change notification settings - Fork 372
feat(metrics): Add implementation for metrics envelope item #6960
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
Open
philprime
wants to merge
45
commits into
philprime/metrics-bootstrap
Choose a base branch
from
philprime/metrics-envelope
base: philprime/metrics-bootstrap
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 34 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
303d533
feat(metrics): Add implementation for metrics envelope item
philprime b991cda
Remove unrelated change
philprime 2aa1f43
fix compilation
philprime 6dfe9bb
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime 0ae250e
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime a7c7df9
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime c143d90
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime 70d5fd3
refactor latest item batcher
philprime 64cc81f
fixes
philprime f7a67bb
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime 70241f7
cleanup
philprime 40b52a3
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime 861161b
cleanup
philprime c6ce033
WIP
philprime d073e22
WIP
philprime 4980aad
cleanup
philprime 56320d5
Remove spanId from Metric struct and related tests; update BatcherSco…
philprime 3eca8ca
fix warnings on set
philprime 1b939e6
add comment explaining reasoning for jsonserialization in tests
philprime 06e863a
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime 74902aa
WIP
philprime 6dca42f
Update Sources/Swift/Integrations/Metrics/MetricsIntegration.swift
philprime 922ea27
WIP
philprime b9ed1a3
fix integration setup in tests
philprime 2a05eb7
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime c8b4ab5
fix tests for integraiton checking
philprime 1a6cf02
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime 62ee6ad
change metric batcher to struct
philprime fe0a0ef
update public api
philprime f10a253
update changelog
philprime f9efc90
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime a12babf
WIP
philprime ba4edde
cleanup
philprime 57b889a
move option to experimental
philprime 7f2f81d
cleanup
philprime 31dc8c8
remove duplicate setter
philprime 28d56ce
apply feedback from PR review
philprime 8d2f500
change encodable conformance to be internal
philprime 146146e
update public api
philprime 19293d4
Refactor SentryMetricsBatcher: Update buffer size to 1MB, enhance doc…
philprime 0273433
fix experimental options and add beforeSendMetrics to changelog
philprime 082e70b
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime b9bd8cc
Merge branch 'philprime/metrics-bootstrap' into philprime/metrics-env…
philprime 84255ec
Refactor SentryMetric to merge value and type
philprime d73700c
apply feedback from PR 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
100 changes: 100 additions & 0 deletions
100
Sources/Swift/Integrations/Metrics/SentryMetricBatcher.swift
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,100 @@ | ||
| @_implementationOnly import _SentryPrivate | ||
| import Foundation | ||
|
|
||
| /// Protocol for batching metrics with scope-based attribute enrichment. | ||
| protocol SentryMetricBatcherProtocol { | ||
| /// Adds a metric to the batcher. | ||
| /// - Parameters: | ||
| /// - metric: The metric to add | ||
| /// - scope: The scope to add the metric to | ||
| func addMetric(_ metric: SentryMetric, scope: Scope) | ||
|
|
||
| /// Captures batched metrics synchronously and returns the duration. | ||
| /// - Returns: The time taken to capture items in seconds | ||
| /// | ||
| /// - Note: This method blocks until all items are captured. The batcher's buffer is cleared after capture. | ||
| /// This is safe to call from any thread, but be aware that it uses dispatchSync internally, | ||
| /// so calling it from a context that holds locks or is on the batcher's queue itself could cause a deadlock. | ||
| @discardableResult func captureMetrics() -> TimeInterval | ||
| } | ||
|
|
||
| protocol SentryMetricBatcherOptionsProtocol { | ||
| var enableMetrics: Bool { get } | ||
| var beforeSendMetric: ((SentryMetric) -> SentryMetric?)? { get } | ||
| var environment: String { get } | ||
| var releaseName: String? { get } | ||
| var cacheDirectoryPath: String { get } | ||
| var sendDefaultPii: Bool { get } | ||
| } | ||
|
|
||
| struct SentryMetricBatcher: SentryMetricBatcherProtocol { | ||
| private let isEnabled: Bool | ||
| private let batcher: any BatcherProtocol<SentryMetric, Scope> | ||
|
|
||
| /// Initializes a new MetricBatcher. | ||
| /// - Parameters: | ||
| /// - options: The Sentry configuration options | ||
| /// - flushTimeout: The timeout interval after which buffered metrics will be flushed | ||
| /// - maxMetricCount: Maximum number of metrics to batch before triggering an immediate flush. | ||
| /// - maxBufferSizeBytes: The maximum buffer size in bytes before triggering an immediate flush | ||
| /// - dateProvider: Instance used to determine current time | ||
| /// - dispatchQueue: A **serial** dispatch queue wrapper for thread-safe access to mutable state | ||
| /// - capturedDataCallback: The callback to handle captured metric batches. This callback is responsible | ||
| /// for invoking client.captureMetricsData() with the batched data. | ||
| /// | ||
| /// - Important: The `dispatchQueue` parameter MUST be a serial queue to ensure thread safety. | ||
| /// Passing a concurrent queue will result in undefined behavior and potential data races. | ||
| /// | ||
| /// - Note: Metrics are flushed when either `maxMetricCount` or `maxBufferSizeBytes` limit is reached. | ||
| init( | ||
| options: SentryMetricBatcherOptionsProtocol, | ||
| flushTimeout: TimeInterval = 5, | ||
| maxMetricCount: Int = 100, // Maximum 100 metrics per batch | ||
| maxBufferSizeBytes: Int = 2 * 1_024, // 2 KiB buffer size, see: https://develop.sentry.dev/sdk/data-model/envelopes/#size-limits | ||
| dateProvider: SentryCurrentDateProvider, | ||
| dispatchQueue: SentryDispatchQueueWrapper, | ||
| capturedDataCallback: @escaping (_ data: Data, _ count: Int) -> Void | ||
| ) { | ||
| self.isEnabled = options.enableMetrics | ||
| self.batcher = Batcher( | ||
| config: .init( | ||
| sendDefaultPii: options.sendDefaultPii, | ||
| flushTimeout: flushTimeout, | ||
| maxItemCount: maxMetricCount, | ||
| maxBufferSizeBytes: maxBufferSizeBytes, | ||
| beforeSendItem: options.beforeSendMetric, | ||
| capturedDataCallback: capturedDataCallback | ||
| ), | ||
| metadata: .init( | ||
| environment: options.environment, | ||
| releaseName: options.releaseName, | ||
| installationId: SentryInstallation.cachedId(withCacheDirectoryPath: options.cacheDirectoryPath) | ||
| ), | ||
| buffer: InMemoryBatchBuffer(), | ||
| dateProvider: dateProvider, | ||
| dispatchQueue: dispatchQueue | ||
| ) | ||
| } | ||
|
|
||
| func addMetric(_ metric: SentryMetric, scope: Scope) { | ||
| guard isEnabled else { | ||
| return | ||
| } | ||
| batcher.add(metric, scope: scope) | ||
| } | ||
|
|
||
| @discardableResult | ||
| func captureMetrics() -> TimeInterval { | ||
| return batcher.capture() | ||
| } | ||
| } | ||
|
|
||
| extension Options: SentryMetricBatcherOptionsProtocol { | ||
| var enableMetrics: Bool { | ||
| return experimental.enableMetrics | ||
| } | ||
|
|
||
| var beforeSendMetric: ((SentryMetric) -> SentryMetric?)? { | ||
| return experimental.beforeSendMetric | ||
| } | ||
| } |
35 changes: 32 additions & 3 deletions
35
Sources/Swift/Integrations/Metrics/SentryMetricsIntegration.swift
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 |
|---|---|---|
| @@ -1,13 +1,42 @@ | ||
| final class SentryMetricsIntegration<Dependencies>: NSObject, SwiftIntegration { | ||
| @_implementationOnly import _SentryPrivate | ||
|
|
||
| final class SentryMetricsIntegration<Dependencies: DateProviderProvider & DispatchQueueWrapperProvider>: NSObject, SwiftIntegration { | ||
| private let metricBatcher: SentryMetricBatcherProtocol | ||
|
|
||
| init?(with options: Options, dependencies: Dependencies) { | ||
| guard options.experimental.enableMetrics else { return nil } | ||
|
|
||
| SentrySDKLog.debug("Integration initialized") | ||
| self.metricBatcher = SentryMetricBatcher( | ||
| options: options, | ||
| dateProvider: dependencies.dateProvider, | ||
| dispatchQueue: dependencies.dispatchQueueWrapper, | ||
| capturedDataCallback: { data, count in | ||
| let hub = SentrySDKInternal.currentHub() | ||
| guard let client = hub.getClient() else { | ||
| SentrySDKLog.debug("MetricsIntegration: No client available, dropping metrics") | ||
| return | ||
| } | ||
| client.captureMetricsData(data, with: NSNumber(value: count)) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| func uninstall() {} | ||
| func uninstall() { | ||
| // Flush any pending metrics before uninstalling. | ||
| // | ||
| // Note: This calls captureMetrics() synchronously, which uses dispatchSync internally. | ||
| // This is safe because uninstall() is typically called from the main thread during | ||
| // app lifecycle events, and the batcher's dispatch queue is a separate serial queue. | ||
| metricBatcher.captureMetrics() | ||
| } | ||
|
|
||
| static var name: String { | ||
philprime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "SentryMetricsIntegration" | ||
| } | ||
|
|
||
| // MARK: - Public API for MetricsApi | ||
|
|
||
| func addMetric(_ metric: SentryMetric, scope: Scope) { | ||
| metricBatcher.addMetric(metric, scope: scope) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.