Skip to content

Commit 27ea77a

Browse files
slashmoktoso
andauthored
Add TracingInstrument (#62)
Co-authored-by: Konrad `ktoso` Malawski <[email protected]> Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
1 parent 75db4ba commit 27ea77a

File tree

11 files changed

+892
-15
lines changed

11 files changed

+892
-15
lines changed

Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import PackageDescription
33

44
let package = Package(
55
name: "gsoc-swift-tracing",
6+
platforms: [
7+
.macOS(.v10_15)
8+
],
69
products: [
710
.library(name: "BaggageLogging", targets: ["BaggageLogging"]),
811
.library(name: "Instrumentation", targets: ["Instrumentation"]),
@@ -44,6 +47,7 @@ let package = Package(
4447
name: "InstrumentationTests",
4548
dependencies: [
4649
"Instrumentation",
50+
"BaggageLogging"
4751
]
4852
),
4953

Sources/Instrumentation/Instrument.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@ public protocol Instrument {
6666
Injector: InjectorProtocol,
6767
Injector.Carrier == Carrier
6868
}
69+
70+
public struct NoOpInstrument: Instrument {
71+
public func inject<Carrier, Injector>(_ baggage: BaggageContext, into carrier: inout Carrier, using injector: Injector)
72+
where
73+
Injector: InjectorProtocol,
74+
Carrier == Injector.Carrier {}
75+
76+
public func extract<Carrier, Extractor>(_ carrier: Carrier, into baggage: inout BaggageContext, using extractor: Extractor)
77+
where
78+
Extractor: ExtractorProtocol,
79+
Carrier == Extractor.Carrier {}
80+
}

Sources/Instrumentation/InstrumentationSystem.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import Baggage
1919
/// - Note: If you need to use more that one cross-cutting tool you can do so by using `MultiplexInstrument`.
2020
public enum InstrumentationSystem {
2121
private static let lock = ReadWriteLock()
22-
private static var _instrument: Instrument = NOOPInstrument.instance
22+
private static var _instrument: Instrument = NoOpInstrument()
23+
private static var _tracer: TracingInstrument?
2324
private static var isInitialized = false
2425

2526
/// Globally select the desired `Instrument` implementation.
@@ -34,27 +35,36 @@ public enum InstrumentationSystem {
3435
you need to use multiple instruments.
3536
"""
3637
)
38+
if let tracer = instrument as? TracingInstrument {
39+
self._tracer = tracer
40+
}
3741
self._instrument = instrument
42+
3843
self.isInitialized = true
3944
}
4045
}
4146

47+
// for our testing we want to allow multiple bootstrapping
48+
internal static func bootstrapInternal(_ instrument: Instrument) {
49+
self.lock.withWriterLock {
50+
if let tracer = instrument as? TracingInstrument {
51+
self._tracer = tracer
52+
}
53+
self._instrument = instrument
54+
}
55+
}
56+
4257
/// Returns the globally configured `Instrument`. Defaults to a no-op `Instrument` if `boostrap` wasn't called before.
4358
public static var instrument: Instrument {
4459
self.lock.withReaderLock { self._instrument }
4560
}
46-
}
47-
48-
private final class NOOPInstrument: Instrument {
49-
static let instance = NOOPInstrument()
50-
51-
func inject<Carrier, Injector>(_ baggage: BaggageContext, into carrier: inout Carrier, using injector: Injector)
52-
where
53-
Injector: InjectorProtocol,
54-
Carrier == Injector.Carrier {}
5561

56-
func extract<Carrier, Extractor>(_ carrier: Carrier, into baggage: inout BaggageContext, using extractor: Extractor)
57-
where
58-
Extractor: ExtractorProtocol,
59-
Carrier == Extractor.Carrier {}
62+
// FIXME: smarter impl
63+
public static var tracer: TracingInstrument {
64+
self.lock.withReaderLock {
65+
let tracer: TracingInstrument? = self._tracer
66+
let res: TracingInstrument = tracer ?? NoOpTracingInstrument()
67+
return res
68+
}
69+
}
6070
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2020 Moritz Lang and the Swift Tracing project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
import Baggage
15+
import Dispatch
16+
17+
/// No operation TracingInstrument, used when no tracing is required.
18+
public struct NoOpTracingInstrument: TracingInstrument {
19+
public var currentSpan: Span? = nil
20+
21+
public func startSpan(named operationName: String, context: BaggageContext, at timestamp: DispatchTime?) -> Span {
22+
NoOpSpan()
23+
}
24+
25+
public func inject<Carrier, Injector>(_ context: BaggageContext, into carrier: inout Carrier, using injector: Injector)
26+
where
27+
Injector: InjectorProtocol,
28+
Carrier == Injector.Carrier {}
29+
30+
public func extract<Carrier, Extractor>(_ carrier: Carrier, into baggage: inout BaggageContext, using extractor: Extractor)
31+
where
32+
Extractor: ExtractorProtocol,
33+
Carrier == Extractor.Carrier {}
34+
35+
public struct NoOpSpan: Span {
36+
public var operationName: String = ""
37+
public var status: SpanStatus?
38+
39+
public var startTimestamp: DispatchTime {
40+
.now()
41+
}
42+
43+
public var endTimestamp: DispatchTime? = nil
44+
45+
public var baggage: BaggageContext {
46+
.init()
47+
}
48+
49+
public var events: [SpanEvent] {
50+
[]
51+
}
52+
53+
public mutating func addEvent(_ event: SpanEvent) {}
54+
55+
public var attributes: SpanAttributes {
56+
get {
57+
[:]
58+
}
59+
set {
60+
// ignore
61+
}
62+
}
63+
64+
public let isRecording = false
65+
66+
public mutating func end(at timestamp: DispatchTime) {
67+
// ignore
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)