Skip to content

Commit 64bf660

Browse files
authored
Merge branch 'main' into entity-poc
2 parents 9bb87de + 5839cb5 commit 64bf660

File tree

19 files changed

+306
-30
lines changed

19 files changed

+306
-30
lines changed

Examples/Logging Tracer/LoggingTracerProvider.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ import Foundation
77
import OpenTelemetryApi
88

99
class LoggingTracerProvider: TracerProvider {
10-
func get(instrumentationName: String, instrumentationVersion: String?) -> Tracer {
11-
Logger.log("TracerFactory.get(\(instrumentationName), \(instrumentationVersion ?? ""))")
10+
func get(instrumentationName: String, instrumentationVersion: String?,
11+
schemaUrl: String? = nil,
12+
attributes: [String: AttributeValue]? = nil) -> any Tracer {
13+
Logger
14+
.log(
15+
"TracerFactory.get(\(instrumentationName), \(instrumentationVersion ?? ""), \(schemaUrl ?? ""), \(attributes ?? [:])"
16+
)
1217
var labels = [String: String]()
1318
labels["instrumentationName"] = instrumentationName
1419
labels["instrumentationVersion"] = instrumentationVersion
20+
labels["schemaUrl"] = schemaUrl
21+
labels["attributes"] = attributes?.description
1522
return LoggingTracer()
1623
}
1724
}

Examples/Network Sample/main.swift

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,54 @@ func simpleNetworkCall() {
2828

2929
class SessionDelegate: NSObject, URLSessionDataDelegate, URLSessionTaskDelegate {
3030
let semaphore = DispatchSemaphore(value: 0)
31+
var callCount = 0
3132

3233
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
3334
semaphore.signal()
3435
}
36+
37+
func urlSession(
38+
_ session: URLSession,
39+
task: URLSessionTask,
40+
didFinishCollecting metrics: URLSessionTaskMetrics
41+
) {
42+
semaphore.signal()
43+
callCount += 1
44+
print("delegate called")
45+
}
3546
}
3647

3748
let delegate = SessionDelegate()
3849

50+
enum TimeoutError: Error {
51+
case timeout
52+
}
53+
54+
func waitForSemaphore(withTimeoutSecs: Int) async {
55+
do {
56+
let _ = try await withThrowingTaskGroup(of: Bool.self) { group in
57+
group.addTask {
58+
try await Task.sleep(nanoseconds: UInt64(withTimeoutSecs) * NSEC_PER_SEC)
59+
throw TimeoutError.timeout
60+
}
61+
group.addTask {
62+
let semaphoreTask = Task {
63+
DispatchQueue.global().async {
64+
delegate.semaphore.wait()
65+
}
66+
}
67+
await semaphoreTask.value
68+
try Task.checkCancellation()
69+
return true
70+
}
71+
72+
return try await group.next()!
73+
}
74+
} catch {
75+
print("timed out waiting for semaphore")
76+
}
77+
}
78+
3979
func simpleNetworkCallWithDelegate() {
4080
let session = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil)
4181

@@ -48,6 +88,38 @@ func simpleNetworkCallWithDelegate() {
4888
delegate.semaphore.wait()
4989
}
5090

91+
@available(macOS 10.15, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
92+
func asyncNetworkCallWithTaskDelegate() async {
93+
let session = URLSession(configuration: .default)
94+
95+
let url = URL(string: "http://httpbin.org/get")!
96+
let request = URLRequest(url: url)
97+
98+
do {
99+
_ = try await session.data(for: request, delegate: delegate)
100+
} catch {
101+
return
102+
}
103+
104+
await waitForSemaphore(withTimeoutSecs: 3)
105+
}
106+
107+
@available(macOS 10.15, iOS 15.0, tvOS 13.0, *)
108+
func asyncNetworkCallWithSessionDelegate() async {
109+
let session = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil)
110+
111+
let url = URL(string: "http://httpbin.org/get")!
112+
let request = URLRequest(url: url)
113+
114+
do {
115+
_ = try await session.data(for: request)
116+
} catch {
117+
return
118+
}
119+
120+
await waitForSemaphore(withTimeoutSecs: 3)
121+
}
122+
51123
let spanProcessor = SimpleSpanProcessor(spanExporter: StdoutSpanExporter(isDebug: true))
52124
OpenTelemetry.registerTracerProvider(tracerProvider:
53125
TracerProviderBuilder()
@@ -57,6 +129,21 @@ OpenTelemetry.registerTracerProvider(tracerProvider:
57129

58130
let networkInstrumentation = URLSessionInstrumentation(configuration: URLSessionInstrumentationConfiguration())
59131

60-
simpleNetworkCall()
132+
print("making simple call")
133+
var callCount = delegate.callCount
61134
simpleNetworkCallWithDelegate()
135+
assert(delegate.callCount == callCount + 1)
136+
137+
if #available(macOS 10.15, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
138+
print("making simple call with task delegate")
139+
callCount = delegate.callCount
140+
await asyncNetworkCallWithTaskDelegate()
141+
assert(delegate.callCount == callCount + 1, "async task delegate not called")
142+
143+
print("making simple call with session delegate")
144+
callCount = delegate.callCount
145+
await asyncNetworkCallWithSessionDelegate()
146+
assert(delegate.callCount == callCount + 1, "async session delegate not called")
147+
}
148+
62149
sleep(1)

Sources/Exporters/OpenTelemetryProtocolCommon/common/CommonAdapter.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ public enum CommonAdapter {
9595
if let version = instrumentationScopeInfo.version {
9696
instrumentationScope.version = version
9797
}
98-
return instrumentationScope
98+
99+
if let attributes = instrumentationScopeInfo.attributes {
100+
attributes.forEach {
101+
instrumentationScope.attributes.append(CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value))
102+
}
103+
}
104+
return instrumentationScope
99105
}
100106
}

