Skip to content

Commit 5839cb5

Browse files
updated instrument scope to include 'attributes' (#753)
* updated instrument scope to include 'attributes' * removed errant comma --------- Co-authored-by: Ignacio Bonafonte <[email protected]>
1 parent 5999e62 commit 5839cb5

File tree

16 files changed

+154
-23
lines changed

16 files changed

+154
-23
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
}

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/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

Sources/OpenTelemetrySdk/Common/ComponentRegistry.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ComponentRegistry<T> {
2020
self.builder = builder
2121
}
2222

23-
public func get(name: String, version: String? = nil, schemaUrl: String? = nil) -> T {
23+
public func get(name: String, version: String? = nil, schemaUrl: String? = nil, attributes: [String: AttributeValue]? = nil) -> T {
2424
lock.lock()
2525
defer {
2626
lock.unlock()
@@ -35,7 +35,7 @@ class ComponentRegistry<T> {
3535
}
3636

3737
if componentByNameVersionSchema[name]![version]![schemaUrl] == nil {
38-
componentByNameVersionSchema[name]![version]![schemaUrl] = buildComponent(InstrumentationScopeInfo(name: name, version: version, schemaUrl: schemaUrl))
38+
componentByNameVersionSchema[name]![version]![schemaUrl] = buildComponent(InstrumentationScopeInfo(name: name, version: version, schemaUrl: schemaUrl, attributes: attributes))
3939
}
4040
return componentByNameVersionSchema[name]![version]![schemaUrl]!
4141
} else if let version {
@@ -44,7 +44,13 @@ class ComponentRegistry<T> {
4444
}
4545

4646
if componentByNameVersion[name]![version] == nil {
47-
componentByNameVersion[name]![version] = buildComponent(InstrumentationScopeInfo(name: name, version: version))
47+
componentByNameVersion[name]![version] = buildComponent(
48+
InstrumentationScopeInfo(
49+
name: name,
50+
version: version,
51+
attributes: attributes
52+
)
53+
)
4854
}
4955

5056
return componentByNameVersion[name]![version]!
@@ -55,14 +61,22 @@ class ComponentRegistry<T> {
5561
}
5662

5763
if componentByNameSchema[name]![schemaUrl] == nil {
58-
componentByNameSchema[name]![schemaUrl] = buildComponent(InstrumentationScopeInfo(name: name, schemaUrl: schemaUrl))
64+
componentByNameSchema[name]![schemaUrl] = buildComponent(
65+
InstrumentationScopeInfo(
66+
name: name,
67+
schemaUrl: schemaUrl,
68+
attributes: attributes
69+
)
70+
)
5971
}
6072

6173
return componentByNameSchema[name]![schemaUrl]!
6274

6375
} else {
6476
if componentByName[name] == nil {
65-
componentByName[name] = buildComponent(InstrumentationScopeInfo(name: name))
77+
componentByName[name] = buildComponent(
78+
InstrumentationScopeInfo(name: name, attributes: attributes)
79+
)
6680
}
6781
return componentByName[name]!
6882
}

Sources/OpenTelemetrySdk/Common/InstrumentationLibraryInfo.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
*/
55

66
import Foundation
7+
import OpenTelemetryApi
78

89
/// Holds information about the instrumentation library specified when creating an instance of
910
/// TracerSdk using TracerProviderSdk.
1011
public struct InstrumentationScopeInfo: Hashable, Codable, Equatable {
1112
public private(set) var name: String = ""
1213
public private(set) var version: String?
1314
public private(set) var schemaUrl: String?
15+
public private(set) var attributes: [String: AttributeValue]?
1416

1517
/// Creates a new empty instance of InstrumentationScopeInfo.
1618
public init() {}
@@ -19,9 +21,10 @@ public struct InstrumentationScopeInfo: Hashable, Codable, Equatable {
1921
/// - Parameters:
2022
/// - name: name of the instrumentation library
2123
/// - version: version of the instrumentation library (e.g., "semver:1.0.0"), might be nil
22-
public init(name: String, version: String? = nil, schemaUrl: String? = nil) {
24+
public init(name: String, version: String? = nil, schemaUrl: String? = nil, attributes: [String: AttributeValue]? = nil) {
2325
self.name = name
2426
self.version = version
2527
self.schemaUrl = schemaUrl
28+
self.attributes = attributes
2629
}
2730
}

0 commit comments

Comments
 (0)