Skip to content

Commit d132d3d

Browse files
committed
Use Baggage directly in Tracer API
1 parent f8a14a6 commit d132d3d

File tree

6 files changed

+22
-32
lines changed

6 files changed

+22
-32
lines changed

Sources/Tracing/NoOpTracer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import BaggageContext
14+
import Baggage
1515
import Instrumentation
1616

1717
/// No operation Tracer, used when no tracing is required.
1818
public struct NoOpTracer: Tracer {
1919
public func startSpan(
2020
named operationName: String,
21-
context: BaggageContext,
21+
baggage: Baggage,
2222
ofKind kind: SpanKind,
2323
at timestamp: Timestamp
2424
) -> Span {
25-
return NoOpSpan(baggage: context.baggage)
25+
return NoOpSpan(baggage: baggage)
2626
}
2727

2828
public func forceFlush() {}

Sources/Tracing/Tracer.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import BaggageContext
14+
import Baggage
1515
import Instrumentation
1616

1717
/// An `Instrument` with added functionality for distributed tracing. Is uses the span-based tracing model and is
1818
/// based on the OpenTracing/OpenTelemetry spec.
1919
public protocol Tracer: Instrument {
20-
/// Start a new `Span` within the given `BaggageContext` at a given timestamp.
20+
/// Start a new `Span` with the given `Baggage` at a given timestamp.
21+
///
2122
/// - Parameters:
2223
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
23-
/// - context: The carrier of a `BaggageContext` within to start the new `Span`.
24+
/// - baggage: The `Baggage` providing information on where to start the new `Span`.
2425
/// - kind: The `SpanKind` of the new `Span`.
2526
/// - timestamp: The `DispatchTime` at which to start the new `Span`.
2627
func startSpan(
2728
named operationName: String,
28-
context: BaggageContext,
29+
baggage: Baggage,
2930
ofKind kind: SpanKind,
3031
at timestamp: Timestamp
3132
) -> Span
@@ -40,19 +41,17 @@ public protocol Tracer: Instrument {
4041
}
4142

4243
extension Tracer {
43-
/// Start a new `Span` within the given `BaggageContext`. This passes `nil` as the timestamp to the tracer, which
44-
/// usually means it should default to the current timestamp.
44+
/// Start a new `Span` with the given `Baggage` starting at `Timestamp.now()`.
45+
///
4546
/// - Parameters:
4647
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
4748
/// - context: The carrier of a `BaggageContext` within to start the new `Span`.
4849
/// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
49-
/// - timestamp: The `DispatchTime` at which to start the new `Span`. Defaults to `.now()`.
5050
public func startSpan(
5151
named operationName: String,
52-
context: BaggageContext,
53-
ofKind kind: SpanKind = .internal,
54-
at timestamp: Timestamp = .now()
52+
baggage: Baggage,
53+
ofKind kind: SpanKind = .internal
5554
) -> Span {
56-
return self.startSpan(named: operationName, context: context, ofKind: kind, at: timestamp)
55+
return self.startSpan(named: operationName, baggage: baggage, ofKind: kind, at: .now())
5756
}
5857
}

Tests/TracingTests/TestTracer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import BaggageContext
14+
import Baggage
1515
import Foundation
1616
import Instrumentation
1717
import Tracing
@@ -21,14 +21,14 @@ final class TestTracer: Tracer {
2121

2222
func startSpan(
2323
named operationName: String,
24-
context: BaggageContext,
24+
baggage: Baggage,
2525
ofKind kind: SpanKind,
2626
at timestamp: Timestamp
2727
) -> Span {
2828
let span = TestSpan(
2929
operationName: operationName,
3030
startTimestamp: timestamp,
31-
baggage: context.baggage,
31+
baggage: baggage,
3232
kind: kind
3333
) { _ in }
3434
self.spans.append(span)

Tests/TracingTests/TracedLock.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ final class TracedLock {
3030
func lock(baggage: Baggage) {
3131
// time here
3232
self.underlyingLock.lock()
33-
self.activeSpan = InstrumentationSystem.tracer.startSpan(
34-
named: self.name,
35-
context: DefaultContext(baggage: baggage, logger: .init(label: "test"))
36-
)
33+
self.activeSpan = InstrumentationSystem.tracer.startSpan(named: self.name, baggage: baggage)
3734
}
3835

3936
func unlock(baggage: Baggage) {

Tests/TracingTests/TracedLockTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import BaggageContext
14+
import Baggage
1515
@testable import Instrumentation
1616
import Tracing
1717
import XCTest
@@ -60,15 +60,15 @@ enum TaskIDKey: Baggage.Key {
6060
private final class TracedLockPrintlnTracer: Tracer {
6161
func startSpan(
6262
named operationName: String,
63-
context: BaggageContext,
63+
baggage: Baggage,
6464
ofKind kind: SpanKind,
6565
at timestamp: Timestamp
6666
) -> Span {
6767
return TracedLockPrintlnSpan(
6868
operationName: operationName,
6969
startTimestamp: timestamp,
7070
kind: kind,
71-
baggage: context.baggage
71+
baggage: baggage
7272
)
7373
}
7474

Tests/TracingTests/TracerTests.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ struct FakeHTTPServer {
9696
var baggage = Baggage.topLevel
9797
InstrumentationSystem.instrument.extract(request.headers, into: &baggage, using: HTTPHeadersExtractor())
9898

99-
let span = tracer.startSpan(
100-
named: "GET \(request.path)",
101-
context: DefaultContext(baggage: baggage, logger: .init(label: "test"))
102-
)
99+
let span = tracer.startSpan(named: "GET \(request.path)", baggage: baggage)
103100

104101
let response = self.catchAllHandler(span.baggage, request, self.client)
105102
span.attributes["http.status"] = .int(response.status)
@@ -115,10 +112,7 @@ final class FakeHTTPClient {
115112

116113
func performRequest(_ baggage: Baggage, request: FakeHTTPRequest) {
117114
var request = request
118-
let span = InstrumentationSystem.tracer.startSpan(
119-
named: "GET \(request.path)",
120-
context: DefaultContext(baggage: baggage, logger: .init(label: "test")), ofKind: .client
121-
)
115+
let span = InstrumentationSystem.tracer.startSpan(named: "GET \(request.path)", baggage: baggage)
122116
self.baggages.append(span.baggage)
123117
InstrumentationSystem.instrument.inject(baggage, into: &request.headers, using: HTTPHeadersInjector())
124118
span.end()

0 commit comments

Comments
 (0)