Sources/Importers/OpenTracingShim/TraceShim.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public class TraceShim {
3030
}
3131

3232
private static func getTracer(tracerProvider: TracerProvider) -> Tracer {
33-
tracerProvider.get(instrumentationName: "opentracingshim", instrumentationVersion: nil)
33+
tracerProvider.get(instrumentationName: "opentracingshim")
3434
}
3535
}

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,8 @@ public class URLSessionInstrumentation {
733733
task.setValue(instrumentedRequest, forKey: "currentRequest")
734734
}
735735
self.setIdKey(value: taskId, for: task)
736-
737-
if task.delegate == nil, task.state != .running {
736+
737+
if task.delegate == nil, task.state != .running, (task.value(forKey: "session") as? URLSession)?.delegate == nil {
738738
task.delegate = FakeDelegate()
739739
}
740740
}

Sources/OpenTelemetryApi/Metrics/Stable/DefaultStableMeterProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public class DefaultStableMeterProvider: StableMeterProvider {
3131
self
3232
}
3333

34+
func setAttributes(attributes: [String: AttributeValue]) -> Self {
35+
self
36+
}
37+
3438
func build() -> StableMeter {
3539
Self.noopMeter
3640
}

Sources/OpenTelemetryApi/Metrics/Stable/MeterBuilder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public protocol MeterBuilder: AnyObject {
1818
/// - Returns: self
1919
func setInstrumentationVersion(instrumentationVersion: String) -> Self
2020

21+
/// Assign a set of attributes that will be applied to the Meter.
22+
///
23+
/// - Parameter attributes: key/value-pair of attributes
24+
/// - Returns: self
25+
func setAttributes(attributes: [String: AttributeValue]) -> Self
26+
2127
/// gets or creates a Meter
2228
///
2329
/// - Returns: a Meter configured with the provided options.

Sources/OpenTelemetryApi/Trace/DefaultTracerProvider.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import Foundation
88
public class DefaultTracerProvider: TracerProvider {
99
public static let instance = DefaultTracerProvider()
1010

11-
public func get(instrumentationName: String, instrumentationVersion: String? = nil) -> Tracer {
11+
public func get(instrumentationName: String,
12+
instrumentationVersion: String? = nil,
13+
schemaUrl: String? = nil,
14+
attributes: [String: AttributeValue]? = nil) -> any Tracer {
1215
return DefaultTracer.instance
1316
}
1417
}

Sources/OpenTelemetryApi/Trace/TracerProvider.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@ public protocol TracerProvider {
1111
/// - Parameters:
1212
/// - instrumentationName: the name of the instrumentation library, not the name of the instrumented library
1313
/// - instrumentationVersion: The version of the instrumentation library (e.g., "semver:1.0.0"). Optional
14-
func get(instrumentationName: String, instrumentationVersion: String?) -> Tracer
14+
/// - schemaUrl: The schema url. Optional
15+
/// - attributes: attributes to be associated with span created by this tracer. Optional
16+
func get(
17+
instrumentationName: String,
18+
instrumentationVersion: String?,
19+
schemaUrl: String?,
20+
attributes: [String: AttributeValue]?
21+
) -> any Tracer
1522
}
1623

17-
extension TracerProvider {
18-
func get(instrumentationName: String) -> Tracer {
19-
return get(instrumentationName: instrumentationName, instrumentationVersion: nil)
24+
public extension TracerProvider {
25+
func get(instrumentationName: String,
26+
instrumentationVersion: String? = nil,
27+
schemaUrl: String? = nil,
28+
attributes: [String: AttributeValue]? = nil) -> any Tracer {
29+
return get(
30+
instrumentationName: instrumentationName,
31+
instrumentationVersion: instrumentationVersion,
32+
schemaUrl: schemaUrl,
33+
attributes: attributes
34+
)
2035
}
2136
}

Sources/OpenTelemetryConcurrency/OpenTelemetry.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@ public struct TracerProviderWrapper {
2323
/// The inner `TracerProvider` used to construct a `Tracer`. Be careful when accessing this property, as it may make it easier to use API's that don't function properly with your configuration.
2424
public let inner: TracerProvider
2525

26-
public func get(instrumentationName: String, instrumentationVersion: String?) -> TracerWrapper {
27-
TracerWrapper(inner: inner.get(instrumentationName: instrumentationName, instrumentationVersion: instrumentationVersion))
26+
public func get(instrumentationName: String,
27+
instrumentationVersion: String? = nil,
28+
schemaUrl: String? = nil,
29+
attributes: [String: AttributeValue]? = nil) -> TracerWrapper {
30+
TracerWrapper(
31+
inner: inner
32+
.get(
33+
instrumentationName: instrumentationName,
34+
instrumentationVersion: instrumentationVersion,
35+
schemaUrl: schemaUrl,
36+
attributes: attributes
37+
)
38+
)
2839
}
2940
}
3041

0 commit comments

Comments
 (0)