Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUM-1836 feat(otel-tracer): conform to otel Tracer, SpanBuilder and Span #1611

Merged
merged 13 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,12 @@
3C41693C29FBF4D50042B9D2 /* DatadogWebViewTracking.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3CE119FE29F7BE0100202522 /* DatadogWebViewTracking.framework */; };
3C6C7FDB2B45738C006F5CBC /* OpenTelemetryApi in Frameworks */ = {isa = PBXBuildFile; productRef = 3C6C7FDA2B45738C006F5CBC /* OpenTelemetryApi */; };
3C6C7FDD2B457392006F5CBC /* OpenTelemetryApi in Frameworks */ = {isa = PBXBuildFile; productRef = 3C6C7FDC2B457392006F5CBC /* OpenTelemetryApi */; };
3C6C7FE52B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FDF2B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift */; };
3C6C7FE62B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FDF2B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift */; };
3C6C7FE72B459AAA006F5CBC /* OTelSpan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE02B459AAA006F5CBC /* OTelSpan.swift */; };
3C6C7FE82B459AAA006F5CBC /* OTelSpan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE02B459AAA006F5CBC /* OTelSpan.swift */; };
3C6C7FE92B459AAA006F5CBC /* OTelSpanBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE12B459AAA006F5CBC /* OTelSpanBuilder.swift */; };
3C6C7FEA2B459AAA006F5CBC /* OTelSpanBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE12B459AAA006F5CBC /* OTelSpanBuilder.swift */; };
3C6C7FEB2B459AAA006F5CBC /* OTelTraceId+Datadog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE22B459AAA006F5CBC /* OTelTraceId+Datadog.swift */; };
3C6C7FEC2B459AAA006F5CBC /* OTelTraceId+Datadog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE22B459AAA006F5CBC /* OTelTraceId+Datadog.swift */; };
3C6C7FED2B459AAA006F5CBC /* OTelNoOpSpan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE32B459AAA006F5CBC /* OTelNoOpSpan.swift */; };
3C6C7FEE2B459AAA006F5CBC /* OTelNoOpSpan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE32B459AAA006F5CBC /* OTelNoOpSpan.swift */; };
3C6C7FEF2B459AAA006F5CBC /* OTelSpanId+Datadog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE42B459AAA006F5CBC /* OTelSpanId+Datadog.swift */; };
3C6C7FF02B459AAA006F5CBC /* OTelSpanId+Datadog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FE42B459AAA006F5CBC /* OTelSpanId+Datadog.swift */; };
3C6C7FFB2B459AF6006F5CBC /* OTelSpanId+DatadogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6C7FF22B459AB3006F5CBC /* OTelSpanId+DatadogTests.swift */; };
Expand All @@ -60,6 +56,10 @@
3C85D42C29F7C87D00AFF894 /* HostsSanitizerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C85D42B29F7C87D00AFF894 /* HostsSanitizerMock.swift */; };
3C85D42D29F7C87D00AFF894 /* HostsSanitizerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C85D42B29F7C87D00AFF894 /* HostsSanitizerMock.swift */; };
3C9C6BB429F7C0C000581C43 /* DatadogInternal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D23039A5298D513C001A1FA3 /* DatadogInternal.framework */; };
3CB012DD2B482E0400557951 /* NOPOTelSpan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CB012DB2B482E0400557951 /* NOPOTelSpan.swift */; };
3CB012DE2B482E0400557951 /* NOPOTelSpan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CB012DB2B482E0400557951 /* NOPOTelSpan.swift */; };
3CB012DF2B482E0400557951 /* NOPOTelSpanBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CB012DC2B482E0400557951 /* NOPOTelSpanBuilder.swift */; };
3CB012E02B482E0400557951 /* NOPOTelSpanBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CB012DC2B482E0400557951 /* NOPOTelSpanBuilder.swift */; };
3CB32AD42ACB733000D602ED /* URLSessionSwizzler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CB32AD32ACB733000D602ED /* URLSessionSwizzler.swift */; };
3CB32AD52ACB733000D602ED /* URLSessionSwizzler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CB32AD32ACB733000D602ED /* URLSessionSwizzler.swift */; };
3CB32AD72ACB735600D602ED /* URLSessionSwizzlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CB32AD62ACB735600D602ED /* URLSessionSwizzlerTests.swift */; };
Expand Down Expand Up @@ -1900,17 +1900,17 @@
3C2206F22AB9CE9300DE780C /* MetaTypeExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetaTypeExtensions.swift; sourceTree = "<group>"; };
3C394EF62AA5F49F008F48BA /* URLSessionDataDelegateSwizzler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionDataDelegateSwizzler.swift; sourceTree = "<group>"; };
3C394EF92AA5F4C8008F48BA /* URLSessionDataDelegateSwizzlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionDataDelegateSwizzlerTests.swift; sourceTree = "<group>"; };
3C6C7FDF2B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OTelNoOpSpanBuilder.swift; sourceTree = "<group>"; };
3C6C7FE02B459AAA006F5CBC /* OTelSpan.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OTelSpan.swift; sourceTree = "<group>"; };
3C6C7FE12B459AAA006F5CBC /* OTelSpanBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OTelSpanBuilder.swift; sourceTree = "<group>"; };
3C6C7FE22B459AAA006F5CBC /* OTelTraceId+Datadog.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "OTelTraceId+Datadog.swift"; sourceTree = "<group>"; };
3C6C7FE32B459AAA006F5CBC /* OTelNoOpSpan.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OTelNoOpSpan.swift; sourceTree = "<group>"; };
3C6C7FE42B459AAA006F5CBC /* OTelSpanId+Datadog.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "OTelSpanId+Datadog.swift"; sourceTree = "<group>"; };
3C6C7FF22B459AB3006F5CBC /* OTelSpanId+DatadogTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "OTelSpanId+DatadogTests.swift"; sourceTree = "<group>"; };
3C6C7FF32B459AB3006F5CBC /* OTelTraceId+DatadogTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "OTelTraceId+DatadogTests.swift"; sourceTree = "<group>"; };
3C6C7FF42B459AB3006F5CBC /* OTelSpanTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OTelSpanTests.swift; sourceTree = "<group>"; };
3C85D41429F7C59C00AFF894 /* WebViewTracking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewTracking.swift; sourceTree = "<group>"; };
3C85D42B29F7C87D00AFF894 /* HostsSanitizerMock.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HostsSanitizerMock.swift; sourceTree = "<group>"; };
3CB012DB2B482E0400557951 /* NOPOTelSpan.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NOPOTelSpan.swift; sourceTree = "<group>"; };
3CB012DC2B482E0400557951 /* NOPOTelSpanBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NOPOTelSpanBuilder.swift; sourceTree = "<group>"; };
3CB32AD32ACB733000D602ED /* URLSessionSwizzler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionSwizzler.swift; sourceTree = "<group>"; };
3CB32AD62ACB735600D602ED /* URLSessionSwizzlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionSwizzlerTests.swift; sourceTree = "<group>"; };
3CBDE66D2AA08BF600F6A7B6 /* URLSessionTaskDelegateSwizzler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionTaskDelegateSwizzler.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3114,11 +3114,11 @@
3C6C7FDE2B459AAA006F5CBC /* OpenTelemetry */ = {
isa = PBXGroup;
children = (
3C6C7FDF2B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift */,
3CB012DB2B482E0400557951 /* NOPOTelSpan.swift */,
3CB012DC2B482E0400557951 /* NOPOTelSpanBuilder.swift */,
3C6C7FE02B459AAA006F5CBC /* OTelSpan.swift */,
3C6C7FE12B459AAA006F5CBC /* OTelSpanBuilder.swift */,
3C6C7FE22B459AAA006F5CBC /* OTelTraceId+Datadog.swift */,
3C6C7FE32B459AAA006F5CBC /* OTelNoOpSpan.swift */,
3C6C7FE42B459AAA006F5CBC /* OTelSpanId+Datadog.swift */,
);
path = OpenTelemetry;
Expand Down Expand Up @@ -8327,9 +8327,8 @@
D2C1A50C29C4C4CB00946C31 /* DDNoOps.swift in Sources */,
D2C1A4FC29C4C4CB00946C31 /* RequestBuilder.swift in Sources */,
D2C1A50D29C4C4CB00946C31 /* SpanTagsReducer.swift in Sources */,
3CB012DD2B482E0400557951 /* NOPOTelSpan.swift in Sources */,
D2C1A51A29C4C5DD00946C31 /* JSONEncoder.swift in Sources */,
3C6C7FED2B459AAA006F5CBC /* OTelNoOpSpan.swift in Sources */,
3C6C7FE52B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift in Sources */,
D2C1A51829C4C53F00946C31 /* OTSpan.swift in Sources */,
D2C1A51429C4C53F00946C31 /* OTSpanContext.swift in Sources */,
3C6C7FEB2B459AAA006F5CBC /* OTelTraceId+Datadog.swift in Sources */,
Expand All @@ -8340,6 +8339,7 @@
61A2CC362A44B0A20000FF25 /* TraceConfiguration.swift in Sources */,
61A2CC392A44B0EA0000FF25 /* Trace.swift in Sources */,
D2C1A50029C4C4CB00946C31 /* ActiveSpansPool.swift in Sources */,
3CB012DF2B482E0400557951 /* NOPOTelSpanBuilder.swift in Sources */,
D2C1A50929C4C4CB00946C31 /* SpanEventEncoder.swift in Sources */,
3C6C7FE72B459AAA006F5CBC /* OTelSpan.swift in Sources */,
D2C1A4FE29C4C4CB00946C31 /* SpanEventMapper.swift in Sources */,
Expand Down Expand Up @@ -8525,9 +8525,8 @@
D2C1A53929C4F2DF00946C31 /* DDNoOps.swift in Sources */,
D2C1A53A29C4F2DF00946C31 /* RequestBuilder.swift in Sources */,
D2C1A53B29C4F2DF00946C31 /* SpanTagsReducer.swift in Sources */,
3CB012DE2B482E0400557951 /* NOPOTelSpan.swift in Sources */,
D2C1A53C29C4F2DF00946C31 /* JSONEncoder.swift in Sources */,
3C6C7FEE2B459AAA006F5CBC /* OTelNoOpSpan.swift in Sources */,
3C6C7FE62B459AAA006F5CBC /* OTelNoOpSpanBuilder.swift in Sources */,
D2C1A53D29C4F2DF00946C31 /* OTSpan.swift in Sources */,
D2C1A53E29C4F2DF00946C31 /* OTSpanContext.swift in Sources */,
3C6C7FEC2B459AAA006F5CBC /* OTelTraceId+Datadog.swift in Sources */,
Expand All @@ -8538,6 +8537,7 @@
61A2CC372A44B0A20000FF25 /* TraceConfiguration.swift in Sources */,
61A2CC3A2A44B0EA0000FF25 /* Trace.swift in Sources */,
D2C1A54229C4F2DF00946C31 /* ActiveSpansPool.swift in Sources */,
3CB012E02B482E0400557951 /* NOPOTelSpanBuilder.swift in Sources */,
D2C1A54329C4F2DF00946C31 /* SpanEventEncoder.swift in Sources */,
3C6C7FE82B459AAA006F5CBC /* OTelSpan.swift in Sources */,
D2C1A54429C4F2DF00946C31 /* SpanEventMapper.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion DatadogTrace/Sources/DDNoOps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal class DDNoopTracer: OTTracer, OpenTelemetryApi.Tracer {

func spanBuilder(spanName: String) -> OpenTelemetryApi.SpanBuilder {
warn()
return OTelNoOpSpanBuilder()
return NOPOTelSpanBuilder()
}
}

Expand Down
2 changes: 1 addition & 1 deletion DatadogTrace/Sources/DatadogTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation
import DatadogInternal
import OpenTelemetryApi

internal class DatadogTracer: OTTracer, OpenTelemetryApi.Tracer {
internal final class DatadogTracer: OTTracer, OpenTelemetryApi.Tracer {
internal weak var core: DatadogCoreProtocol?

/// Global tags configured for Trace feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation
import OpenTelemetryApi

internal class OTelNoOpSpan: Span {
internal class NOPOTelSpan: Span {
var kind: OpenTelemetryApi.SpanKind = .internal

var name: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import Foundation
import OpenTelemetryApi

internal class OTelNoOpSpanBuilder: SpanBuilder {
internal class NOPOTelSpanBuilder: SpanBuilder {
@discardableResult
func startSpan() -> Span {
return OTelNoOpSpan()
return NOPOTelSpan()
}

@discardableResult
Expand Down
95 changes: 59 additions & 36 deletions DatadogTrace/Sources/OpenTelemetry/OTelSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class OTelSpan: OpenTelemetryApi.Span {
var attributes: [String: OpenTelemetryApi.AttributeValue]
let context: OpenTelemetryApi.SpanContext
var kind: OpenTelemetryApi.SpanKind
let nestedSpan: DDSpan
let ddSpan: DDSpan
let tracer: DatadogTracer
let queue: DispatchQueue

Expand Down Expand Up @@ -50,7 +50,7 @@ internal class OTelSpan: OpenTelemetryApi.Span {
}
_name = newValue
}
nestedSpan.setOperationName(name)
ddSpan.setOperationName(name)
}
}

Expand All @@ -72,7 +72,7 @@ internal class OTelSpan: OpenTelemetryApi.Span {
self.isRecording = true
self.queue = tracer.queue
self.tracer = tracer
self.nestedSpan = .init(
self.ddSpan = .init(
tracer: tracer,
context: .init(
traceID: context.traceId.toDatadog(),
Expand Down Expand Up @@ -109,54 +109,77 @@ internal class OTelSpan: OpenTelemetryApi.Span {
}

func end(time: Date) {
let semaphore = DispatchSemaphore(value: 0)
var ended = false
var tags: [String: String] = [:]

queue.sync {
guard isRecording else {
ended = true
semaphore.signal()
return
}
isRecording = false
tags = makeTags()
semaphore.signal()
}
semaphore.wait()
ganeshnj marked this conversation as resolved.
Show resolved Hide resolved

// Attributes maps to tags in Datadog
for (key, value) in attributes {
switch value {
case .string(let value):
nestedSpan.setTag(key: key, value: value)
case .bool(let value):
nestedSpan.setTag(key: key, value: value.description)
case .int(let value):
nestedSpan.setTag(key: key, value: value.description)
case .double(let value):
nestedSpan.setTag(key: key, value: value.description)
// swiftlint:disable unavailable_function
case .stringArray:
fatalError("Not implemented yet")
case .boolArray:
fatalError("Not implemented yet")
case .intArray:
fatalError("Not implemented yet")
case .doubleArray:
fatalError("Not implemented yet")
case .set:
fatalError("Not implemented yet")
// swiftlint:enable unavailable_function
}
nestedSpan.setTag(key: key, value: value.description)
}
// if the span was already ended before, we don't want to end it again
guard !ended else {
return
}

// There is no need to lock here, because `DDSpan` is thread-safe
for (key, value) in tags {
ddSpan.setTag(key: key, value: value)
}

// SpanKind maps to the `span.kind` tag in Datadog
ddSpan.setTag(key: DatadogTagKeys.spanKind.rawValue, value: kind.rawValue)
ddSpan.finish(at: time)
}

// SpanKind maps to the `span.kind` tag in Datadog
nestedSpan.setTag(key: DatadogTagKeys.spanKind.rawValue, value: kind.rawValue)
private func makeTags() -> [String: String] {
var tags = [String: String]()
for (key, value) in attributes {
switch value {
case .string(let value):
tags[key] = value
case .bool(let value):
tags[key] = value.description
case .int(let value):
tags[key] = value.description
case .double(let value):
tags[key] = value.description
// swiftlint:disable unavailable_function
case .stringArray:
fatalError("Not implemented yet")
case .boolArray:
fatalError("Not implemented yet")
case .intArray:
fatalError("Not implemented yet")
case .doubleArray:
fatalError("Not implemented yet")
case .set:
fatalError("Not implemented yet")
// swiftlint:enable unavailable_function
}
}
nestedSpan.finish(at: time)
return tags
}

var description: String {
return "OTelSpan"
}

func setAttribute(key: String, value: OpenTelemetryApi.AttributeValue?) {
guard isRecording else {
return
}
queue.sync {
guard isRecording else {
return
}

attributes[key] = value
attributes[key] = value
}
}
}
Loading