Skip to content

Commit d60fbab

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #160 from nachoBonafonte/Context-handling-rework
Reworked context handling in the library: Add initial support for implementing a different contextProvider, by default uses a contextProvider based on Activity.framework Removed Scope concept from the library Now context is handled in OpenTelemetry.instance.contextProvider, which uses the desired ContextProvider This rework also implied modifiying the default Baggage, whihch is now an optional set to nil instead of an EmptyBaggage instance, these empty classes are not public anymore. Some more cleaning can be done in Baggage handling, but the PR is big enough Fixes #126, Fixes #154, partially improves #141 Some changes ease development for #38
2 parents 8da92e7 + 88fe6dd commit d60fbab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+373
-442
lines changed

Examples/Datadog Sample/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func simpleSpan() {
7171
func childSpan() {
7272
let span = tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).startSpan()
7373
span.setAttribute(key: sampleKey, value: sampleValue)
74-
OpenTelemetryContext.setActiveSpan(span)
74+
OpenTelemetry.instance.contextProvider.setActiveSpan(span)
7575
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
7676
childSpan.setAttribute(key: sampleKey, value: sampleValue)
7777
childSpan.end()

Examples/Logging Tracer/LoggingSpan.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class LoggingSpan: Span {
2222
var context: SpanContext
2323
var isRecording: Bool = true
2424
var status: Status = .unset
25-
var scope: Scope?
2625

2726
public init(name: String, kind: SpanKind) {
2827
self.name = name

Examples/Logging Tracer/LoggingTracer.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import OpenTelemetryApi
1818

1919
class LoggingTracer: Tracer {
2020
let tracerName = "LoggingTracer"
21-
public var activeSpan: Span? {
22-
return OpenTelemetryContext.activeSpan
23-
}
2421

2522
var binaryFormat: BinaryFormattable = LoggingBinaryFormat()
2623
var textFormat: TextMapPropagator = W3CTraceContextPropagator()
@@ -42,7 +39,7 @@ class LoggingTracer: Tracer {
4239

4340
func startSpan() -> Span {
4441
if spanContext == nil, !isRootSpan {
45-
spanContext = OpenTelemetryContext.activeSpan?.context
42+
spanContext = OpenTelemetry.instance.contextProvider.activeSpan?.context
4643
}
4744
if spanContext != nil {
4845
return LoggingSpan(name: name, kind: .client)

Examples/Logging Tracer/main.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ OpenTelemetry.registerTracerProvider(tracerProvider: LoggingTracerProvider())
2222

2323
var tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "ConsoleApp", instrumentationVersion: "semver:1.0.0")
2424

25-
let scope = OpenTelemetryContext.setActiveSpan(tracer.spanBuilder(spanName: "Main (span1)").startSpan())
25+
26+
let span1 = tracer.spanBuilder(spanName: "Main (span1)").startSpan()
27+
OpenTelemetry.instance.contextProvider.setActiveSpan(span1)
2628
let semaphore = DispatchSemaphore(value: 0)
2729
DispatchQueue.global().async {
28-
var scope2 = OpenTelemetryContext.setActiveSpan(tracer.spanBuilder(spanName: "Main (span2)").startSpan())
29-
OpenTelemetryContext.activeSpan?.setAttribute(key: "myAttribute", value: "myValue")
30+
let span2 = tracer.spanBuilder(spanName: "Main (span2)").startSpan()
31+
OpenTelemetry.instance.contextProvider.setActiveSpan(span2)
32+
OpenTelemetry.instance.contextProvider.activeSpan?.setAttribute(key: "myAttribute", value: "myValue")
3033
sleep(1)
3134
semaphore.signal()
32-
scope2.close()
35+
span2.end()
3336
}
37+
span1.end()
3438

3539
semaphore.wait()

Examples/Simple Exporter/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func simpleSpan() {
3939
func childSpan() {
4040
let span = tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).startSpan()
4141
span.setAttribute(key: sampleKey, value: sampleValue)
42-
OpenTelemetryContext.setActiveSpan(span)
42+
OpenTelemetry.instance.contextProvider.setActiveSpan(span)
4343
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
4444
childSpan.setAttribute(key: sampleKey, value: sampleValue)
4545
childSpan.end()

Sources/OpenTelemetryApi/Baggage/BaggageBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public protocol BaggageBuilder: AnyObject {
2424
/// propagation is used.
2525
/// If called multiple times, only the last specified value will be used.
2626
/// - Parameter parent: the Baggage used as parent
27-
@discardableResult func setParent(_ parent: Baggage) -> Self
27+
@discardableResult func setParent(_ parent: Baggage?) -> Self
2828

2929
/// Sets the option to become a root Baggage with no parent. If not
3030
/// called, the value provided using setParent(Baggage) or otherwise
@@ -43,6 +43,6 @@ public protocol BaggageBuilder: AnyObject {
4343
/// - Parameter key: the EntryKey which will be removed.
4444
@discardableResult func remove(key: EntryKey) -> Self
4545

46-
/// Creates a Baggage from this builder.
46+
/// Creates a Baggage from this builder.
4747
func build() -> Baggage
4848
}

Sources/OpenTelemetryApi/Baggage/BaggageManager.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ import Foundation
2323
/// own subtypes. This means callers cannot assume the getCurrentContext()
2424
/// is the same instance as the one withContext() placed into scope.
2525
public protocol BaggageManager: AnyObject {
26-
/// Returns the current Baggage
27-
func getCurrentBaggage() -> Baggage
28-
2926
/// Returns a new ContextBuilder.
3027
func baggageBuilder() -> BaggageBuilder
31-
32-
/// Enters the scope of code where the given Baggage is in the current context
33-
/// (replacing the previous Baggage) and returns an object that represents that
34-
/// scope. The scope is exited when the returned object is closed.
35-
/// - Parameter baggage: the Baggage to be set as the current context.
36-
func withContext(baggage: Baggage) -> Scope
3728
}

Sources/OpenTelemetryApi/Baggage/DefaultBaggageBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class DefaultBaggageBuilder: BaggageBuilder {
2222

2323
public init() {}
2424

25-
@discardableResult public func setParent(_ parent: Baggage) -> Self {
25+
@discardableResult public func setParent(_ parent: Baggage?) -> Self {
2626
self.parent = parent
2727
return self
2828
}
@@ -60,7 +60,7 @@ public class DefaultBaggageBuilder: BaggageBuilder {
6060
public func build() -> Baggage {
6161
var parentCopy = parent
6262
if parent == nil, !noImplicitParent {
63-
parentCopy = OpenTelemetry.instance.baggageManager.getCurrentBaggage()
63+
parentCopy = OpenTelemetry.instance.contextProvider.activeBaggage
6464
}
6565

6666
var combined = entries

Sources/OpenTelemetryApi/Baggage/DefaultBaggageManager.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ public class DefaultBaggageManager: BaggageManager {
2727
return DefaultBaggageBuilder()
2828
}
2929

30-
public func getCurrentBaggage() -> Baggage {
31-
return OpenTelemetryContext.activeBaggage ?? EmptyBaggage.instance
32-
}
33-
34-
public func withContext(baggage: Baggage) -> Scope {
35-
return OpenTelemetryContext.setActiveBaggage(baggage)
30+
public func getCurrentBaggage() -> Baggage? {
31+
return OpenTelemetry.instance.contextProvider.activeBaggage
3632
}
3733
}

Sources/OpenTelemetryApi/Baggage/EmptyBaggage.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616
import Foundation
1717

1818
/// An immutable implementation of the Baggage that does not contain any entries.
19-
public class EmptyBaggage: Baggage {
19+
class EmptyBaggage: Baggage {
2020
private init() {}
2121

2222
/// Returns the single instance of the EmptyBaggage class.
23-
public static var instance = EmptyBaggage()
23+
static var instance = EmptyBaggage()
2424

25-
public static func baggageBuilder() -> BaggageBuilder {
25+
static func baggageBuilder() -> BaggageBuilder {
2626
return EmptyBaggageBuilder()
2727
}
2828

29-
public func getEntries() -> [Entry] {
29+
func getEntries() -> [Entry] {
3030
return [Entry]()
3131
}
3232

33-
public func getEntryValue(key: EntryKey) -> EntryValue? {
33+
func getEntryValue(key: EntryKey) -> EntryValue? {
3434
return nil
3535
}
3636
}

0 commit comments

Comments
 (0)