Skip to content

Commit 394441c

Browse files
authored
Adopt ServiceContext 1.0 (#127)
1 parent 4018353 commit 394441c

37 files changed

+579
-1113
lines changed

Docs/SwiftDistributedTracing/empty.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
// This module is left purposefully empty of any source files, as it serves
1717
// only as a "landing page" for the documentation. This is in-place until docc
1818
// gains the ability to support package-wide documentation.
19-
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //
19+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //

IntegrationTests/tests_01_performance/test_01_resources/run-nio-alloc-counter-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fi
5353

5454
"$here/../../allocation-counter-tests-framework/run-allocation-counter.sh" \
5555
-p "$here/../../.." \
56-
-m Baggage \
56+
-m ServiceContext \
5757
-m Instrumentation \
5858
-m NIOInstrumentation \
5959
-s "$here/shared.swift" \

IntegrationTests/tests_01_performance/test_01_resources/shared.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//===----------------------------------------------------------------------===//
2828

2929
import Foundation
30-
import InstrumentationBaggage
30+
import ServiceContextModule
3131

3232
@inline(never)
3333
func take1(context: BaggageContext) -> Int {

IntegrationTests/tests_01_performance/test_01_resources/test_001_pass_around_static_strings_small.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//
2727
//===----------------------------------------------------------------------===//
2828

29-
import InstrumentationBaggage
29+
import ServiceContextModule
3030

3131
func run(identifier: String) {
3232
measure(identifier: identifier) {

IntegrationTests/tests_01_performance/test_01_resources/test_002_pass_around_static_strings_large.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//
2727
//===----------------------------------------------------------------------===//
2828

29-
import InstrumentationBaggage
29+
import ServiceContextModule
3030

3131
func run(identifier: String) {
3232
measure(identifier: identifier) {

Package.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ let package = Package(
1414
.library(name: "Tracing", targets: ["Tracing"]),
1515
],
1616
dependencies: [
17-
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
17+
// .package(url: "https://github.com/apple/swift-service-context.git", from: "1.0.0"),
18+
.package(url: "https://github.com/apple/swift-service-context.git", branch: "main"),
1819
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
1920
],
2021
targets: [
@@ -24,7 +25,7 @@ let package = Package(
2425
.target(
2526
name: "Instrumentation",
2627
dependencies: [
27-
.product(name: "InstrumentationBaggage", package: "swift-distributed-tracing-baggage"),
28+
.product(name: "ServiceContextModule", package: "swift-service-context"),
2829
]
2930
),
3031
.testTarget(
@@ -56,7 +57,7 @@ let package = Package(
5657
.executableTarget(
5758
name: "_TracingBenchmarks",
5859
dependencies: [
59-
.product(name: "InstrumentationBaggage", package: "swift-distributed-tracing-baggage"),
60+
.product(name: "ServiceContextModule", package: "swift-service-context"),
6061
.target(name: "Tracing"),
6162
.target(name: "_TracingBenchmarkTools"),
6263
]

README.md

Lines changed: 15 additions & 548 deletions
Large diffs are not rendered by default.

Samples/Dinner/Sources/Onboarding/Clock+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
func sleep(for duration: ContinuousClock.Duration) async {
28-
try? await Task.sleep(until: ContinuousClock.now + duration, clock: .continuous)
29-
}
28+
try? await Task.sleep(until: ContinuousClock.now + duration, clock: .continuous)
29+
}

Samples/Dinner/Sources/Onboarding/Dinner.swift

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,67 +24,66 @@
2424
//
2525
//===----------------------------------------------------------------------===//
2626

27-
2827
import Tracing
2928

3029
func makeDinner() async throws -> Meal {
31-
try await InstrumentationSystem.tracer.withSpan("makeDinner") { _ in
32-
await sleep(for: .milliseconds(200))
30+
try await InstrumentationSystem.tracer.withSpan("makeDinner") { _ in
31+
await sleep(for: .milliseconds(200))
3332

34-
async let veggies = try chopVegetables()
35-
async let meat = marinateMeat()
36-
async let oven = preheatOven(temperature: 350)
37-
// ...
38-
return try await cook(veggies, meat, oven)
39-
}
33+
async let veggies = try chopVegetables()
34+
async let meat = marinateMeat()
35+
async let oven = preheatOven(temperature: 350)
36+
// ...
37+
return try await cook(veggies, meat, oven)
38+
}
4039
}
4140

4241
func chopVegetables() async throws -> [Vegetable] {
43-
try await otelChopping1.tracer().withSpan("chopVegetables") { _ in
44-
// Chop the vegetables...!
45-
//
46-
// However, since chopping is a very difficult operation,
47-
// one chopping task can be performed at the same time on a single service!
48-
// (Imagine that... we cannot parallelize these two tasks, and need to involve another service).
49-
async let carrot = try chop(.carrot, tracer: otelChopping1.tracer())
50-
async let potato = try chop(.potato, tracer: otelChopping2.tracer())
51-
return try await [carrot, potato]
52-
}
42+
try await otelChopping1.tracer().withSpan("chopVegetables") { _ in
43+
// Chop the vegetables...!
44+
//
45+
// However, since chopping is a very difficult operation,
46+
// one chopping task can be performed at the same time on a single service!
47+
// (Imagine that... we cannot parallelize these two tasks, and need to involve another service).
48+
async let carrot = try chop(.carrot, tracer: otelChopping1.tracer())
49+
async let potato = try chop(.potato, tracer: otelChopping2.tracer())
50+
return try await [carrot, potato]
51+
}
5352
}
5453

5554
func chop(_ vegetable: Vegetable, tracer: any Tracer) async throws -> Vegetable {
56-
await tracer.withSpan("chop-\(vegetable)") { _ in
57-
await sleep(for: .seconds(5))
58-
// ...
59-
return vegetable // "chopped"
60-
}
55+
await tracer.withSpan("chop-\(vegetable)") { _ in
56+
await sleep(for: .seconds(5))
57+
// ...
58+
return vegetable // "chopped"
59+
}
6160
}
6261

6362
func marinateMeat() async -> Meat {
64-
await sleep(for: .milliseconds(620))
63+
await sleep(for: .milliseconds(620))
6564

66-
return await InstrumentationSystem.tracer.withSpan("marinateMeat") { _ in
67-
await sleep(for: .seconds(3))
68-
// ...
69-
return Meat()
70-
}
65+
return await InstrumentationSystem.tracer.withSpan("marinateMeat") { _ in
66+
await sleep(for: .seconds(3))
67+
// ...
68+
return Meat()
69+
}
7170
}
7271

7372
func preheatOven(temperature: Int) async -> Oven {
74-
await InstrumentationSystem.tracer.withSpan("preheatOven") { _ in
75-
// ...
76-
await sleep(for: .seconds(6))
77-
return Oven()
78-
}
73+
await InstrumentationSystem.tracer.withSpan("preheatOven") { _ in
74+
// ...
75+
await sleep(for: .seconds(6))
76+
return Oven()
77+
}
7978
}
8079

8180
func cook(_: Any, _: Any, _: Any) async -> Meal {
82-
await InstrumentationSystem.tracer.withSpan("cook") { span in
83-
span.addEvent("children-asking-if-done-already")
84-
await sleep(for: .seconds(3))
85-
span.addEvent("children-asking-if-done-already-again")
86-
await sleep(for: .seconds(2))
87-
// ...
88-
return Meal()
89-
}
81+
await InstrumentationSystem.tracer.withSpan("cook") { span in
82+
span.addEvent("children-asking-if-done-already")
83+
await sleep(for: .seconds(3))
84+
span.addEvent("children-asking-if-done-already-again")
85+
await sleep(for: .seconds(2))
86+
// ...
87+
return Meal()
88+
}
9089
}

Samples/Dinner/Sources/Onboarding/Model.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
//
2525
//===----------------------------------------------------------------------===//
2626

27-
2827
struct Meal: Sendable {}
2928
struct Meat: Sendable {}
3029
struct Oven: Sendable {}
3130
enum Vegetable: Sendable {
32-
case potato
33-
case carrot
31+
case potato
32+
case carrot
3433
}
35-

Samples/Dinner/Sources/Onboarding/main.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
//
2525
//===----------------------------------------------------------------------===//
2626

27-
2827
import Logging
2928
import NIO
3029
import OpenTelemetry
@@ -36,9 +35,9 @@ import Tracing
3635
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
3736

3837
LoggingSystem.bootstrap { label in
39-
var handler = StreamLogHandler.standardOutput(label: label)
40-
handler.logLevel = .trace
41-
return handler
38+
var handler = StreamLogHandler.standardOutput(label: label)
39+
handler.logLevel = .trace
40+
return handler
4241
}
4342

4443
// ==== ----------------------------------------------------------------------------------------------------------------

Sources/Instrumentation/Instrument.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import InstrumentationBaggage
15+
import ServiceContextModule
1616

1717
/// Typealias used to simplify Support of old Swift versions which do not have `Sendable` defined.
1818
#if swift(>=5.6.0)
@@ -51,22 +51,22 @@ public protocol Injector: _SwiftInstrumentationSendable {
5151
/// Conforming types are usually cross-cutting tools like tracers. They are agnostic of what specific `Carrier` is used
5252
/// to propagate metadata across boundaries, but instead just specify what values to use for which keys.
5353
public protocol Instrument: _SwiftInstrumentationSendable {
54-
/// Extract values from a `Carrier` by using the given extractor and inject them into the given `Baggage`.
54+
/// Extract values from a `Carrier` by using the given extractor and inject them into the given `ServiceContext`.
5555
/// It's quite common for `Instrument`s to come up with new values if they weren't passed along in the given `Carrier`.
5656
///
5757
/// - Parameters:
5858
/// - carrier: The `Carrier` that was used to propagate values across boundaries.
59-
/// - baggage: The `Baggage` into which these values should be injected.
59+
/// - context: The `ServiceContext` into which these values should be injected.
6060
/// - extractor: The ``Extractor`` that extracts values from the given `Carrier`.
61-
func extract<Carrier, Extract>(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract)
61+
func extract<Carrier, Extract>(_ carrier: Carrier, into context: inout ServiceContext, using extractor: Extract)
6262
where Extract: Extractor, Extract.Carrier == Carrier
6363

64-
/// Extract values from a `Baggage` and inject them into the given `Carrier` using the given ``Injector``.
64+
/// Extract values from a `ServiceContext` and inject them into the given `Carrier` using the given ``Injector``.
6565
///
6666
/// - Parameters:
67-
/// - baggage: The `Baggage` from which relevant information will be extracted.
67+
/// - context: The `ServiceContext` from which relevant information will be extracted.
6868
/// - carrier: The `Carrier` into which this information will be injected.
69-
/// - injector: The ``Injector`` used to inject extracted `Baggage` into the given `Carrier`.
70-
func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject)
69+
/// - injector: The ``Injector`` used to inject extracted `ServiceContext` into the given `Carrier`.
70+
func inject<Carrier, Inject>(_ context: ServiceContext, into carrier: inout Carrier, using injector: Inject)
7171
where Inject: Injector, Inject.Carrier == Carrier
7272
}

Sources/Instrumentation/InstrumentationSystem.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import InstrumentationBaggage
15+
import ServiceContextModule
1616

1717
/// `InstrumentationSystem` is a global facility where the default cross-cutting tool can be configured.
1818
/// It is set up just once in a given program to select the desired ``Instrument`` implementation.
@@ -22,7 +22,7 @@ import InstrumentationBaggage
2222
///
2323
/// # Access the Instrument
2424
/// ``instrument``: Returns whatever you passed to ``bootstrap(_:)`` as an ``Instrument``.
25-
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
25+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
2626
public enum InstrumentationSystem {
2727
private static let lock = ReadWriteLock()
2828
private static var _instrument: Instrument = NoOpInstrument()
@@ -62,9 +62,9 @@ public enum InstrumentationSystem {
6262
}
6363
}
6464

65-
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
65+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
6666
extension InstrumentationSystem {
67-
/// :nodoc: INTERNAL API: Do Not Use
67+
/// INTERNAL API: Do Not Use
6868
public static func _findInstrument(where predicate: (Instrument) -> Bool) -> Instrument? {
6969
self.lock.withReaderLock {
7070
if let multiplex = self._instrument as? MultiplexInstrument {

Sources/Instrumentation/MultiplexInstrument.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import InstrumentationBaggage
15+
import ServiceContextModule
1616

1717
/// A pseudo-``Instrument`` that may be used to instrument using multiple other ``Instrument``s across a
18-
/// common `Baggage`.
18+
/// common `ServiceContext`.
1919
public struct MultiplexInstrument {
2020
private var instruments: [Instrument]
2121

2222
/// Create a ``MultiplexInstrument``.
2323
///
2424
/// - Parameter instruments: An array of ``Instrument``s, each of which will be used to ``Instrument/inject(_:into:using:)`` or ``Instrument/extract(_:into:using:)``
25-
/// through the same `Baggage`.
25+
/// through the same `ServiceContext`.
2626
public init(_ instruments: [Instrument]) {
2727
self.instruments = instruments
2828
}
@@ -35,15 +35,15 @@ extension MultiplexInstrument {
3535
}
3636

3737
extension MultiplexInstrument: Instrument {
38-
public func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject)
38+
public func inject<Carrier, Inject>(_ context: ServiceContext, into carrier: inout Carrier, using injector: Inject)
3939
where Inject: Injector, Carrier == Inject.Carrier
4040
{
41-
self.instruments.forEach { $0.inject(baggage, into: &carrier, using: injector) }
41+
self.instruments.forEach { $0.inject(context, into: &carrier, using: injector) }
4242
}
4343

44-
public func extract<Carrier, Extract>(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract)
44+
public func extract<Carrier, Extract>(_ carrier: Carrier, into context: inout ServiceContext, using extractor: Extract)
4545
where Extract: Extractor, Carrier == Extract.Carrier
4646
{
47-
self.instruments.forEach { $0.extract(carrier, into: &baggage, using: extractor) }
47+
self.instruments.forEach { $0.extract(carrier, into: &context, using: extractor) }
4848
}
4949
}

Sources/Instrumentation/NoOpInstrument.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import InstrumentationBaggage
15+
import ServiceContextModule
1616

1717
/// A "no op" implementation of an ``Instrument``.
1818
public struct NoOpInstrument: Instrument {
1919
public init() {}
2020

21-
public func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject)
21+
public func inject<Carrier, Inject>(_ context: ServiceContext, into carrier: inout Carrier, using injector: Inject)
2222
where Inject: Injector, Carrier == Inject.Carrier
2323
{
2424
// no-op
2525
}
2626

27-
public func extract<Carrier, Extract>(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract)
27+
public func extract<Carrier, Extract>(_ carrier: Carrier, into context: inout ServiceContext, using extractor: Extract)
2828
where Extract: Extractor, Carrier == Extract.Carrier
2929
{
3030
// no-op

0 commit comments

Comments
 (0)