Skip to content

Commit 3867759

Browse files
committed
WIP
1 parent 6237006 commit 3867759

20 files changed

+2185
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add attributes data to `SentryScope` (#6830)
88
- Add `SentryScope` attributes into log messages (#6834)
99
- Add integration to collect Metrics, can be enabled by setting `options.enableMetrics = true` (#6956)
10+
- Add `Sentry.metrics.count(..)`, `Sentry.metrics.distribution(..)` and `Sentry.metrics.gauge(..)` to public API (#6957)
1011

1112
## 9.0.0
1213

Sentry.xcodeproj/project.pbxproj

Lines changed: 12 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+
D46B04482EDF25E100AF4A0A /* SentryMetric.swift in Sources */ = {isa = PBXBuildFile; fileRef = D46B04472EDF25E100AF4A0A /* SentryMetric.swift */; };
788+
D46B044F2EDF260A00AF4A0A /* SentryMetricBatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D46B044E2EDF260A00AF4A0A /* SentryMetricBatcher.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+
D46B04472EDF25E100AF4A0A /* SentryMetric.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMetric.swift; sourceTree = "<group>"; };
2153+
D46B044E2EDF260A00AF4A0A /* SentryMetricBatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMetricBatcher.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;
@@ -4645,6 +4651,7 @@
46454651
FA01BCB12E69352A00968DFA /* SentryDiscardedEvent.swift */,
46464652
92235CAD2E15549C00865983 /* SentryLogger.swift */,
46474653
92235CAB2E15369900865983 /* SentryLogBatcher.swift */,
4654+
D46B044E2EDF260A00AF4A0A /* SentryMetricBatcher.swift */,
46484655
F451FAA52E0B304E0050ACF2 /* LoadValidator.swift */,
46494656
FA34C1A22E692A5000BC52AA /* SentryEnvelopeItem.swift */,
46504657
FA90FAFC2E070A3B008CAAE8 /* SentryURLRequestFactory.swift */,
@@ -4870,6 +4877,7 @@
48704877
92ECD73F2E05AD500063EC10 /* SentryLogAttribute.swift */,
48714878
92ECD73D2E05AD2B0063EC10 /* SentryLogLevel.swift */,
48724879
9264E1EA2E2E385B00B077CF /* SentryLogMessage.swift */,
4880+
D46B04472EDF25E100AF4A0A /* SentryMetric.swift */,
48734881
F458D1122E180BB00028273E /* SentryFileManagerProtocol.swift */,
48744882
);
48754883
path = Protocol;
@@ -5803,8 +5811,11 @@
58035811
D8ACE3C82762187200F5A213 /* SentryFileIOTrackerHelper.m in Sources */,
58045812
D8B088B729C9E3FF00213258 /* SentryTracerConfiguration.m in Sources */,
58055813
FA7206E12E0B37C80072FDD4 /* SentryProfileCollector.mm in Sources */,
5814+
D46B044F2EDF260A00AF4A0A /* SentryMetricBatcher.swift in Sources */,
5815+
D46B043E2EDF1E3C00AF4A0A /* MetricsApi.swift in Sources */,
58065816
9264E1EB2E2E385E00B077CF /* SentryLogMessage.swift in Sources */,
58075817
8ECC674A25C23A20000E2BF6 /* SentryTransactionContext.m in Sources */,
5818+
D46B04482EDF25E100AF4A0A /* SentryMetric.swift in Sources */,
58085819
03BCC38C27E1C01A003232C7 /* SentryTime.mm in Sources */,
58095820
A8F17B342902870300990B25 /* SentryHttpStatusCodeRange.m in Sources */,
58105821
62C97D3A2CC64E6B00DDA204 /* SentryUncaughtNSExceptions.m in Sources */,
@@ -6313,6 +6324,7 @@
63136324
7B72D23A28D074BC0014798A /* TestExtensions.swift in Sources */,
63146325
7BBD18BB24530D2600427C76 /* SentryFileManagerTests.swift in Sources */,
63156326
63FE722020DA66EC00CDBAE8 /* SentryCrashObjC_Tests.m in Sources */,
6327+
D46B04422EDF207900AF4A0A /* MetricsApiTests.swift in Sources */,
63166328
62277BBC2DA5183500EF06B7 /* SentryTracer+Test.m in Sources */,
63176329
7B58816727FC5D790098B121 /* SentryDiscardReasonMapperTests.swift in Sources */,
63186330
D434DB102DE09D0300DD6F82 /* TestSentryWatchdogTerminationAttributesProcessorTests.swift in Sources */,

SentryTestUtils/Sources/TestClient.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,10 @@ public class TestClient: SentryClientInternal {
167167
captureLogInvocations.record((castLog, scope))
168168
}
169169
}
170+
171+
@_spi(Private) public var captureMetricsDataInvocations = Invocations<(data: NSData, count: NSNumber)>()
172+
@_spi(Private) public override func captureMetricsData(_ data: NSData, with itemCount: NSNumber) {
173+
captureMetricsDataInvocations.record((data, itemCount))
174+
super.captureMetricsData(data, with: itemCount)
175+
}
170176
}

Sources/Sentry/SentryClient.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,18 @@ - (void)captureLogsData:(NSData *)data with:(NSNumber *)itemCount
11171117
[self captureEnvelope:envelope];
11181118
}
11191119

