Skip to content

Commit

Permalink
Merge pull request #527 from DataDog/xgouchet/RUMM-1434/support_bridg…
Browse files Browse the repository at this point in the history
…e_traces

RUMM-1434 read resource span_id and trace_id from attributes
  • Loading branch information
xgouchet authored Jun 30, 2021
2 parents 5e3fb26 + 75eba16 commit aeca66b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMResourceScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ internal class RUMResourceScope: RUMScope {
let resourceDuration: TimeInterval
let size: Int64?

// Check trace attributes
let traceId = (attributes.removeValue(forKey: "_dd.trace_id") as? String) ?? spanContext?.traceID
let spanId = (attributes.removeValue(forKey: "_dd.span_id") as? String) ?? spanContext?.spanID

/// Metrics values take precedence over other values.
if let metrics = resourceMetrics {
resourceStartTime = metrics.fetch.start
Expand All @@ -126,8 +130,8 @@ internal class RUMResourceScope: RUMScope {

let eventData = RUMResourceEvent(
dd: .init(
spanId: spanContext?.spanID,
traceId: spanContext?.traceID
spanId: spanId,
traceId: traceId
),
action: context.activeUserActionID.flatMap { rumUUID in
.init(id: rumUUID.toRUMDataFormat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,69 @@ class RUMResourceScopeTests: XCTestCase {
XCTAssertEqual(event.model.dd.spanId, "200")
}

func testGivenStartedResource_whenResourceLoadingEnds_itSendsResourceEventWithCustomSpanAndTraceId() throws {
var currentTime: Date = .mockDecember15th2019At10AMUTC()

// Given
let scope = RUMResourceScope.mockWith(
context: context,
dependencies: dependencies,
resourceKey: "/resource/1",
attributes: ["_dd.trace_id": "100", "_dd.span_id": "200"],
startTime: currentTime,
dateCorrection: .zero,
url: "https://foo.com/resource/1",
httpMethod: .post,
isFirstPartyResource: nil,
resourceKindBasedOnRequest: nil,
spanContext: nil
)

currentTime.addTimeInterval(2)

// When
XCTAssertFalse(
scope.process(
command: RUMStopResourceCommand(
resourceKey: "/resource/1",
time: currentTime,
attributes: ["foo": "bar"],
kind: .image,
httpStatusCode: 200,
size: 1_024
)
)
)

// Then
let event = try XCTUnwrap(output.recordedEvents(ofType: RUMEvent<RUMResourceEvent>.self).first)
XCTAssertEqual(event.model.date, Date.mockDecember15th2019At10AMUTC().timeIntervalSince1970.toInt64Milliseconds)
XCTAssertEqual(event.model.application.id, scope.context.rumApplicationID)
XCTAssertEqual(event.model.session.id, scope.context.sessionID.toRUMDataFormat)
XCTAssertEqual(event.model.session.type, .user)
XCTAssertEqual(event.model.view.id, context.activeViewID?.toRUMDataFormat)
XCTAssertEqual(event.model.view.url, "FooViewController")
XCTAssertEqual(event.model.view.name, "FooViewName")
XCTAssertValidRumUUID(event.model.resource.id)
XCTAssertEqual(event.model.resource.type, .image)
XCTAssertEqual(event.model.resource.method, .post)
XCTAssertNil(event.model.resource.provider)
XCTAssertEqual(event.model.resource.url, "https://foo.com/resource/1")
XCTAssertEqual(event.model.resource.statusCode, 200)
XCTAssertEqual(event.model.resource.duration, 2_000_000_000)
XCTAssertEqual(event.model.resource.size, 1_024)
XCTAssertNil(event.model.resource.redirect)
XCTAssertNil(event.model.resource.dns)
XCTAssertNil(event.model.resource.connect)
XCTAssertNil(event.model.resource.ssl)
XCTAssertNil(event.model.resource.firstByte)
XCTAssertNil(event.model.resource.download)
XCTAssertEqual(try XCTUnwrap(event.model.action?.id), context.activeUserActionID?.toRUMDataFormat)
XCTAssertEqual(event.attributes as? [String: String], ["foo": "bar"])
XCTAssertEqual(event.model.dd.traceId, "100")
XCTAssertEqual(event.model.dd.spanId, "200")
}

func testGivenStartedResource_whenResourceLoadingEndsWithError_itSendsErrorEvent() throws {
var currentTime: Date = .mockDecember15th2019At10AMUTC()

Expand Down

0 comments on commit aeca66b

Please sign in to comment.