Skip to content

Commit ee11776

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #165 from bryce-b/resource-extension
Added Resource Extension to generate OS attributes This provides a number of resource generating objects that captures various OS related attributes for use with TracerProviders.
2 parents 26095e3 + 2c434f8 commit ee11776

25 files changed

+855
-43
lines changed

Examples/Simple Exporter/main.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ import Foundation
1717
import JaegerExporter
1818
import OpenTelemetryApi
1919
import OpenTelemetrySdk
20+
import ResourceExtension
2021
import StdoutExporter
2122
import ZipkinExporter
2223

2324
let sampleKey = "sampleKey"
2425
let sampleValue = "sampleValue"
2526

27+
let resources = DefaultResources().get()
28+
2629
let instrumentationLibraryName = "SimpleExporter"
2730
let instrumentationLibraryVersion = "semver:0.1.0"
2831
var instrumentationLibraryInfo = InstrumentationLibraryInfo(name: instrumentationLibraryName, version: instrumentationLibraryVersion)

Package.swift

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ let package = Package(
1414
.library(name: "libOpenTelemetryApi", type: .static, targets: ["OpenTelemetryApi"]),
1515
.library(name: "OpenTelemetrySdk", type: .dynamic, targets: ["OpenTelemetrySdk"]),
1616
.library(name: "libOpenTelemetrySdk", type: .static, targets: ["OpenTelemetrySdk"]),
17+
.library(name: "ResourceExtension", type: .dynamic, targets: ["ResourceExtension"]),
18+
.library(name: "libResourceExtension", type: .static, targets: ["ResourceExtension"]),
1719
.library(name: "OpenTracingShim-experimental", type: .dynamic, targets: ["OpenTracingShim"]),
1820
.library(name: "libOpenTracingShim-experimental", type: .static, targets: ["OpenTracingShim"]),
1921
.library(name: "SwiftMetricsShim-experimental", type: .dynamic, targets: ["SwiftMetricsShim"]),
@@ -45,12 +47,15 @@ let package = Package(
4547
],
4648
targets: [
4749
.target(name: "OpenTelemetryApi",
48-
dependencies: []
49-
),
50+
dependencies: []),
51+
5052
.target(name: "OpenTelemetrySdk",
5153
dependencies: ["OpenTelemetryApi",
52-
.product(name: "Atomics", package: "swift-atomics")]
53-
),
54+
.product(name: "Atomics", package: "swift-atomics")]),
55+
.target(name: "ResourceExtension",
56+
dependencies: ["OpenTelemetrySdk"],
57+
path: "Sources/Instrumentation/SDKResourceExtension",
58+
exclude: ["README.md"]),
5459
.target(name: "OpenTracingShim",
5560
dependencies: ["OpenTelemetrySdk",
5661
"Opentracing"],
@@ -66,40 +71,35 @@ let package = Package(
6671
.target(name: "JaegerExporter",
6772
dependencies: ["OpenTelemetrySdk",
6873
.product(name: "Thrift", package: "Thrift")],
69-
path: "Sources/Exporters/Jaeger"
70-
),
74+
path: "Sources/Exporters/Jaeger"),
7175
.target(name: "ZipkinExporter",
7276
dependencies: ["OpenTelemetrySdk"],
73-
path: "Sources/Exporters/Zipkin"
74-
),
77+
path: "Sources/Exporters/Zipkin"),
7578
.target(name: "PrometheusExporter",
7679
dependencies: ["OpenTelemetrySdk",
7780
.product(name: "NIO", package: "swift-nio"),
7881
.product(name: "NIOHTTP1", package: "swift-nio")],
79-
path: "Sources/Exporters/Prometheus"
80-
),
82+
path: "Sources/Exporters/Prometheus"),
8183
.target(name: "OpenTelemetryProtocolExporter",
8284
dependencies: ["OpenTelemetrySdk",
8385
.product(name: "GRPC", package: "grpc-swift")],
84-
path: "Sources/Exporters/OpenTelemetryProtocol"
85-
),
86+
path: "Sources/Exporters/OpenTelemetryProtocol"),
8687
.target(name: "StdoutExporter",
8788
dependencies: ["OpenTelemetrySdk"],
88-
path: "Sources/Exporters/Stdout"
89-
),
89+
path: "Sources/Exporters/Stdout"),
9090
.target(name: "InMemoryExporter",
9191
dependencies: ["OpenTelemetrySdk"],
92-
path: "Sources/Exporters/InMemory"
93-
),
92+
path: "Sources/Exporters/InMemory"),
9493
.target(name: "DatadogExporter",
9594
dependencies: ["OpenTelemetrySdk"],
9695
path: "Sources/Exporters/DatadogExporter",
97-
exclude: ["NOTICE", "README.md"]
98-
),
96+
exclude: ["NOTICE", "README.md"]),
9997
.testTarget(name: "OpenTelemetryApiTests",
10098
dependencies: ["OpenTelemetryApi"],
101-
path: "Tests/OpenTelemetryApiTests"
102-
),
99+
path: "Tests/OpenTelemetryApiTests"),
100+
.testTarget(name: "ResourceExtensionTests",
101+
dependencies: ["ResourceExtension", "OpenTelemetrySdk"],
102+
path: "Tests/InstrumentationTests/sdk-resource-extension-tests"),
103103
.testTarget(name: "OpenTracingShimTests",
104104
dependencies: ["OpenTracingShim",
105105
"OpenTelemetrySdk"],
@@ -113,52 +113,41 @@ let package = Package(
113113
.testTarget(name: "OpenTelemetrySdkTests",
114114
dependencies: ["OpenTelemetryApi",
115115
"OpenTelemetrySdk"],
116-
path: "Tests/OpenTelemetrySdkTests"
117-
),
116+
path: "Tests/OpenTelemetrySdkTests"),
118117
.testTarget(name: "JaegerExporterTests",
119118
dependencies: ["JaegerExporter"],
120-
path: "Tests/ExportersTests/Jaeger"
121-
),
119+
path: "Tests/ExportersTests/Jaeger"),
122120
.testTarget(name: "ZipkinExporterTests",
123121
dependencies: ["ZipkinExporter"],
124-
path: "Tests/ExportersTests/Zipkin"
125-
),
122+
path: "Tests/ExportersTests/Zipkin"),
126123
.testTarget(name: "PrometheusExporterTests",
127124
dependencies: ["PrometheusExporter"],
128-
path: "Tests/ExportersTests/Prometheus"
129-
),
125+
path: "Tests/ExportersTests/Prometheus"),
130126
.testTarget(name: "OpenTelemetryProtocolExporterTests",
131127
dependencies: ["OpenTelemetryProtocolExporter"],
132-
path: "Tests/ExportersTests/OpenTelemetryProtocol"
133-
),
128+
path: "Tests/ExportersTests/OpenTelemetryProtocol"),
134129
.testTarget(name: "InMemoryExporterTests",
135130
dependencies: ["InMemoryExporter"],
136-
path: "Tests/ExportersTests/InMemory"
137-
),
131+
path: "Tests/ExportersTests/InMemory"),
138132
.testTarget(name: "DatadogExporterTests",
139133
dependencies: ["DatadogExporter",
140134
.product(name: "NIO", package: "swift-nio"),
141135
.product(name: "NIOHTTP1", package: "swift-nio")],
142-
path: "Tests/ExportersTests/DatadogExporter"
143-
),
136+
path: "Tests/ExportersTests/DatadogExporter"),
144137
.target(name: "LoggingTracer",
145138
dependencies: ["OpenTelemetryApi"],
146-
path: "Examples/Logging Tracer"
147-
),
139+
path: "Examples/Logging Tracer"),
148140
.target(name: "SimpleExporter",
149-
dependencies: ["OpenTelemetrySdk", "JaegerExporter", "StdoutExporter", "ZipkinExporter"],
141+
dependencies: ["OpenTelemetrySdk", "JaegerExporter", "StdoutExporter", "ZipkinExporter", "ResourceExtension"],
150142
path: "Examples/Simple Exporter",
151-
exclude: ["README.md"]
152-
),
143+
exclude: ["README.md"]),
153144
.target(name: "PrometheusSample",
154145
dependencies: ["OpenTelemetrySdk", "PrometheusExporter"],
155146
path: "Examples/Prometheus Sample",
156-
exclude: ["README.md"]
157-
),
147+
exclude: ["README.md"]),
158148
.target(name: "DatadogSample",
159149
dependencies: ["DatadogExporter"],
160150
path: "Examples/Datadog Sample",
161-
exclude: ["README.md"]
162-
),
151+
exclude: ["README.md"]),
163152
]
164153
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2021, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import Foundation
17+
import OpenTelemetryApi
18+
import OpenTelemetrySdk
19+
20+
public class ApplicationResourceProvider: ResourceProvider {
21+
let applicationDataSource: IApplicationDataSource
22+
23+
public init(source: IApplicationDataSource) {
24+
applicationDataSource = source
25+
}
26+
27+
override public var attributes: [String: AttributeValue] {
28+
var attributes = [String: AttributeValue]()
29+
30+
if let bundleName = applicationDataSource.name {
31+
attributes[ResourceAttributes.serviceName.rawValue] = AttributeValue.string(bundleName)
32+
}
33+
34+
if let version = applicationVersion() {
35+
attributes[ResourceAttributes.serviceVersion.rawValue] = AttributeValue.string(version)
36+
}
37+
38+
if let bundleId = applicationDataSource.identifier {
39+
attributes[ResourceAttributes.serviceNamespace.rawValue] = AttributeValue.string(bundleId)
40+
}
41+
42+
return attributes
43+
}
44+
45+
func applicationVersion() -> String? {
46+
if let build = applicationDataSource.build {
47+
if let version = applicationDataSource.version {
48+
return "\(version) (\(build))"
49+
}
50+
return build
51+
} else {
52+
return applicationDataSource.version
53+
}
54+
}
55+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import Foundation
17+
18+
public class ApplicationDataSource: IApplicationDataSource {
19+
public var name: String? {
20+
Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String
21+
}
22+
23+
public var identifier: String? {
24+
Bundle.main.infoDictionary?[kCFBundleIdentifierKey as String] as? String
25+
}
26+
27+
public var version: String? {
28+
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
29+
}
30+
31+
public var build: String? {
32+
Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String
33+
}
34+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2021, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import Foundation
17+
#if os(watchOS)
18+
import WatchKit
19+
#elseif os(macOS)
20+
import AppKit
21+
#else
22+
import UIKit
23+
#endif
24+
public class DeviceDataSource: IDeviceDataSource {
25+
public var model: String? {
26+
#if os(watchOS)
27+
return WKInterfaceDevice.current().localizedModel
28+
#else
29+
let hwName = UnsafeMutablePointer<Int32>.allocate(capacity: 2)
30+
hwName[0] = CTL_HW
31+
#if os(macOS)
32+
hwName[1] = HW_MODEL
33+
#else
34+
hwName[1] = HW_MACHINE
35+
#endif
36+
let machine = UnsafeMutablePointer<CChar>.allocate(capacity: 255)
37+
let len: UnsafeMutablePointer<Int>! = UnsafeMutablePointer<Int>.allocate(capacity: 1)
38+
39+
let error = sysctl(hwName, 2, machine, len, nil, 0)
40+
if error != 0 {
41+
// TODO: better error log
42+
print("error #\(errno): \(String(describing: String(utf8String: strerror(errno))))")
43+
44+
return nil
45+
}
46+
let machineName = String(cString: machine)
47+
return machineName
48+
#endif
49+
}
50+
51+
public var identifier: String? {
52+
#if os(watchOS)
53+
if #available(watchOS 6.3, *) {
54+
return WKInterfaceDevice.current().identifierForVendor?.uuidString
55+
} else {
56+
return nil
57+
}
58+
#elseif os(macOS)
59+
return nil
60+
#else
61+
return UIDevice.current.identifierForVendor?.uuidString
62+
63+
#endif
64+
}
65+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2021, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import Foundation
17+
18+
public protocol IApplicationDataSource {
19+
var name: String? { get }
20+
var identifier: String? { get }
21+
var version: String? { get }
22+
var build: String? { get }
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import Foundation
17+
18+
public protocol IDeviceDataSource {
19+
var identifier: String? { get }
20+
var model: String? { get }
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
import Foundation
17+
18+
public protocol IOperatingSystemDataSource {
19+
var type: String { get }
20+
var description: String { get }
21+
}

0 commit comments

Comments
 (0)