Skip to content

Commit

Permalink
RUM-1836 feat(otel-tracer): scaffold conformance to otel Tracer, Span…
Browse files Browse the repository at this point in the history
…Builder and Span
  • Loading branch information
ganeshnj committed Jan 3, 2024
1 parent c487468 commit 87ea047
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 3 deletions.
9 changes: 8 additions & 1 deletion DatadogTrace/Sources/DDNoOps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

import Foundation
import DatadogInternal
import OpenTelemetryApi

internal struct DDNoopGlobals {
static let tracer = DDNoopTracer()
static let span = DDNoopSpan()
static let context = DDNoopSpanContext()
}

internal struct DDNoopTracer: OTTracer {
internal class DDNoopTracer: OTTracer, OpenTelemetryApi.Tracer {
var activeSpan: OTSpan? = nil

private func warn() {
Expand Down Expand Up @@ -44,6 +45,12 @@ internal struct DDNoopTracer: OTTracer {
warn()
return DDNoopGlobals.span
}

// MARK: - Open Telemetry

func spanBuilder(spanName: String) -> OpenTelemetryApi.SpanBuilder {
fatalError("Not implemented")
}
}

internal struct DDNoopSpan: OTSpan {
Expand Down
9 changes: 8 additions & 1 deletion DatadogTrace/Sources/DatadogTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import Foundation
import DatadogInternal
import OpenTelemetryApi

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

/// Global tags configured for Trace feature.
Expand Down Expand Up @@ -156,4 +157,10 @@ internal class DatadogTracer: OTTracer {
forKey: SpanCoreContext.key
)
}

// MARK: - OpenTelemetry

func spanBuilder(spanName: String) -> OpenTelemetryApi.SpanBuilder {
OTelSpanBuilder()
}
}
47 changes: 47 additions & 0 deletions DatadogTrace/Sources/OpenTelemetry/OTelNoOpSpan.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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-Present Datadog, Inc.
*/

import Foundation
import OpenTelemetryApi

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

var name: String = ""

var context: SpanContext = SpanContext.create(
traceId: TraceId.invalid,
spanId: SpanId.invalid,
traceFlags: TraceFlags(),
traceState: TraceState()
)

var isRecording: Bool = false

var status: Status = Status.unset

var description: String = "NoOpSpan"

func updateName(name: String) {}

func setAttribute(key: String, value: OpenTelemetryApi.AttributeValue?) {}

func addEvent(name: String) {}

func addEvent(name: String, timestamp: Date) {}

func addEvent(name: String, attributes: [String: OpenTelemetryApi.AttributeValue]) {}

func addEvent(name: String, attributes: [String: OpenTelemetryApi.AttributeValue], timestamp: Date) {}

func end() {
OpenTelemetry.instance.contextProvider.removeContextForSpan(self)
}

func end(time: Date) {
end()
}
}
50 changes: 50 additions & 0 deletions DatadogTrace/Sources/OpenTelemetry/OTelNoOpSpanBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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-Present Datadog, Inc.
*/

import Foundation
import OpenTelemetryApi

class NoOpSpanBuilder: SpanBuilder {
@discardableResult public func startSpan() -> Span {
return OTelNoOpSpan()
}

@discardableResult public func setParent(_ parent: Span) -> Self {
return self
}

@discardableResult public func setParent(_ parent: SpanContext) -> Self {
return self
}

@discardableResult public func setNoParent() -> Self {
return self
}

@discardableResult public func addLink(spanContext: SpanContext) -> Self {
return self
}

@discardableResult public func addLink(spanContext: SpanContext, attributes: [String: OpenTelemetryApi.AttributeValue]) -> Self {
return self
}

@discardableResult public func setSpanKind(spanKind: SpanKind) -> Self {
return self
}

@discardableResult public func setStartTime(time: Date) -> Self {
return self
}

public func setAttribute(key: String, value: OpenTelemetryApi.AttributeValue) -> Self {
return self
}

func setActive(_ active: Bool) -> Self {
return self
}
}
94 changes: 94 additions & 0 deletions DatadogTrace/Sources/OpenTelemetry/OTelSpan.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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-Present Datadog, Inc.
*/

