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 25, 2020
1 parent 6a4b76a commit 78cff21
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 20 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
7 changes: 4 additions & 3 deletions Sources/Datadog/Core/Attributes/UserInfoProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ 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, extraInfo: [:])

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 } }
}
}

Expand All @@ -25,4 +25,5 @@ internal struct UserInfo {
let id: String?
let name: String?
let email: String?
let extraInfo: [AttributeKey: AttributeValue]
}
10 changes: 8 additions & 2 deletions Sources/Datadog/Datadog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ 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.value = UserInfo(
id: id,
name: name,
email: email,
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 userInfoAttributes = userInfoProvider.value.extraInfo
var mergedAttributes = attributes
mergedAttributes.userAttributes.merge(userInfoAttributes) { logAttribute, _ in logAttribute }
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
13 changes: 11 additions & 2 deletions Sources/Datadog/RUM/RUMEvent/RUMEventBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@

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)
let userInfoAttributes = userInfoProvider.value.extraInfo
var mergedAttributes = attributes
mergedAttributes.merge(userInfoAttributes) { eventAttribute, _ in eventAttribute }
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
12 changes: 7 additions & 5 deletions Sources/Datadog/Tracing/Span/SpanBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ internal struct SpanBuilder {
func createSpan(from ddspan: DDSpan, finishTime: Date) -> Span {
let tagsReducer = SpanTagsReducer(spanTags: ddspan.tags, logFields: ddspan.logFields)

var jsonStringEncodedTags: [String: JSONStringEncodableValue] = [:]

// First, add baggage items as tags...
var jsonStringEncodedTags: [String: JSONStringEncodableValue]
// 1. add user info attributes as tags...
jsonStringEncodedTags = userInfoProvider.value.extraInfo.compactMapValues {
JSONStringEncodableValue($0, encodedUsing: tagsJSONEncoder)
}
// 2. add baggage items as tags
for (itemKey, itemValue) in ddspan.ddContext.baggageItems.all {
jsonStringEncodedTags[itemKey] = JSONStringEncodableValue(itemValue, encodedUsing: tagsJSONEncoder)
}

// ... then, add regular tags
// 3. add regular tags
for (tagName, tagValue) in tagsReducer.reducedSpanTags {
jsonStringEncodedTags[tagName] = JSONStringEncodableValue(tagValue, encodedUsing: tagsJSONEncoder)
}
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 78cff21

Please sign in to comment.