Skip to content

Commit be4aadb

Browse files
ktososlashmo
andauthored
-remove tracer(of:) instrument(of:) (#16)
* minor span docs improvement * -remove tracer(of:) instrument(of:) * Minor docs improvement on bootstrapInternal * Apply suggestions from code review Co-authored-by: Moritz Lang <[email protected]> Co-authored-by: Moritz Lang <[email protected]>
1 parent c0213fe commit be4aadb

File tree

4 files changed

+32
-48
lines changed

4 files changed

+32
-48
lines changed

Sources/Instrumentation/InstrumentationSystem.swift

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,7 @@ import Baggage
2020
/// If you need to use more that one cross-cutting tool you can do so by using `MultiplexInstrument`.
2121
///
2222
/// # Access the Instrument
23-
/// There are two ways of getting the bootstrapped instrument.
24-
/// 1. `InstrumentationSystem.instrument`: Returns whatever you passed to `.bootstrap` as an `Instrument`.
25-
/// 2. `InstrumentationSystem.instrument(of: MyInstrument.self)`: Returns the bootstrapped `Instrument` if it's
26-
/// an instance of the given type or the first instance of `MyInstrument` if it's part of a `MultiplexInstrument`.
27-
///
28-
/// ## What getter to use
29-
/// - Default to using `InstrumentationSystem.instrument`
30-
/// - Use `InstrumentationSystem.instrument(of: MyInstrument.self)` only if you need to use specific `MyInstrument` APIs
31-
///
32-
/// Specific instrumentation libraries may also provide their own accessors as extensions, e.g. GreatInstrumentation could provide an
33-
/// `InstrumentationSystem.great` convenience accessor, so prefer using them if available. These accessors should call
34-
/// `.instrument(of: GreatInstrument.self)` under the hood to ensure they work when being used through a `MultiplexInstrument`.
23+
/// `InstrumentationSystem.instrument`: Returns whatever you passed to `.bootstrap` as an `Instrument`.
3524
public enum InstrumentationSystem {
3625
private static let lock = ReadWriteLock()
3726
private static var _instrument: Instrument = NoOpInstrument()
@@ -54,7 +43,9 @@ public enum InstrumentationSystem {
5443
}
5544
}
5645

57-
// for our testing we want to allow multiple bootstrapping
46+
/// For testing scenarios one may want to set instruments multiple times, rather than the set-once semantics enforced by `bootstrap()`.
47+
///
48+
/// - Parameter instrument: the instrument to boostrap the system with, if `nil` the `NoOpInstrument` is bootstrapped.
5849
internal static func bootstrapInternal(_ instrument: Instrument?) {
5950
self.lock.withWriterLock {
6051
self._instrument = instrument ?? NoOpInstrument()
@@ -67,16 +58,6 @@ public enum InstrumentationSystem {
6758
public static var instrument: Instrument {
6859
return self.lock.withReaderLock { self._instrument }
6960
}
70-
71-
/// Get an `Instrument` instance of the given type.
72-
///
73-
/// When using `MultiplexInstrument`, this returns the first instance of the given type stored in the `MultiplexInstrument`.
74-
///
75-
/// - Parameter instrumentType: The type of `Instrument` you want to retrieve an instance for.
76-
/// - Returns: An `Instrument` instance of the given type or `nil` if no `Instrument` of that type has been bootstrapped.
77-
public static func instrument<I>(of instrumentType: I.Type) -> I? where I: Instrument {
78-
return self._findInstrument(where: { $0 is I }) as? I
79-
}
8061
}
8162

8263
extension InstrumentationSystem {

Sources/Tracing/InstrumentationSystem+Tracing.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@
1414
@_exported import Instrumentation
1515

1616
extension InstrumentationSystem {
17-
/// Get a `Tracer` instance of the given type.
18-
///
19-
/// When using `MultiplexInstrument`, this returns the first instance of the given type stored in the `MultiplexInstrument`.
20-
///
21-
/// Usually tracing libraries will provide their own convenience getter, e.g. CoolTracing could provide `InstrumentationSystem.coolTracer`;
22-
/// if available, prefer using those APIs rather than relying on this general function.
23-
///
24-
/// - Parameter tracerType: The type of `Tracer` you want to retrieve an instance for.
25-
/// - Returns: A `Tracer` instance of the given type or `nil` if no `Tracer` of that type has been bootstrapped.
26-
public static func tracer<T>(of tracerType: T.Type) -> T? where T: Tracer {
27-
return self._findInstrument(where: { $0 is T }) as? T
28-
}
29-
3017
/// Returns the `Tracer` bootstrapped as part of the `InstrumentationSystem`.
3118
///
3219
/// If the system was bootstrapped with a `MultiplexInstrument` this function attempts to locate the _first_

Tests/InstrumentationTests/InstrumentationSystemTests.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import Baggage
1515
@testable import Instrumentation
1616
import XCTest
1717

18+
extension InstrumentationSystem {
19+
public static func _instrument<I>(of instrumentType: I.Type) -> I? where I: Instrument {
20+
return self._findInstrument(where: { $0 is I }) as? I
21+
}
22+
}
23+
1824
final class InstrumentationSystemTests: XCTestCase {
1925
override class func tearDown() {
2026
super.tearDown()
@@ -26,18 +32,18 @@ final class InstrumentationSystemTests: XCTestCase {
2632
let instrument = FakeInstrument()
2733
let multiplexInstrument = MultiplexInstrument([tracer, instrument])
2834

29-
XCTAssertNil(InstrumentationSystem.instrument(of: FakeTracer.self))
30-
XCTAssertNil(InstrumentationSystem.instrument(of: FakeInstrument.self))
35+
XCTAssertNil(InstrumentationSystem._instrument(of: FakeTracer.self))
36+
XCTAssertNil(InstrumentationSystem._instrument(of: FakeInstrument.self))
3137

3238
InstrumentationSystem.bootstrapInternal(multiplexInstrument)
3339
XCTAssert(InstrumentationSystem.instrument is MultiplexInstrument)
34-
XCTAssert(InstrumentationSystem.instrument(of: FakeTracer.self) === tracer)
35-
XCTAssert(InstrumentationSystem.instrument(of: FakeInstrument.self) === instrument)
40+
XCTAssert(InstrumentationSystem._instrument(of: FakeTracer.self) === tracer)
41+
XCTAssert(InstrumentationSystem._instrument(of: FakeInstrument.self) === instrument)
3642

3743
InstrumentationSystem.bootstrapInternal(tracer)
3844
XCTAssertFalse(InstrumentationSystem.instrument is MultiplexInstrument)
39-
XCTAssert(InstrumentationSystem.instrument(of: FakeTracer.self) === tracer)
40-
XCTAssertNil(InstrumentationSystem.instrument(of: FakeInstrument.self))
45+
XCTAssert(InstrumentationSystem._instrument(of: FakeTracer.self) === tracer)
46+
XCTAssertNil(InstrumentationSystem._instrument(of: FakeInstrument.self))
4147
}
4248
}
4349

Tests/TracingTests/TracingInstrumentationSystemTests.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
import Tracing
1616
import XCTest
1717

18+
extension InstrumentationSystem {
19+
public static func _tracer<T>(of tracerType: T.Type) -> T? where T: Tracer {
20+
return self._findInstrument(where: { $0 is T }) as? T
21+
}
22+
23+
public static func _instrument<I>(of instrumentType: I.Type) -> I? where I: Instrument {
24+
return self._findInstrument(where: { $0 is I }) as? I
25+
}
26+
}
27+
1828
final class TracingInstrumentationSystemTests: XCTestCase {
1929
override class func tearDown() {
2030
super.tearDown()
@@ -24,22 +34,22 @@ final class TracingInstrumentationSystemTests: XCTestCase {
2434
func testItProvidesAccessToATracer() {
2535
let tracer = TestTracer()
2636

27-
XCTAssertNil(InstrumentationSystem.tracer(of: TestTracer.self))
37+
XCTAssertNil(InstrumentationSystem._tracer(of: TestTracer.self))
2838

2939
InstrumentationSystem.bootstrapInternal(tracer)
3040
XCTAssertFalse(InstrumentationSystem.instrument is MultiplexInstrument)
31-
XCTAssert(InstrumentationSystem.instrument(of: TestTracer.self) === tracer)
32-
XCTAssertNil(InstrumentationSystem.instrument(of: NoOpInstrument.self))
41+
XCTAssert(InstrumentationSystem._instrument(of: TestTracer.self) === tracer)
42+
XCTAssertNil(InstrumentationSystem._instrument(of: NoOpInstrument.self))
3343

34-
XCTAssert(InstrumentationSystem.tracer(of: TestTracer.self) === tracer)
44+
XCTAssert(InstrumentationSystem._tracer(of: TestTracer.self) === tracer)
3545
XCTAssert(InstrumentationSystem.tracer is TestTracer)
3646

3747
let multiplexInstrument = MultiplexInstrument([tracer])
3848
InstrumentationSystem.bootstrapInternal(multiplexInstrument)
3949
XCTAssert(InstrumentationSystem.instrument is MultiplexInstrument)
40-
XCTAssert(InstrumentationSystem.instrument(of: TestTracer.self) === tracer)
50+
XCTAssert(InstrumentationSystem._instrument(of: TestTracer.self) === tracer)
4151

42-
XCTAssert(InstrumentationSystem.tracer(of: TestTracer.self) === tracer)
52+
XCTAssert(InstrumentationSystem._tracer(of: TestTracer.self) === tracer)
4353
XCTAssert(InstrumentationSystem.tracer is TestTracer)
4454
}
4555
}

0 commit comments

Comments
 (0)