Skip to content

Commit a235bc4

Browse files
authored
Move tracer to Configuration to support tracer config changes after swizzle (#783)
Move tracer to UrlSessionInstrumentationConfiguration
1 parent 400664f commit a235bc4

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class URLSessionInstrumentation {
4343
]
4444
.compactMap { NSClassFromString($0) }
4545

46-
public private(set) var tracer: Tracer
47-
4846
public var startedRequestSpans: [Span] {
4947
var spans = [Span]()
5048
URLSessionLogger.runningSpansQueue.sync {
@@ -55,7 +53,6 @@ public class URLSessionInstrumentation {
5553

5654
public init(configuration: URLSessionInstrumentationConfiguration) {
5755
self.configuration = configuration
58-
tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "NSURLSession", instrumentationVersion: "0.0.1")
5956
injectInNSURLClasses()
6057
}
6158

Sources/Instrumentation/URLSession/URLSessionInstrumentationConfiguration.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public struct URLSessionInstrumentationConfiguration {
2525
receivedResponse: ((URLResponse, DataOrFile?, Span) -> Void)? = nil,
2626
receivedError: ((Error, DataOrFile?, HTTPStatus, Span) -> Void)? = nil,
2727
delegateClassesToInstrument: [AnyClass]? = nil,
28-
baggageProvider: ((inout URLRequest, Span?) -> (Baggage)?)? = nil) {
28+
baggageProvider: ((inout URLRequest, Span?) -> (Baggage)?)? = nil,
29+
tracer: Tracer? = nil) {
2930
self.shouldRecordPayload = shouldRecordPayload
3031
self.shouldInstrument = shouldInstrument
3132
self.shouldInjectTracingHeaders = shouldInjectTracingHeaders
@@ -37,8 +38,12 @@ public struct URLSessionInstrumentationConfiguration {
3738
self.receivedError = receivedError
3839
self.delegateClassesToInstrument = delegateClassesToInstrument
3940
self.baggageProvider = baggageProvider
41+
self.tracer = tracer ??
42+
OpenTelemetry.instance.tracerProvider.get(instrumentationName: "NSURLSession", instrumentationVersion: "0.0.1")
4043
}
4144

45+
public var tracer: Tracer
46+
4247
// Instrumentation Callbacks
4348

4449
/// Implement this callback to filter which requests you want to instrument, all by default

Sources/Instrumentation/URLSession/URLSessionLogger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class URLSessionLogger {
7070
if let customSpanName = instrumentation.configuration.nameSpan?(request) {
7171
spanName = customSpanName
7272
}
73-
let spanBuilder = instrumentation.tracer.spanBuilder(spanName: spanName)
73+
let spanBuilder = instrumentation.configuration.tracer.spanBuilder(spanName: spanName)
7474
spanBuilder.setSpanKind(spanKind: .client)
7575
attributes.forEach {
7676
spanBuilder.setAttribute(key: $0.key, value: $0.value)

0 commit comments

Comments
 (0)