Skip to content

Commit fad87d5

Browse files
authored
feat: Add Scope attributes into log (#6834)
* feat: Add attributes data to `SentryScope` * Update changelog * Update sdk_api.json * feat: Add Scope atributes into log * Update changelog * Undo changes from merge conflict * Fix `SentryScope.m`
1 parent 7f6a9a5 commit fad87d5

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Add attributes data to `SentryScope` (#6830)
8+
- Add `SentryScope` attributes into log messages (#6834)
89

910
## 9.0.0
1011

Sources/Swift/Tools/SentryLogBatcher.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import Foundation
9090
addDeviceAttributes(to: &log.attributes, scope: scope)
9191
addUserAttributes(to: &log.attributes, scope: scope)
9292
addReplayAttributes(to: &log.attributes, scope: scope)
93+
addScopeAttributes(to: &log.attributes, scope: scope)
9394

9495
let propagationContextTraceIdString = scope.propagationContextTraceIdString
9596
log.traceId = SentryId(uuidString: propagationContextTraceIdString)
@@ -187,6 +188,13 @@ import Foundation
187188
#endif
188189
#endif
189190
}
191+
192+
private func addScopeAttributes(to attributes: inout [String: SentryLog.Attribute], scope: Scope) {
193+
// Scope attributes should not override any existing attribute in the log
194+
for (key, value) in scope.attributes where attributes[key] == nil {
195+
attributes[key] = .init(value: value)
196+
}
197+
}
190198

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

Tests/SentryTests/SentryLogBatcherTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,47 @@ final class SentryLogBatcherTests: XCTestCase {
494494
XCTAssertNil(attributes["device.family"])
495495
}
496496

497+
func testAddLog_AddsScopeAttributes() throws {
498+
let scope = Scope()
499+
scope.setAttribute(value: "aString", key: "string-attribute")
500+
scope.setAttribute(value: false, key: "bool-attribute")
501+
scope.setAttribute(value: 1.765, key: "double-attribute")
502+
scope.setAttribute(value: 5, key: "integer-attribute")
503+
504+
let log = createTestLog(body: "Test log message with user")
505+
sut.addLog(log, scope: scope)
506+
sut.captureLogs()
507+
508+
let capturedLogs = testDelegate.getCapturedLogs()
509+
let capturedLog = try XCTUnwrap(capturedLogs.first)
510+
let attributes = capturedLog.attributes
511+
512+
XCTAssertEqual(attributes["string-attribute"]?.value as? String, "aString")
513+
XCTAssertEqual(attributes["string-attribute"]?.type, "string")
514+
XCTAssertEqual(attributes["bool-attribute"]?.value as? Bool, false)
515+
XCTAssertEqual(attributes["bool-attribute"]?.type, "boolean")
516+
XCTAssertEqual(attributes["double-attribute"]?.value as? Double, 1.765)
517+
XCTAssertEqual(attributes["double-attribute"]?.type, "double")
518+
XCTAssertEqual(attributes["integer-attribute"]?.value as? Int, 5)
519+
XCTAssertEqual(attributes["integer-attribute"]?.type, "integer")
520+
}
521+
522+
func testAddLog_ScopeAttributesDoNotOverrideLogAttribute() throws {
523+
let scope = Scope()
524+
scope.setAttribute(value: true, key: "log-attribute")
525+
526+
let log = createTestLog(body: "Test log message with user", attributes: [ "log-attribute": .init(value: false)])
527+
sut.addLog(log, scope: scope)
528+
sut.captureLogs()
529+
530+
let capturedLogs = testDelegate.getCapturedLogs()
531+
let capturedLog = try XCTUnwrap(capturedLogs.first)
532+
let attributes = capturedLog.attributes
533+
534+
XCTAssertEqual(attributes["log-attribute"]?.value as? Bool, false)
535+
XCTAssertEqual(attributes["log-attribute"]?.type, "boolean")
536+
}
537+
497538
// MARK: - Replay Attributes Tests
498539

499540
#if canImport(UIKit) && !SENTRY_NO_UIKIT

0 commit comments

Comments
 (0)