Skip to content

Commit d05d866

Browse files
authored
feat: Add support for dist and environment fields for termination watch (#5560)
* Update tests in SentryScopePersistentStoreTests and SentryWatchdogTerminationFieldsProcessorTests for the new fields * Rename `SentryWatchdogTerminationFieldsProcessor` to `SentryWatchdogTerminationAttributesProcessor` * feat: Add support for dist and environment fields for termination watch * Update `SentrySDKTests` tests * Update `SentrySDKTests` tests * Update tests * Update changelog * Undo extrDictionary move * Fix rebase tests * Extract string encoding / decoding to a new file
1 parent ccf1278 commit d05d866

18 files changed

+591
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Removed `APPLICATION_EXTENSION_API_ONLY` requirement (#5524)
1414
- Improve launch profile configuration management (#5318)
1515
- Record user for watchdog termination events (#5558)
16+
- Add support for dist and environment fields for termination watch (#5560)
1617

1718
## 8.53.1
1819

Sentry.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@
10271027
F452438A2DE65968003E8F50 /* ExceptionCatcher.h in Headers */ = {isa = PBXBuildFile; fileRef = F45243862DE65968003E8F50 /* ExceptionCatcher.h */; };
10281028
F452438C2DE65BC0003E8F50 /* SentryUseNSExceptionCallstackWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = F452438B2DE65BC0003E8F50 /* SentryUseNSExceptionCallstackWrapper.h */; };
10291029
F458D1132E180BB00028273E /* SentryFileManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F458D1122E180BB00028273E /* SentryFileManagerProtocol.swift */; };
1030+
F458D1152E1869AD0028273E /* SentryScopePersistentStore+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = F458D1142E1869AD0028273E /* SentryScopePersistentStore+String.swift */; };
10301031
F49D41982DEA27AF00D9244E /* SentryUseNSExceptionCallstackWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F49D41972DEA27AF00D9244E /* SentryUseNSExceptionCallstackWrapperTests.swift */; };
10311032
F49D419A2DEA2FB000D9244E /* SentryCrashExceptionApplicationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F49D41992DEA2FB000D9244E /* SentryCrashExceptionApplicationTests.swift */; };
10321033
F49D419C2DEA30C300D9244E /* SentryCrashExceptionApplicationHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = F49D419B2DEA30B800D9244E /* SentryCrashExceptionApplicationHelper.h */; };
@@ -2290,6 +2291,7 @@
22902291
F45243872DE65968003E8F50 /* ExceptionCatcher.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ExceptionCatcher.m; sourceTree = "<group>"; };
22912292
F452438B2DE65BC0003E8F50 /* SentryUseNSExceptionCallstackWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryUseNSExceptionCallstackWrapper.h; path = include/SentryUseNSExceptionCallstackWrapper.h; sourceTree = "<group>"; };
22922293
F458D1122E180BB00028273E /* SentryFileManagerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryFileManagerProtocol.swift; sourceTree = "<group>"; };
2294+
F458D1142E1869AD0028273E /* SentryScopePersistentStore+String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryScopePersistentStore+String.swift"; sourceTree = "<group>"; };
22932295
F49D41972DEA27AF00D9244E /* SentryUseNSExceptionCallstackWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUseNSExceptionCallstackWrapperTests.swift; sourceTree = "<group>"; };
22942296
F49D41992DEA2FB000D9244E /* SentryCrashExceptionApplicationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashExceptionApplicationTests.swift; sourceTree = "<group>"; };
22952297
F49D419B2DEA30B800D9244E /* SentryCrashExceptionApplicationHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryCrashExceptionApplicationHelper.h; path = include/SentryCrashExceptionApplicationHelper.h; sourceTree = "<group>"; };
@@ -4039,6 +4041,7 @@
40394041
isa = PBXGroup;
40404042
children = (
40414043
F4E3DCCA2E1579240093CB80 /* SentryScopePersistentStore.swift */,
4044+
F458D1142E1869AD0028273E /* SentryScopePersistentStore+String.swift */,
40424045
);
40434046
path = Persistence;
40444047
sourceTree = "<group>";
@@ -5374,6 +5377,7 @@
53745377
620078722D38F00D0022CB67 /* SentryGeoCodable.swift in Sources */,
53755378
D8CB741B2947286500A5F964 /* SentryEnvelopeItemHeader.m in Sources */,
53765379
92ECD73C2E05ACE00063EC10 /* SentryLog.swift in Sources */,
5380+
F458D1152E1869AD0028273E /* SentryScopePersistentStore+String.swift in Sources */,
53775381
D8CB7417294724CC00A5F964 /* SentryEnvelopeAttachmentHeader.m in Sources */,
53785382
D84793262788737D00BE8E99 /* SentryByteCountFormatter.m in Sources */,
53795383
63AA769E1EB9C57A00D153DE /* SentryError.mm in Sources */,

Sources/Sentry/SentryScope.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ @interface SentryScope ()
2929
*/
3030
@property (atomic, strong) NSMutableDictionary<NSString *, id> *extraDictionary;
3131

