Skip to content

Commit 97c11f4

Browse files
committed
feat(metrics): Add public API to collect count, distribution and gauge
1 parent bbf3041 commit 97c11f4

File tree

4 files changed

+366
-1
lines changed

4 files changed

+366
-1
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,8 @@
784784
D468C0622D3669A200964230 /* SentryFileIOTracker+SwiftHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D468C0612D3669A200964230 /* SentryFileIOTracker+SwiftHelpers.swift */; };
785785
D46B041D2EDF168400AF4A0A /* MetricsIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D46B041C2EDF167D00AF4A0A /* MetricsIntegration.swift */; };
786786
D46B04202EDF175C00AF4A0A /* MetricsIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D46B041F2EDF175600AF4A0A /* MetricsIntegrationTests.swift */; };
787+
D46B043E2EDF1E3C00AF4A0A /* MetricsApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = D46B043D2EDF1E3A00AF4A0A /* MetricsApi.swift */; };
788+
D46B04422EDF207900AF4A0A /* MetricsApiTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D46B04412EDF207900AF4A0A /* MetricsApiTests.swift */; };
787789
D473ACD72D8090FC000F1CC6 /* FileManager+SentryTracing.swift in Sources */ = {isa = PBXBuildFile; fileRef = D473ACD62D8090FC000F1CC6 /* FileManager+SentryTracing.swift */; };
788790
D480F9D92DE47A50009A0594 /* TestSentryScopePersistentStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D480F9D82DE47A48009A0594 /* TestSentryScopePersistentStore.swift */; };
789791
D480F9DB2DE47AF2009A0594 /* SentryScopePersistentStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D480F9DA2DE47AEB009A0594 /* SentryScopePersistentStoreTests.swift */; };
@@ -2147,6 +2149,8 @@
21472149
D468C0612D3669A200964230 /* SentryFileIOTracker+SwiftHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryFileIOTracker+SwiftHelpers.swift"; sourceTree = "<group>"; };
21482150
D46B041C2EDF167D00AF4A0A /* MetricsIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricsIntegration.swift; sourceTree = "<group>"; };
21492151
D46B041F2EDF175600AF4A0A /* MetricsIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricsIntegrationTests.swift; sourceTree = "<group>"; };
2152+
D46B043D2EDF1E3A00AF4A0A /* MetricsApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricsApi.swift; sourceTree = "<group>"; };
2153+
D46B04412EDF207900AF4A0A /* MetricsApiTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricsApiTests.swift; sourceTree = "<group>"; };
21502154
D46D45E12D5F3FD600A1CB35 /* Sentry_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = Sentry_Base.xctestplan; sourceTree = "<group>"; };
21512155
D46D45E92D5F411700A1CB35 /* SentrySwiftUI_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = SentrySwiftUI_Base.xctestplan; path = Plans/SentrySwiftUI_Base.xctestplan; sourceTree = SOURCE_ROOT; };
21522156
D473ACD62D8090FC000F1CC6 /* FileManager+SentryTracing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+SentryTracing.swift"; sourceTree = "<group>"; };
@@ -4369,6 +4373,7 @@
43694373
D46B04162EDF167800AF4A0A /* Metrics */ = {
43704374
isa = PBXGroup;
43714375
children = (
4376+
D46B043D2EDF1E3A00AF4A0A /* MetricsApi.swift */,
43724377
D46B041C2EDF167D00AF4A0A /* MetricsIntegration.swift */,
43734378
);
43744379
path = Metrics;
@@ -4377,6 +4382,7 @@
43774382
D46B041E2EDF173A00AF4A0A /* Metrics */ = {
43784383
isa = PBXGroup;
43794384
children = (
4385+
D46B04412EDF207900AF4A0A /* MetricsApiTests.swift */,
43804386
D46B041F2EDF175600AF4A0A /* MetricsIntegrationTests.swift */,
43814387
);
43824388
path = Metrics;
@@ -5803,6 +5809,7 @@
58035809
D8ACE3C82762187200F5A213 /* SentryFileIOTrackerHelper.m in Sources */,
58045810
D8B088B729C9E3FF00213258 /* SentryTracerConfiguration.m in Sources */,
58055811
FA7206E12E0B37C80072FDD4 /* SentryProfileCollector.mm in Sources */,
5812+
D46B043E2EDF1E3C00AF4A0A /* MetricsApi.swift in Sources */,
58065813
9264E1EB2E2E385E00B077CF /* SentryLogMessage.swift in Sources */,
58075814
8ECC674A25C23A20000E2BF6 /* SentryTransactionContext.m in Sources */,
58085815
03BCC38C27E1C01A003232C7 /* SentryTime.mm in Sources */,
@@ -6313,6 +6320,7 @@
63136320
7B72D23A28D074BC0014798A /* TestExtensions.swift in Sources */,
63146321
7BBD18BB24530D2600427C76 /* SentryFileManagerTests.swift in Sources */,
63156322
63FE722020DA66EC00CDBAE8 /* SentryCrashObjC_Tests.m in Sources */,
6323+
D46B04422EDF207900AF4A0A /* MetricsApiTests.swift in Sources */,
63166324
62277BBC2DA5183500EF06B7 /* SentryTracer+Test.m in Sources */,
63176325
7B58816727FC5D790098B121 /* SentryDiscardReasonMapperTests.swift in Sources */,
63186326
D434DB102DE09D0300DD6F82 /* TestSentryWatchdogTerminationAttributesProcessorTests.swift in Sources */,

Sources/Swift/Helper/SentrySDK.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ import Foundation
3737
return SentryLogger(dateProvider: SentryDependencyContainer.sharedInstance().dateProvider)
3838
}
3939
}
40-
40+
41+
/// API to collect metrics
42+
@objc public static var metrics = MetricsApi()
43+
4144
/// Inits and configures Sentry (`SentryHub`, `SentryClient`) and sets up all integrations. Make sure to
4245
/// set a valid DSN.
4346
/// - note: Call this method on the main thread. When calling it from a background thread, the
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@objc public class MetricsApi: NSObject {
2+
3+
/// Records a count metric for the specified key.
4+
///
5+
/// Use this to increment or set a discrete occurrence count associated with a metric key,
6+
/// such as the number of events, requests, or errors.
7+
///
8+
/// - Parameters:
9+
/// - key: A namespaced identifier for the metric (for example, "network.request.count").
10+
/// Prefer stable, lowercase, dot-delimited names to aid aggregation and filtering.
11+
/// - value: The count value to record. Typically a non-negative integer (e.g., 1 to increment by one).
12+
/// Values less than zero may be ignored or clamped by the metrics backend.
13+
public func count(key: String, value: Int) {
14+
// noop
15+
}
16+
17+
/// Records a distribution metric for the specified key.
18+
///
19+
/// Use this to track the distribution of a value over time, such as response times,
20+
/// request durations, or any measurable quantity where you want to analyze statistical
21+
/// properties (mean, median, percentiles, etc.).
22+
///
23+
/// - Parameters:
24+
/// - key: A namespaced identifier for the metric (for example, "http.request.duration").
25+
/// Prefer stable, lowercase, dot-delimited names to aid aggregation and filtering.
26+
/// - value: The value to record in the distribution. This can be any numeric value
27+
/// representing the measurement (e.g., milliseconds for response time).
28+
public func distribution(key: String, value: Double) {
29+
// noop
30+
}
31+
32+
/// Records a gauge metric for the specified key.
33+
///
34+
/// Use this to track a value that can go up and down over time, such as current memory usage,
35+
/// queue depth, active connections, or any metric that represents a current state rather
36+
/// than an incrementing counter.
37+
///
38+
/// - Parameters:
39+
/// - key: A namespaced identifier for the metric (for example, "memory.usage" or "queue.depth").
40+
/// Prefer stable, lowercase, dot-delimited names to aid aggregation and filtering.
41+
/// - value: The current gauge value to record. This represents the state at the time of
42+
/// recording (e.g., current memory in bytes, current number of items in queue).
43+
public func gauge(key: String, value: Double) {
44+
// noop
45+
}
46+
}

0 commit comments

Comments
 (0)