Skip to content

Commit

Permalink
Use ServiceContext instead of Baggage (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
slashmo authored Jun 4, 2023
1 parent 88a390f commit 4afda7e
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
.library(name: "OtlpGRPCSpanExporting", targets: ["OtlpGRPCSpanExporting"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.0.0-beta.2"),
.package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.1"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.43.0"),
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0"),
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ Our demo application creates two spans: `hello` and `world`. To make things even
the `hello` span:

```swift
let rootSpan = InstrumentationSystem.tracer.startSpan("hello", baggage: .topLevel)
let rootSpan = InstrumentationSystem.tracer.startSpan("hello", context: .topLevel)

sleep(1)
rootSpan.addEvent(SpanEvent(
name: "Discovered the meaning of life",
attributes: ["meaning_of_life": 42]
))

let childSpan = InstrumentationSystem.tracer.startSpan("world", baggage: rootSpan.baggage)
let childSpan = InstrumentationSystem.tracer.startSpan("world", context: rootSpan.context)

sleep(1)
childSpan.end()
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenTelemetry/Logging/MetadataProvider+OTel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//

import InstrumentationBaggage
import Logging
import ServiceContextModule

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension Logger.MetadataProvider {
Expand All @@ -24,7 +24,7 @@ extension Logger.MetadataProvider {
/// - Returns: A metadata provider ready to use with Logging.
public static func otel(traceIDKey: String = "trace-id", spanIDKey: String = "span-id") -> Logger.MetadataProvider {
.init {
guard let spanContext = Baggage.current?.spanContext else { return [:] }
guard let spanContext = ServiceContext.current?.spanContext else { return [:] }
return [
traceIDKey: "\(spanContext.traceID)",
spanIDKey: "\(spanContext.spanID)",
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenTelemetry/Processing/SimpleSpanProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension OTel {
}

public func processEndedSpan(_ span: OTel.RecordedSpan) {
guard span.context.traceFlags.contains(.sampled) else { return }
guard span.spanContext.traceFlags.contains(.sampled) else { return }
_ = exporter.export([span])
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/OpenTelemetry/RecordedSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ extension OTel {
public let status: SpanStatus?

/// The context of this span.
public let context: OTel.SpanContext
public let spanContext: OTel.SpanContext

/// The baggage propagated with this span.
/// The service context propagated with this span.
///
/// - Note: This `Baggage` doesn't contain the `OTel.SpanContext` as that's already unwrapped and accessible through `self.context`.
public let baggage: Baggage
/// - Note: This `ServiceContext` doesn't contain the `OTel.SpanContext` as that's already unwrapped and accessible through `self.context`.
public let context: ServiceContext

/// The absolute time at which this span was started.
public let startTime: UInt64
Expand All @@ -55,18 +55,18 @@ extension OTel {

extension OTel.RecordedSpan {
init?(_ span: OTel.Tracer.Span) {
guard let context = span.baggage.spanContext else { return nil }
guard let spanContext = span.context.spanContext else { return nil }
guard let endTime = span.endTime else { return nil }

operationName = span.operationName
kind = span.kind
status = span.status
self.context = context
self.spanContext = spanContext

// strip span context from baggage because it's already stored as `context`.
var baggage = span.baggage
baggage.spanContext = nil
self.baggage = baggage
// strip span context from service context because it's already stored as `context`.
var context = span.context
context.spanContext = nil
self.context = context

startTime = span.startTime
self.endTime = endTime
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenTelemetry/Sampling/ConstantSampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension OTel {
traceID: OTel.TraceID,
attributes: SpanAttributes,
links: [SpanLink],
parentBaggage: Baggage
parentContext: ServiceContext
) -> OTel.SamplingResult {
OTel.SamplingResult(decision: isOn ? .recordAndSample : .drop, attributes: [:])
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/OpenTelemetry/Sampling/ParentBasedSampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ extension OTel {
traceID: OTel.TraceID,
attributes: SpanAttributes,
links: [SpanLink],
parentBaggage: Baggage
parentContext: ServiceContext
) -> OTel.SamplingResult {
guard let parentSpanContext = parentBaggage.spanContext else {
guard let parentSpanContext = parentContext.spanContext else {
return rootSampler.makeSamplingDecision(
operationName: operationName,
kind: kind,
traceID: traceID,
attributes: attributes,
links: links,
parentBaggage: parentBaggage
parentContext: parentContext
)
}

Expand All @@ -85,7 +85,7 @@ extension OTel {
traceID: traceID,
attributes: attributes,
links: links,
parentBaggage: parentBaggage
parentContext: parentContext
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenTelemetry/Sampling/Sampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public protocol OTelSampler {
/// - traceID: The trace id, either propagated or newly generated.
/// - attributes: A set of default span attributes.
/// - links: A set of default span links.
/// - parentBaggage: The parent baggage which might contain a `OTel.SpanContext`.
/// - parentContext: The parent service context which might contain a `OTel.SpanContext`.
/// - Returns: A decision on whether the span to be started should be dropped, only recorded, or recorded and sampled.
func makeSamplingDecision(
operationName: String,
kind: SpanKind,
traceID: OTel.TraceID,
attributes: SpanAttributes,
links: [SpanLink],
parentBaggage: Baggage
parentContext: ServiceContext
) -> OTel.SamplingResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//
//===----------------------------------------------------------------------===//

import InstrumentationBaggage
import ServiceContextModule

extension Baggage {
extension ServiceContext {
public internal(set) var spanContext: OTel.SpanContext? {
get {
self[SpanContextKey.self]
Expand All @@ -31,7 +31,7 @@ extension OTel.SpanContext: CustomStringConvertible {
}
}

private enum SpanContextKey: BaggageKey {
private enum SpanContextKey: ServiceContextKey {
typealias Value = OTel.SpanContext

static var nameOverride: String? = "otel-span-context"
Expand Down
38 changes: 19 additions & 19 deletions Sources/OpenTelemetry/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ extension OTel {
extension OTel.Tracer: Instrument {
public func extract<Carrier, Extract>(
_ carrier: Carrier,
into baggage: inout Baggage,
into context: inout ServiceContext,
using extractor: Extract
) where Carrier == Extract.Carrier, Extract: Extractor {
do {
baggage.spanContext = try propagator.extractSpanContext(from: carrier, using: extractor)
context.spanContext = try propagator.extractSpanContext(from: carrier, using: extractor)
} catch {
logger.debug("Failed to extract span context", metadata: [
"carrier": .string(String(describing: carrier)),
Expand All @@ -59,11 +59,11 @@ extension OTel.Tracer: Instrument {
}

public func inject<Carrier, Inject>(
_ baggage: Baggage,
_ context: ServiceContext,
into carrier: inout Carrier,
using injector: Inject
) where Carrier == Inject.Carrier, Inject: Injector {
guard let spanContext = baggage.spanContext else { return }
guard let spanContext = context.spanContext else { return }
propagator.inject(spanContext, into: &carrier, using: injector)
}
}
Expand All @@ -73,7 +73,7 @@ extension OTel.Tracer: Tracer {

public func startAnySpan<Instant: TracerInstant>(
_ operationName: String,
baggage: @autoclosure () -> Baggage,
context: @autoclosure () -> ServiceContext,
ofKind kind: SpanKind,
at instant: @autoclosure () -> Instant,
function: String,
Expand All @@ -82,7 +82,7 @@ extension OTel.Tracer: Tracer {
) -> any Tracing.Span {
startSpan(
operationName,
baggage: baggage(),
context: context(),
ofKind: kind,
at: instant(),
function: function,
Expand All @@ -93,21 +93,21 @@ extension OTel.Tracer: Tracer {

public func startSpan<Instant: TracerInstant>(
_ operationName: String,
baggage: @autoclosure () -> Baggage,
context: @autoclosure () -> ServiceContext,
ofKind kind: SpanKind,
at instant: @autoclosure () -> Instant,
function: String,
file fileID: String,
line: UInt
) -> TracerSpan {
let parentBaggage = baggage()
var childBaggage = parentBaggage
let parentContext = context()
var childContext = parentContext

let traceID: OTel.TraceID
let traceState: OTel.TraceState?
let spanID = idGenerator.generateSpanID()

if let parentSpanContext = parentBaggage.spanContext {
if let parentSpanContext = parentContext.spanContext {
traceID = parentSpanContext.traceID
traceState = parentSpanContext.traceState
} else {
Expand All @@ -121,23 +121,23 @@ extension OTel.Tracer: Tracer {
traceID: traceID,
attributes: [:],
links: [],
parentBaggage: parentBaggage
parentContext: parentContext
)
let traceFlags: OTel.TraceFlags = samplingResult.decision == .recordAndSample ? .sampled : []
let spanContext = OTel.SpanContext(
traceID: traceID,
spanID: spanID,
parentSpanID: parentBaggage.spanContext?.spanID,
parentSpanID: parentContext.spanContext?.spanID,
traceFlags: traceFlags,
traceState: traceState,
isRemote: false
)
childBaggage.spanContext = spanContext
childContext.spanContext = spanContext

if samplingResult.decision == .drop {
return Span(
operationName: operationName,
baggage: childBaggage,
context: childContext,
kind: kind,
startTime: instant().nanosecondsSinceEpoch,
attributes: samplingResult.attributes,
Expand All @@ -151,7 +151,7 @@ extension OTel.Tracer: Tracer {

return Span(
operationName: operationName,
baggage: childBaggage,
context: childContext,
kind: kind,
startTime: instant().nanosecondsSinceEpoch,
attributes: samplingResult.attributes,
Expand Down Expand Up @@ -184,7 +184,7 @@ extension OTel.Tracer {
public let kind: SpanKind
public private(set) var status: SpanStatus?

public let baggage: Baggage
public let context: ServiceContext

public let isRecording: Bool

Expand All @@ -203,7 +203,7 @@ extension OTel.Tracer {

init(
operationName: String,
baggage: Baggage,
context: ServiceContext,
kind: SpanKind,
startTime: UInt64,
attributes: SpanAttributes,
Expand All @@ -213,7 +213,7 @@ extension OTel.Tracer {
onEnd: @escaping (OTel.RecordedSpan) -> Void
) {
_operationName = operationName
self.baggage = baggage
self.context = context
self.kind = kind
self.startTime = startTime
self.attributes = attributes
Expand Down Expand Up @@ -252,7 +252,7 @@ extension OTel.Tracer {
public func end<Instant: TracerInstant>(at instant: @autoclosure () -> Instant) {
lock.withLockVoid {
if let endTime = self.endTime {
if let spanContext = baggage.spanContext {
if let spanContext = context.spanContext {
logger.trace("Ignoring a span that was ended before", metadata: [
"previousEndTime": .stringConvertible(endTime),
"traceID": .stringConvertible(spanContext.traceID),
Expand Down
8 changes: 4 additions & 4 deletions Sources/OtlpGRPCSpanExporting/TypeConversion/Span+Proto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ extension Opentelemetry_Proto_Trace_V1_Span {
init(_ span: OTel.RecordedSpan) {
self.name = span.operationName
self.kind = SpanKind(span.kind)
self.traceID = Data(span.context.traceID.bytes)
self.spanID = Data(span.context.spanID.bytes)
if let parentSpanID = span.context.parentSpanID {
self.traceID = Data(span.spanContext.traceID.bytes)
self.spanID = Data(span.spanContext.spanID.bytes)
if let parentSpanID = span.spanContext.parentSpanID {
self.parentSpanID = Data(parentSpanID.bytes)
}
if let traceState = span.context.traceState {
if let traceState = span.spanContext.traceState {
self.traceState = traceState.description
}
if let status = span.status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Tracing

extension Opentelemetry_Proto_Trace_V1_Span.Link {
init?(_ spanLink: SpanLink) {
guard let spanContext = spanLink.baggage.spanContext else { return nil }
guard let spanContext = spanLink.context.spanContext else { return nil }
self.traceID = Data(spanContext.traceID.bytes)
self.spanID = Data(spanContext.spanID.bytes)
if let traceState = spanContext.traceState {
Expand Down
4 changes: 2 additions & 2 deletions Tests/OpenTelemetryTests/Helpers/MockSampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class MockSampler: OTelSampler {
traceID: OTel.TraceID,
attributes: SpanAttributes,
links: [SpanLink],
parentBaggage: Baggage
parentContext: ServiceContext
) -> OTel.SamplingResult {
numberOfSamplingDecisions += 1
return sampler.makeSamplingDecision(
Expand All @@ -37,7 +37,7 @@ final class MockSampler: OTelSampler {
traceID: traceID,
attributes: attributes,
links: links,
parentBaggage: parentBaggage
parentContext: parentContext
)
}
}
6 changes: 3 additions & 3 deletions Tests/OpenTelemetryTests/Helpers/Span+Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ extension OTel.Tracer.Span {
logger: Logger = Logger(label: #function),
onEnd: @escaping (OTel.RecordedSpan) -> Void = { _ in }
) -> OTel.Tracer.Span {
var baggage = Baggage.topLevel
baggage.spanContext = spanContext
var context = ServiceContext.topLevel
context.spanContext = spanContext

return OTel.Tracer.Span(
operationName: operationName,
baggage: baggage,
context: context,
kind: kind,
startTime: startTime,
attributes: attributes,
Expand Down
Loading

0 comments on commit 4afda7e

Please sign in to comment.