32-
/**
33-
* This distribution of the application.
34-
*/
35-
@property (atomic, copy) NSString *_Nullable distString;
36-
3732
/**
3833
* Set the fingerprint of an event to determine the grouping
3934
*/

Sources/Sentry/SentryWatchdogTerminationScopeObserver.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ - (void)setContext:(nullable NSDictionary<NSString *, id> *)context
5454

5555
- (void)setDist:(nullable NSString *)dist
5656
{
57-
// Left blank on purpose
57+
[self.attributesProcessor setDist:dist];
5858
}
5959

6060
- (void)setEnvironment:(nullable NSString *)environment
6161
{
62-
// Left blank on purpose
62+
[self.attributesProcessor setEnvironment:environment];
6363
}
6464

6565
- (void)setExtras:(nullable NSDictionary<NSString *, id> *)extras

Sources/Sentry/SentryWatchdogTerminationTracker.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ - (void)start
5858
[self addBreadcrumbsToEvent:event];
5959
[self addContextToEvent:event];
6060
event.user = [self.scopePersistentStore readPreviousUserFromDisk];
61+
event.dist = [self.scopePersistentStore readPreviousDistFromDisk];
62+
event.environment = [self.scopePersistentStore readPreviousEnvironmentFromDisk];
6163

6264
SentryException *exception =
6365
[[SentryException alloc] initWithValue:SentryWatchdogTerminationExceptionValue

Sources/Sentry/SentryWatchdogTerminationTrackingIntegration.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ - (BOOL)installWithOptions:(SentryOptions *)options
9797
// before installation.
9898
[scopeObserver setContext:outerScope.contextDictionary];
9999
[scopeObserver setUser:outerScope.userObject];
100+
[scopeObserver setEnvironment:outerScope.environmentString];
101+
[scopeObserver setDist:outerScope.distString];
100102
}];
101103

102104
return YES;

Sources/Sentry/include/SentryScope+Private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ NS_ASSUME_NONNULL_BEGIN
2525
*/
2626
@property (nonatomic, strong) SentryPropagationContext *propagationContext;
2727

28+
/**
29+
* This distribution of the application.
30+
*/
31+
@property (atomic, copy) NSString *_Nullable distString;
32+
2833
@property (nonatomic, nullable, copy) NSString *currentScreen;
2934

3035
- (NSArray<SentryBreadcrumb *> *)breadcrumbs;

Sources/Swift/Integrations/WatchdogTerminations/Processors/SentryWatchdogTerminationAttributesProcessor.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ import Foundation
3636
}
3737
}
3838

39+
public func setDist(_ dist: String?) {
40+
setData(data: dist, field: .dist) { [weak self] data in
41+
self?.scopePersistentStore.writeDistToDisk(dist: data)
42+
}
43+
}
44+
45+
public func setEnvironment(_ environment: String?) {
46+
setData(data: environment, field: .environment) { [weak self] data in
47+
self?.scopePersistentStore.writeEnvironmentToDisk(environment: data)
48+
}
49+
}
50+
3951
// MARK: - Private
4052
private func setData<T>(data: T?, field: SentryScopeField, save: @escaping (T) -> Void) {
4153
SentrySDKLog.debug("Setting \(field.name) in background queue: \(String(describing: data))")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MARK: - Strings
2+
extension SentryScopePersistentStore {
3+
func encode(string: String) -> Data? {
4+
return string.data(using: .utf8)
5+
}
6+
7+
func decodeString(from data: Data) -> String? {
8+
return String(data: data, encoding: .utf8)
9+
}
10+
}

Sources/Swift/Persistence/SentryScopePersistentStore.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
enum SentryScopeField: UInt, CaseIterable {
55
case context
66
case user
7+
case dist
8+
case environment
79

810
var name: String {
911
switch self {
1012
case .context:
1113
return "context"
1214
case .user:
1315
return "user"
16+
case .dist:
17+
return "dist"
18+
case .environment:
19+
return "environment"
1420
}
1521
}
1622
}
@@ -82,6 +88,30 @@ enum SentryScopeField: UInt, CaseIterable {
8288
writeFieldToDisk(field: .user, data: encode(user: user))
8389
}
8490

91+
// MARK: - Dist
92+
@objc
93+
public func readPreviousDistFromDisk() -> String? {
94+
readFieldFromDisk(field: .dist) { data in
95+
decodeString(from: data)
96+
}
97+
}
98+
99+
func writeDistToDisk(dist: String) {
100+
writeFieldToDisk(field: .dist, data: encode(string: dist))
101+
}
102+
103+
// MARK: - User
104+
@objc
105+
public func readPreviousEnvironmentFromDisk() -> String? {
106+
readFieldFromDisk(field: .environment) { data in
107+
decodeString(from: data)
108+
}
109+
}
110+
111+
func writeEnvironmentToDisk(environment: String) {
112+
writeFieldToDisk(field: .environment, data: encode(string: environment))
113+
}
114+
85115
// MARK: - Private Functions
86116

87117
private func moveCurrentFileToPreviousFile(field: SentryScopeField) {

0 commit comments

Comments
 (0)