Skip to content

Commit

Permalink
RUMM-886 User.ExtraInfo added
Browse files Browse the repository at this point in the history
  • Loading branch information
buranmert committed Nov 20, 2020
1 parent f119262 commit ce3ada5
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Datadog/Example/ExampleAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ExampleAppDelegate: UIResponder, UIApplicationDelegate {
)

// Set user information
Datadog.setUserInfo(id: "abcd-1234", name: "foo", email: "foo@example.com")
Datadog.setUserInfo(id: "abcd-1234", name: "foo", email: "foo@example.com", extraInfo: ["key-extraUserInfo": "value-extraUserInfo"])

// Create Logger
logger = Logger.builder
Expand Down
21 changes: 18 additions & 3 deletions Sources/Datadog/Core/Attributes/UserInfoProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,26 @@ internal class UserInfoProvider {
/// `UserInfo` can be mutated by any user thread with `Datadog.setUserInfo(id:name:email:)` - at the same
/// time it might be accessed by different queues running in the SDK.
private let queue = DispatchQueue(label: "com.datadoghq.user-info-provider", qos: .userInteractive)
private var current = UserInfo(id: nil, name: nil, email: nil)
private var _value = UserInfo(id: nil, name: nil, email: nil)
private var _extraInfo = [AttributeKey: AttributeValue]()

var value: UserInfo {
set { queue.async { self.current = newValue } }
get { queue.sync { self.current } }
set { queue.async { self._value = newValue } }
get { queue.sync { self._value } }
}

var extraInfo: [AttributeKey: AttributeValue] {
set {
queue.async {
var processedInfo = newValue
newValue.keys.forEach {
let value = processedInfo.removeValue(forKey: $0)
processedInfo["usr.\($0)"] = value
}
self._extraInfo = processedInfo
}
}
get { queue.sync { self._extraInfo } }
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Datadog/Datadog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ public class Datadog {
public static func setUserInfo(
id: String? = nil,
name: String? = nil,
email: String? = nil
email: String? = nil,
extraInfo: [AttributeKey: AttributeValue] = [:]
) {
instance?.userInfoProvider.value = UserInfo(id: id, name: name, email: email)
instance?.userInfoProvider.extraInfo = extraInfo
}

// MARK: - Internal
Expand Down
5 changes: 4 additions & 1 deletion Sources/Datadog/Logging/Log/LogBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ internal struct LogBuilder {
let carrierInfoProvider: CarrierInfoProviderType?

func createLogWith(level: LogLevel, message: String, date: Date, attributes: LogAttributes, tags: Set<String>) -> Log {
let extraUserInfo = userInfoProvider.extraInfo
var mergedAttributes = attributes
mergedAttributes.userAttributes.merge(extraUserInfo) { userAttr, _ in userAttr }
return Log(
date: date,
status: logStatus(for: level),
Expand All @@ -37,7 +40,7 @@ internal struct LogBuilder {
userInfo: userInfoProvider.value,
networkConnectionInfo: networkConnectionInfoProvider?.current,
mobileCarrierInfo: carrierInfoProvider?.current,
attributes: attributes,
attributes: mergedAttributes,
tags: !tags.isEmpty ? Array(tags) : nil
)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/Logging/LogOutputs/LogOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation

internal struct LogAttributes {
/// Log attributes received from the user. They are subject for sanitization.
let userAttributes: [String: Encodable]
var userAttributes: [String: Encodable]
/// Log attributes added internally by the SDK. They are not a subject for sanitization.
let internalAttributes: [String: Encodable]?
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/Datadog/RUM/RUMEvent/RUMEventBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@

import Foundation

internal struct RUMEventBuilder {
internal class RUMEventBuilder {
let userInfoProvider: UserInfoProvider

init(userInfoProvider: UserInfoProvider) {
self.userInfoProvider = userInfoProvider
}

func createRUMEvent<DM: RUMDataModel>(with model: DM, attributes: [String: Encodable]) -> RUMEvent<DM> {
return RUMEvent(model: model, attributes: attributes)
var mergedAttributes = attributes
mergedAttributes.merge(userInfoProvider.extraInfo) { userAttr, _ in userAttr }
return RUMEvent(model: model, attributes: mergedAttributes)
}
}
2 changes: 1 addition & 1 deletion Sources/Datadog/RUMMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public class RUMMonitor: DDRUMMonitor, RUMCommandSubscriber {
networkConnectionInfoProvider: rumFeature.networkConnectionInfoProvider,
carrierInfoProvider: rumFeature.carrierInfoProvider
),
eventBuilder: RUMEventBuilder(),
eventBuilder: RUMEventBuilder(userInfoProvider: rumFeature.userInfoProvider),
eventOutput: RUMEventFileOutput(
fileWriter: rumFeature.storage.writer
),
Expand Down
5 changes: 4 additions & 1 deletion Sources/Datadog/Tracing/Span/SpanBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ internal struct SpanBuilder {
func createSpan(from ddspan: DDSpan, finishTime: Date) -> Span {
let tagsReducer = SpanTagsReducer(spanTags: ddspan.tags, logFields: ddspan.logFields)

var jsonStringEncodedTags: [String: JSONStringEncodableValue] = [:]
var jsonStringEncodedTags: [String: JSONStringEncodableValue]
jsonStringEncodedTags = userInfoProvider.extraInfo.compactMapValues {
JSONStringEncodableValue($0, encodedUsing: tagsJSONEncoder)
}

// First, add baggage items as tags...
for (itemKey, itemValue) in ddspan.ddContext.baggageItems.all {
Expand Down
4 changes: 2 additions & 2 deletions Tests/DatadogTests/Datadog/Mocks/RUMFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct RUMDataModelMock: RUMDataModel, Equatable {

extension RUMEventBuilder {
static func mockAny() -> RUMEventBuilder {
return RUMEventBuilder()
return RUMEventBuilder(userInfoProvider: UserInfoProvider.mockAny())
}
}

Expand Down Expand Up @@ -323,7 +323,7 @@ extension RUMScopeDependencies {
networkConnectionInfoProvider: NetworkConnectionInfoProviderMock(networkConnectionInfo: nil),
carrierInfoProvider: CarrierInfoProviderMock(carrierInfo: nil)
),
eventBuilder: RUMEventBuilder = RUMEventBuilder(),
eventBuilder: RUMEventBuilder = RUMEventBuilder(userInfoProvider: UserInfoProvider.mockAny()),
eventOutput: RUMEventOutput = RUMEventOutputMock(),
rumUUIDGenerator: RUMUUIDGenerator = DefaultRUMUUIDGenerator()
) -> RUMScopeDependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest

class RUMEventBuilderTests: XCTestCase {
func testItBuildsRUMEvent() {
let builder = RUMEventBuilder()
let builder = RUMEventBuilder(userInfoProvider: UserInfoProvider.mockAny())
let event = builder.createRUMEvent(
with: RUMDataModelMock(attribute: "foo"),
attributes: ["foo": "bar", "fizz": "buzz"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RUMEventFileOutputTests: XCTestCase {
func testItWritesRUMEventToFileAsJSON() throws {
let fileCreationDateProvider = RelativeDateProvider(startingFrom: .mockDecember15th2019At10AMUTC())
let queue = DispatchQueue(label: "com.datadohq.testItWritesRUMEventToFileAsJSON")
let builder = RUMEventBuilder()
let builder = RUMEventBuilder(userInfoProvider: UserInfoProvider.mockAny())
let output = RUMEventFileOutput(
fileWriter: FileWriter(
dataFormat: RUMFeature.dataFormat,
Expand Down

0 comments on commit ce3ada5

Please sign in to comment.