import Foundation
import OpenTelemetryApi

class OTelSpan: OpenTelemetryApi.Span {
private let tracer: DatadogTracer
private let queue: DispatchQueue
private var _status: OpenTelemetryApi.Status
private var _name: String

init(
context: OpenTelemetryApi.SpanContext,
kind: OpenTelemetryApi.SpanKind,
name: String,
tracer: DatadogTracer
) {
self._name = name
self._status = .unset
self.context = context
self.isRecording = true
self.kind = kind
self.queue = tracer.queue
self.tracer = tracer
}

var kind: OpenTelemetryApi.SpanKind

var context: OpenTelemetryApi.SpanContext

var isRecording: Bool

var status: OpenTelemetryApi.Status {
get {
queue.sync {
_status
}
}
set {
queue.sync {
_status = newValue
}
}
}

var name: String {
get {
queue.sync {
_name
}
}
set {
queue.sync {
_name = newValue
}
}
}

func addEvent(name: String) {
fatalError("Not implemented")
}

func addEvent(name: String, timestamp: Date) {
fatalError("Not implemented")
}

func addEvent(name: String, attributes: [String : OpenTelemetryApi.AttributeValue]) {
fatalError("Not implemented")
}

func addEvent(name: String, attributes: [String : OpenTelemetryApi.AttributeValue], timestamp: Date) {
fatalError("Not implemented")
}

func end() {
fatalError("Not implemented")
}

func end(time: Date) {
fatalError("Not implemented")
}

var description: String {
return "WrapperSpan"
}

func setAttribute(key: String, value: OpenTelemetryApi.AttributeValue?) {
fatalError("Not implemented")
}
}
50 changes: 50 additions & 0 deletions DatadogTrace/Sources/OpenTelemetry/OTelSpanBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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-Present Datadog, Inc.
*/

import Foundation
import OpenTelemetryApi

class OTelSpanBuilder: OpenTelemetryApi.SpanBuilder {
func setParent(_ parent: OpenTelemetryApi.Span) -> Self {
fatalError("Not implemented")
}

func setParent(_ parent: OpenTelemetryApi.SpanContext) -> Self {
fatalError("Not implemented")
}

func setNoParent() -> Self {
fatalError("Not implemented")
}

func addLink(spanContext: OpenTelemetryApi.SpanContext) -> Self {
fatalError("Not implemented")
}

func addLink(spanContext: OpenTelemetryApi.SpanContext, attributes: [String : OpenTelemetryApi.AttributeValue]) -> Self {
fatalError("Not implemented")
}

func setSpanKind(spanKind: OpenTelemetryApi.SpanKind) -> Self {
fatalError("Not implemented")
}

func setStartTime(time: Date) -> Self {
fatalError("Not implemented")
}

func setActive(_ active: Bool) -> Self {
fatalError("Not implemented")
}

func startSpan() -> OpenTelemetryApi.Span {
fatalError("Not implemented")
}

func setAttribute(key: String, value: OpenTelemetryApi.AttributeValue) -> Self {
fatalError("Not implemented")
}
}
3 changes: 2 additions & 1 deletion DatadogTrace/Sources/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import Foundation
import DatadogInternal
import OpenTelemetryApi

/// Datadog - specific span tags to be used with `Tracer.shared().startSpan(operationName:references:tags:startTime:)`
/// and `span.setTag(key:value:)`.
Expand Down Expand Up @@ -49,7 +50,7 @@ public class Tracer {
/// It requires `Trace.enable(with:in:)` to be called first - otherwise it will return no-op implementation.
/// - Parameter core: the instance of Datadog SDK the Trace feature was enabled in (global instance by default)
/// - Returns: the Tracer that conforms to Open Tracing API (`OTTracer`)
public static func shared(in core: DatadogCoreProtocol = CoreRegistry.default) -> OTTracer {
public static func shared(in core: DatadogCoreProtocol = CoreRegistry.default) -> OTTracer & OpenTelemetryApi.Tracer {
do {
guard !(core is NOPDatadogCore) else {
throw ProgrammerError(
Expand Down

0 comments on commit 87ea047

Please sign in to comment.