Skip to content

Commit dae4e5a

Browse files
authored
fix: Fixes user.id not set to installationId if no user is set (#7005)
* fix: Fixes `user.id` not set to installationId if no user is set * Update changelog * Add cached only method to `SentryInstallation` * Fix tests
1 parent 48e0b92 commit dae4e5a

File tree

7 files changed

+58
-1
lines changed

7 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
- Disabled automatic session tracking in system extensions to prevent extension blocking and unwanted dock icon behavior (#6962) (#6962)
1313
- Fixes crash when null values are passed to `UIApplication sendAction:to:from:forEvent:` (#6970)
14+
- Fixes `user.id` not set to installationId if no user is set (#7005)
1415

1516
## 9.0.0
1617

Sources/Sentry/SentryInstallation.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ + (NSString *)installationFilePath:(NSString *)cacheDirectoryPath
8383
return [cacheDirectoryPath stringByAppendingPathComponent:@"INSTALLATION"];
8484
}
8585

86+
+ (nullable NSString *)cachedIdWithCacheDirectoryPath:(NSString *)cacheDirectoryPath
87+
{
88+
@synchronized(self) {
89+
return self.installationStringsByCacheDirectoryPaths[cacheDirectoryPath];
90+
}
91+
}
92+
8693
@end
8794

8895
NS_ASSUME_NONNULL_END

Sources/Sentry/include/SentryInstallation.h

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

1313
+ (void)cacheIDAsyncWithCacheDirectoryPath:(NSString *)cacheDirectoryPath;
1414

15+
+ (nullable NSString *)cachedIdWithCacheDirectoryPath:(NSString *)cacheDirectoryPath;
16+
1517
@end
1618

1719
NS_ASSUME_NONNULL_END

Sources/Sentry/include/SentryPrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#import "SentryDeviceContextKeys.h"
5252
#import "SentryFileIOTrackerHelper.h"
5353
#import "SentryFileManagerHelper.h"
54+
#import "SentryInstallation.h"
5455
#import "SentryMeta.h"
5556
#import "SentryMsgPackSerializer.h"
5657
#import "SentryNSDictionarySanitize.h"

Sources/Swift/Tools/SentryLogBatcher.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import Foundation
9191
addUserAttributes(to: &log.attributes, scope: scope)
9292
addReplayAttributes(to: &log.attributes, scope: scope)
9393
addScopeAttributes(to: &log.attributes, scope: scope)
94+
addDefaultUserIdIfNeeded(to: &log.attributes, scope: scope, options: options)
9495

9596
let propagationContextTraceIdString = scope.propagationContextTraceIdString
9697
log.traceId = SentryId(uuidString: propagationContextTraceIdString)
@@ -195,6 +196,18 @@ import Foundation
195196
attributes[key] = .init(value: value)
196197
}
197198
}
199+
200+
private func addDefaultUserIdIfNeeded(to attributes: inout [String: SentryLog.Attribute], scope: Scope, options: Options) {
201+
guard attributes["user.id"] == nil && attributes["user.name"] == nil && attributes["user.email"] == nil else {
202+
return
203+
}
204+
205+
if let installationId = SentryInstallation.cachedId(withCacheDirectoryPath: options.cacheDirectoryPath) {
206+
// We only want to set the id if the customer didn't set a user so we at least set something to
207+
// identify the user.
208+
attributes["user.id"] = .init(value: installationId)
209+
}
210+
}
198211

199212
// Only ever call this from the serial dispatch queue.
200213
private func encodeAndBuffer(log: SentryLog) {

Tests/SentryTests/SentryLogBatcherTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,9 @@ final class SentryLogBatcherTests: XCTestCase {
414414
XCTAssertNil(attributes["user.email"])
415415
}
416416

417-
func testAddLog_DoesNotAddUserAttributesWhenNoUser() throws {
417+
func testAddLog_NoUserAtributesAreSetIfInstallationIdIsNotCached() throws {
418418
// No user set on scope
419+
// InstallationId not cached
419420

420421
let log = createTestLog(body: "Test log message without user")
421422
sut.addLog(log, scope: scope)
@@ -430,6 +431,25 @@ final class SentryLogBatcherTests: XCTestCase {
430431
XCTAssertNil(attributes["user.email"])
431432
}
432433

434+
func testAddLog_OnlySetsUserIdToInstallationIdWhenNoUserIsSet() throws {
435+
// No user set on scope
436+
// Create and cache installationId
437+
_ = SentryInstallation.id(withCacheDirectoryPath: options.cacheDirectoryPath)
438+
439+
let log = createTestLog(body: "Test log message without user")
440+
sut.addLog(log, scope: scope)
441+
sut.captureLogs()
442+
443+
let capturedLogs = testDelegate.getCapturedLogs()
444+
let capturedLog = try XCTUnwrap(capturedLogs.first)
445+
let attributes = capturedLog.attributes
446+
447+
XCTAssertNotNil(attributes["user.id"])
448+
XCTAssertEqual(attributes["user.id"]?.value as? String, SentryInstallation.id(withCacheDirectoryPath: options.cacheDirectoryPath))
449+
XCTAssertNil(attributes["user.name"])
450+
XCTAssertNil(attributes["user.email"])
451+
}
452+
433453
func testAddLog_AddsOSAndDeviceAttributes() throws {
434454
let osContext = ["name": "iOS", "version": "16.0.1"]
435455
let deviceContext = ["family": "iOS", "model": "iPhone14,4"]

Tests/SentryTests/State/SentryInstallationTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,17 @@ final class SentryInstallationTests: XCTestCase {
7272
XCTAssertEqual(cachedID, nonCachedID)
7373

7474
}
75+
76+
func testCachedIDIsNilWhenNoInstallationIsFound() {
77+
let cachedID = SentryInstallation.cachedId(withCacheDirectoryPath: basePath)
78+
XCTAssertNil(cachedID)
79+
}
80+
81+
func testCachedID_returnsActuallyCachedId() {
82+
let id1 = SentryInstallation.id(withCacheDirectoryPath: basePath)
83+
84+
let cachedID = SentryInstallation.cachedId(withCacheDirectoryPath: basePath)
85+
86+
XCTAssertEqual(id1, cachedID)
87+
}
7588
}

0 commit comments

Comments
 (0)