1120+
- (void)captureMetricsData:(NSData *)data with:(NSNumber *)itemCount
1121+
{
1122+
SentryEnvelopeItem *envelopeItem =
1123+
[[SentryEnvelopeItem alloc] initWithType:SentryEnvelopeItemTypes.traceMetric
1124+
data:data
1125+
contentType:@"application/vnd.sentry.items.trace-metric+json"
1126+
itemCount:itemCount];
1127+
SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:[SentryEnvelopeHeader empty]
1128+
singleItem:envelopeItem];
1129+
[self captureEnvelope:envelope];
1130+
}
1131+
11201132
@end
11211133

11221134
NS_ASSUME_NONNULL_END

Sources/Sentry/SentryDataCategoryMapper.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
NSString *const kSentryDataCategoryNameSpan = @"span";
1919
NSString *const kSentryDataCategoryNameFeedback = @"feedback";
2020
NSString *const kSentryDataCategoryNameLogItem = @"log_item";
21+
NSString *const kSentryDataCategoryNameTraceMetric = @"trace_metric";
2122
NSString *const kSentryDataCategoryNameUnknown = @"unknown";
2223

2324
NS_ASSUME_NONNULL_BEGIN
@@ -57,6 +58,9 @@
5758
if ([itemType isEqualToString:SentryEnvelopeItemTypes.log]) {
5859
return kSentryDataCategoryLogItem;
5960
}
61+
if ([itemType isEqualToString:SentryEnvelopeItemTypes.traceMetric]) {
62+
return kSentryDataCategoryTraceMetric;
63+
}
6064

6165
return kSentryDataCategoryDefault;
6266
}
@@ -113,6 +117,9 @@
113117
if ([value isEqualToString:kSentryDataCategoryNameLogItem]) {
114118
return kSentryDataCategoryLogItem;
115119
}
120+
if ([value isEqualToString:kSentryDataCategoryNameTraceMetric]) {
121+
return kSentryDataCategoryTraceMetric;
122+
}
116123

117124
return kSentryDataCategoryUnknown;
118125
}
@@ -148,6 +155,8 @@
148155
return kSentryDataCategoryNameFeedback;
149156
case kSentryDataCategoryLogItem:
150157
return kSentryDataCategoryNameLogItem;
158+
case kSentryDataCategoryTraceMetric:
159+
return kSentryDataCategoryNameTraceMetric;
151160

152161
default: // !!!: fall-through!
153162
case kSentryDataCategoryUnknown:

Sources/Sentry/SentryOptionsInternal.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ + (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
120120
sentryOptions.beforeSendLog = options[@"beforeSendLog"];
121121
}
122122

123+
if ([self isBlock:options[@"beforeSendMetric"]]) {
124+
sentryOptions.beforeSendMetric = options[@"beforeSendMetric"];
125+
}
126+
123127
if ([self isBlock:options[@"beforeSendSpan"]]) {
124128
sentryOptions.beforeSendSpan = options[@"beforeSendSpan"];
125129
}

Sources/Sentry/include/SentryClient+Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ NS_ASSUME_NONNULL_BEGIN
8282

8383
- (void)_swiftCaptureLog:(NSObject *)log withScope:(SentryScope *)scope;
8484

85+
- (void)captureMetricsData:(NSData *)data with:(NSNumber *)itemCount;
86+
8587
@end
8688

8789
NS_ASSUME_NONNULL_END

Sources/Sentry/include/SentryDataCategory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ typedef NS_ENUM(NSUInteger, SentryDataCategory) {
2020
kSentryDataCategorySpan = 11,
2121
kSentryDataCategoryFeedback = 12,
2222
kSentryDataCategoryLogItem = 13,
23-
kSentryDataCategoryUnknown = 14,
23+
kSentryDataCategoryTraceMetric = 14,
24+
kSentryDataCategoryUnknown = 15,
2425
};

Sources/Swift/Helper/SentryEnvelopeItemType.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
public static let statsd = "statsd"
1515
public static let profileChunk = "profile_chunk"
1616
public static let log = "log"
17+
public static let traceMetric = "trace_metric"
1718
}

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

0 commit comments

Comments
 (0)