Skip to content

Commit

Permalink
RUMM-2334 Tracing with RUM Context
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Sep 6, 2022
1 parent aeb3029 commit b274944
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2708,7 +2708,6 @@
isa = PBXGroup;
children = (
61D980B924E28D0100E03345 /* RUMIntegrations.swift */,
6156CB9B24E18224008CB2B2 /* TracingWithRUMIntegration.swift */,
61FC5F4425CC23C9006BB4DE /* RUMWithCrashContextIntegration.swift */,
E11625D727B681D200E428C6 /* CITestIntegration.swift */,
9EB4B860274E79620041CD03 /* WebView */,
Expand Down Expand Up @@ -4227,6 +4226,7 @@
isa = PBXGroup;
children = (
61216275247D1CD700AC5D67 /* TracingWithLoggingIntegration.swift */,
6156CB9B24E18224008CB2B2 /* TracingWithRUMIntegration.swift */,
);
path = Integrations;
sourceTree = "<group>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

internal struct DatadogContext {
public struct DatadogContext {
// MARK: - Datadog Specific

/// [Datadog Site](https://docs.datadoghq.com/getting_started/site/) for data uploads. It can be `nil` in V1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public enum FeatureMessage {
key: String,
attributes: FeatureMessageAttributes
)

case context(DatadogContext)
}

This file was deleted.

21 changes: 10 additions & 11 deletions Sources/Datadog/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class Tracer: OTTracer {
/// Queue ensuring thread-safety of the `Tracer` and `DDSpan` operations.
internal let queue: DispatchQueue
/// Integration with RUM Context. `nil` if disabled for this Tracer or if the RUM feature is disabled.
internal let rumContextIntegration: TracingWithRUMContextIntegration?
internal let rumIntegration: TracingWithRUMIntegration?
/// Integration with Logging.
internal let loggingIntegration: TracingWithLoggingIntegration

Expand Down Expand Up @@ -92,8 +92,7 @@ public class Tracer: OTTracer {
return DDTracer(
core: core,
tracingFeature: tracingFeature,
tracerConfiguration: configuration,
rumEnabled: core.v1.feature(RUMFeature.self) != nil
tracerConfiguration: configuration
)
} catch {
consolePrint("\(error)")
Expand All @@ -104,16 +103,15 @@ public class Tracer: OTTracer {
internal convenience init(
core: DatadogCoreProtocol,
tracingFeature: TracingFeature,
tracerConfiguration: Configuration,
rumEnabled: Bool
tracerConfiguration: Configuration
) {
self.init(
core: core,
configuration: tracerConfiguration,
spanEventMapper: tracingFeature.configuration.spanEventMapper,
tracingUUIDGenerator: tracingFeature.configuration.uuidGenerator,
dateProvider: tracingFeature.configuration.dateProvider,
rumContextIntegration: (rumEnabled && tracerConfiguration.bundleWithRUM) ? TracingWithRUMContextIntegration() : nil,
rumIntegration: tracerConfiguration.bundleWithRUM ? TracingWithRUMIntegration() : nil,
loggingIntegration: TracingWithLoggingIntegration(
core: core,
tracerConfiguration: tracerConfiguration
Expand All @@ -127,7 +125,7 @@ public class Tracer: OTTracer {
spanEventMapper: SpanEventMapper?,
tracingUUIDGenerator: TracingUUIDGenerator,
dateProvider: DateProvider,
rumContextIntegration: TracingWithRUMContextIntegration?,
rumIntegration: TracingWithRUMIntegration?,
loggingIntegration: TracingWithLoggingIntegration
) {
self.core = core
Expand All @@ -140,7 +138,7 @@ public class Tracer: OTTracer {

self.tracingUUIDGenerator = tracingUUIDGenerator
self.dateProvider = dateProvider
self.rumContextIntegration = rumContextIntegration
self.rumIntegration = rumIntegration
self.loggingIntegration = loggingIntegration
}

Expand Down Expand Up @@ -196,10 +194,11 @@ public class Tracer: OTTracer {
internal func startSpan(spanContext: DDSpanContext, operationName: String, tags: [String: Encodable]? = nil, startTime: Date? = nil) -> OTSpan {
var combinedTags = configuration.globalTags ?? [:]
if let userTags = tags {
combinedTags.merge(userTags) { _, last in last }
combinedTags.merge(userTags) { $1 }
}
if let currentRUMContextTags = rumContextIntegration?.currentRUMContextTags {
combinedTags.merge(currentRUMContextTags) { _, last in last }

if let rumTags = rumIntegration?.attribues {
combinedTags.merge(rumTags) { $1 }
}

let span = DDSpan(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/

import Foundation

/// Provides the current RUM context tags for produced `Spans`.
internal final class TracingWithRUMIntegration {
var attribues: [String: Encodable]? {
get { acquire { _attribues } }
set { acquire { _attribues = newValue } }
}

private let lock = NSLock()
private var _attribues: [String: Encodable]?

private func acquire<T>(_ block: () -> T) -> T {
lock.lock()
defer { lock.unlock() }
return block()
}
}
29 changes: 29 additions & 0 deletions Sources/Datadog/Tracing/TracingV2Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,32 @@ internal struct TracingRequestBuilder: FeatureRequestBuilder {
return builder.uploadRequest(with: data)
}
}

internal struct TracingMessageReceiver: FeatureMessageReceiver {
/// Process messages receives from the bus.
///
/// - Parameters:
/// - message: The Feature message
/// - core: The core from which the message is transmitted.
func receive(message: FeatureMessage, from core: DatadogCoreProtocol) -> Bool {
switch message {
case .context(let context):
return update(context: context)
default:
return false
}
}

/// Adds RUM Error with given message and stack to current RUM View.
private func update(context: DatadogContext) -> Bool {
guard
let tracer = Global.sharedTracer as? Tracer,
let integration = tracer.rumIntegration
else {
return false
}

integration.attribues = context.featuresAttributes["rum"]?.all()
return true
}
}
4 changes: 2 additions & 2 deletions Tests/DatadogTests/Datadog/Mocks/TracingFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ extension Tracer {
spanEventMapper: SpanEventMapper? = nil,
tracingUUIDGenerator: TracingUUIDGenerator = DefaultTracingUUIDGenerator(),
dateProvider: DateProvider = SystemDateProvider(),
rumContextIntegration: TracingWithRUMContextIntegration? = nil
rumIntegration: TracingWithRUMIntegration? = nil
) -> Tracer {
return Tracer(
core: core,
configuration: configuration,
spanEventMapper: spanEventMapper,
tracingUUIDGenerator: tracingUUIDGenerator,
dateProvider: dateProvider,
rumContextIntegration: rumContextIntegration,
rumIntegration: rumIntegration,
loggingIntegration: .init(core: core, tracerConfiguration: configuration)
)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/DatadogTests/Datadog/TracerConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ class TracerConfigurationTests: XCTestCase {
XCTAssertNotNil(tracer.core)
XCTAssertNil(tracer.configuration.serviceName)
XCTAssertFalse(tracer.configuration.sendNetworkInfo)
XCTAssertNil(tracer.rumContextIntegration)
XCTAssertNotNil(tracer.rumIntegration)
}

func testDefaultTracerWithRUMEnabled() {
let rum: RUMFeature = .mockNoOp()
core.register(feature: rum)

let tracer1 = Tracer.initialize(configuration: .init(), in: core).dd
XCTAssertNotNil(tracer1.rumContextIntegration)
XCTAssertNotNil(tracer1.rumIntegration)

let tracer2 = Tracer.initialize(configuration: .init(bundleWithRUM: false), in: core).dd
XCTAssertNil(tracer2.rumContextIntegration)
XCTAssertNil(tracer2.rumIntegration)
}

func testCustomizedTracer() throws {
Expand All @@ -61,6 +61,6 @@ class TracerConfigurationTests: XCTestCase {
XCTAssertNotNil(tracer.core)
XCTAssertEqual(tracer.configuration.serviceName, "custom-service-name")
XCTAssertTrue(tracer.configuration.sendNetworkInfo)
XCTAssertNil(tracer.rumContextIntegration)
XCTAssertNil(tracer.rumIntegration)
}
}

0 comments on commit b274944

Please sign in to comment.