Skip to content

Commit 408af2d

Browse files
semicoleonbryce-b
andauthored
Add Linux support via a TaskLocalContextManager (#546)
* Basic linux support via a concurrency based wrapper library * Fix/don't run some tests when they can't pass on the current platform. Remove some unnecessary 'Base' protocols in favor of wrapper types in OpenTelemetryConcurrency * Remove swift-atomics from the direct dependencies on Darwin platforms * Remove unnecessary protocol functions, and add some documentation comments * Update some more doc comments * Rename OpenTelemetryTestCase to OpenTelemetryContextTestCase * Remove an unnecessary import and assertion * Remove debugging code * Add linux test github action * Add comments and clean up some issues in the tests * Add a concurrency example, and allow SpanBuilderBase to startSpans * Apply extensions to SpanBuilderBase instead of SpanBuilder * Split building and running the tests into separate steps * Avoid using docker * Force noninteractive swiftly installation * Try removing complex test case overrides * Revert "Try removing complex test case overrides" This reverts commit c982b2e. * Revert "Force noninteractive swiftly installation" This reverts commit a8bc0ac. * Revert "Avoid using docker" This reverts commit 387b99f. * Allow batch worker threads to exit once they are cancelled, and increase scheduleDelay on batch workers to avoid keeping the condition variables locked * Also check for cancellation on the inner loop in BatchWorker * Update Package.swift * Update [email protected] * Update [email protected] * fixed additional package issues * added additional test skip --------- Co-authored-by: Bryce Buchanan <[email protected]> Co-authored-by: Bryce Buchanan <[email protected]>
1 parent 2df4725 commit 408af2d

File tree

65 files changed

+1575
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1575
-408
lines changed

.github/workflows/BuildAndTest.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ jobs:
4545
run: make build-for-testing-watchos
4646
- name: Test for watchOS
4747
run: make test-without-building-watchos
48+
linux:
49+
runs-on: ubuntu-latest
50+
container: swift:5.10
51+
steps:
52+
- uses: actions/checkout@v2
53+
- name: Build tests for Linux
54+
run: swift build --build-tests
55+
- name: Run tests for Linux
56+
run: swift test
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#if canImport(_Concurrency)
7+
import OpenTelemetrySdk
8+
import OpenTelemetryConcurrency
9+
import StdoutExporter
10+
11+
let sampleKey = "sampleKey"
12+
let sampleValue = "sampleValue"
13+
14+
// On Apple platforms, the default is the activity based context manager. We want to opt-in to the structured concurrency based context manager instead.
15+
OpenTelemetry.registerDefaultConcurrencyContextManager()
16+
17+
let stdout = StdoutExporter()
18+
OpenTelemetry.registerTracerProvider(
19+
tracerProvider: TracerProviderBuilder().add(
20+
spanProcessor: SimpleSpanProcessor(spanExporter: stdout)
21+
).build()
22+
)
23+
24+
let tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "ConcurrencyContext", instrumentationVersion: "semver:0.1.0")
25+
26+
extension Task where Failure == Never, Success == Never {
27+
static func sleep(seconds: Double) async throws {
28+
try await self.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
29+
}
30+
}
31+
32+
func simpleSpan() async throws {
33+
let span = tracer.spanBuilder(spanName: "SimpleSpan").setSpanKind(spanKind: .client).startSpan()
34+
span.setAttribute(key: sampleKey, value: sampleValue)
35+
try await Task.sleep(seconds: 0.5)
36+
span.end()
37+
}
38+
39+
func childSpan() async throws {
40+
// SpanBuilder's `setActive` method is not available here, since it isn't compatible with structured concurrency based context management
41+
try await tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).withActiveSpan { span in
42+
span.setAttribute(key: sampleKey, value: sampleValue)
43+
await Task.detached {
44+
// A detached task doesn't inherit the task local context, so this span won't have a parent.
45+
let notAChildSpan = tracer.spanBuilder(spanName: "notAChild").setSpanKind(spanKind: .client).startSpan()
46+
notAChildSpan.setAttribute(key: sampleKey, value: sampleValue)
47+
notAChildSpan.end()
48+
}.value
49+
50+
try await Task {
51+
// Normal tasks should still inherit the context.
52+
try await Task.sleep(seconds: 0.2)
53+
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
54+
childSpan.setAttribute(key: sampleKey, value: sampleValue)
55+
try await Task.sleep(seconds: 0.5)
56+
childSpan.end()
57+
}.value
58+
}
59+
}
60+
61+
try await simpleSpan()
62+
try await Task.sleep(seconds: 1)
63+
try await childSpan()
64+
try await Task.sleep(seconds: 1)
65+
66+
#endif

Examples/Logging Tracer/LoggingTracer.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,23 @@ class LoggingTracer: Tracer {
6969
func setActive(_ active: Bool) -> Self {
7070
return self
7171
}
72+
73+
func withActiveSpan<T>(_ operation: (any SpanBase) throws -> T) rethrows -> T {
74+
let span = self.startSpan()
75+
defer {
76+
span.end()
77+
}
78+
return try operation(span)
79+
}
80+
81+
#if canImport(_Concurrency)
82+
func withActiveSpan<T>(_ operation: (any SpanBase) async throws -> T) async rethrows -> T {
83+
let span = self.startSpan()
84+
defer {
85+
span.end()
86+
}
87+
return try await operation(span)
88+
}
89+
#endif
7290
}
7391
}

Package.resolved

Lines changed: 40 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ let package = Package(
77
name: "opentelemetry-swift",
88
platforms: [
99
.macOS(.v10_13),
10-
.iOS(.v11),
11-
.tvOS(.v11)
10+
.iOS(.v12),
11+
.tvOS(.v12)
1212
],
1313
products: [
1414
.library(name: "OpenTelemetryApi", type: .static, targets: ["OpenTelemetryApi"]),
@@ -28,29 +28,31 @@ let package = Package(
2828
.library(name: "InMemoryExporter", type: .static, targets: ["InMemoryExporter"]),
2929
.library(name: "DatadogExporter", type: .static, targets: ["DatadogExporter"]),
3030
.library(name: "NetworkStatus", type: .static, targets: ["NetworkStatus"]),
31-
.library(name: "OTelSwiftLog" type: .static, targets: ["OTelSwiftLog"])
31+
.library(name: "OTelSwiftLog", type: .static, targets: ["OTelSwiftLog"]),
3232
.executable(name: "simpleExporter", targets: ["SimpleExporter"]),
3333
.executable(name: "OTLPExporter", targets: ["OTLPExporter"]),
3434
.executable(name: "OTLPHTTPExporter", targets: ["OTLPHTTPExporter"]),
3535
.executable(name: "loggingTracer", targets: ["LoggingTracer"]),
3636
],
3737
dependencies: [
38-
.package(name: "Opentracing", url: "https://github.com/undefinedlabs/opentracing-objc", exact: "0.5.2"),
39-
.package(name: "Thrift", url: "https://github.com/undefinedlabs/Thrift-Swift", exact: "1.1.1"),
40-
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio.git", exact: "2.0.0"),
41-
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", exact: "1.0.0"),
42-
.package(name: "swift-protobuf", url: "https://github.com/apple/swift-protobuf.git", exact: "1.20.2"),
43-
.package(name: "swift-log", url: "https://github.com/apple/swift-log.git", exact: "1.4.4"),
44-
.package(name: "swift-metrics", url: "https://github.com/apple/swift-metrics.git", exact: "2.1.1"),
45-
.package(name: "Reachability.swift", url: "https://github.com/ashleymills/Reachability.swift", exact: "5.1.0")
38+
.package(url: "https://github.com/undefinedlabs/opentracing-objc", exact: "0.5.2"),
39+
.package(url: "https://github.com/undefinedlabs/Thrift-Swift", exact: "1.1.1"),
40+
.package(url: "https://github.com/apple/swift-nio.git", exact: "2.0.0"),
41+
.package(url: "https://github.com/grpc/grpc-swift.git", exact: "1.0.0"),
42+
.package(url: "https://github.com/apple/swift-protobuf.git", exact: "1.20.2"),
43+
.package(url: "https://github.com/apple/swift-log.git", exact: "1.4.4"),
44+
.package(url: "https://github.com/apple/swift-metrics.git", exact: "2.1.1"),
45+
.package(url: "https://github.com/ashleymills/Reachability.swift", exact: "5.1.0")
4646
],
4747
targets: [
4848
.target(name: "OpenTelemetryApi",
4949
dependencies: []),
5050
.target(name: "OpenTelemetrySdk",
5151
dependencies: ["OpenTelemetryApi"]),
52+
.target(name: "OpenTelemetryTestUtils",
53+
dependencies: ["OpenTelemetryApi", "OpenTelemetrySdk"]),
5254
.target(name: "OTelSwiftLog",
53-
dependencies: ["OpenTelemetryApigi",
55+
dependencies: ["OpenTelemetryApi",
5456
.product(name: "Logging", package: "swift-log")],
5557
path: "Sources/Bridges/OTelSwiftLog"),
5658
.target(name: "ResourceExtension",
@@ -67,7 +69,7 @@ let package = Package(
6769
.product(name: "Reachability", package: "Reachability.swift")
6870
],
6971
path: "Sources/Instrumentation/NetworkStatus",
70-
linkerSettings: [.linkedFramework("CoreTelephony", .when(platforms: [.iOS], configuration: nil))]),
72+
linkerSettings: [.linkedFramework("CoreTelephony", .when(platforms: [.iOS]))]),
7173
.target(name: "SignPostIntegration",
7274
dependencies: ["OpenTelemetrySdk"],
7375
path: "Sources/Instrumentation/SignPostIntegration",
@@ -128,7 +130,7 @@ let package = Package(
128130
dependencies: ["NetworkStatus", .product(name: "Reachability", package: "Reachability.swift")],
129131
path: "Tests/InstrumentationTests/NetworkStatusTests"),
130132
.testTarget(name: "OpenTelemetryApiTests",
131-
dependencies: ["OpenTelemetryApi"],
133+
dependencies: ["OpenTelemetryApi", "OpenTelemetryTestUtils"],
132134
path: "Tests/OpenTelemetryApiTests"),
133135
.testTarget(name: "OpenTelemetrySdkTests",
134136
dependencies: ["OpenTelemetrySdk"],

[email protected]

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ let package = Package(
5050
dependencies: []),
5151
.target(name: "OpenTelemetrySdk",
5252
dependencies: ["OpenTelemetryApi"]),
53+
.target(name: "OpenTelemetryTestUtils",
54+
dependencies: ["OpenTelemetryApi", "OpenTelemetrySdk"]),
5355
.target(name: "ResourceExtension",
5456
dependencies: ["OpenTelemetrySdk"],
5557
path: "Sources/Instrumentation/SDKResourceExtension",
@@ -136,11 +138,12 @@ let package = Package(
136138
],
137139
path: "Tests/InstrumentationTests/NetworkStatusTests"),
138140
.testTarget(name: "OpenTelemetryApiTests",
139-
dependencies: ["OpenTelemetryApi"],
141+
dependencies: ["OpenTelemetryApi", "OpenTelemetryTestUtils"],
140142
path: "Tests/OpenTelemetryApiTests"),
141143
.testTarget(name: "OpenTelemetrySdkTests",
142144
dependencies: ["OpenTelemetryApi",
143-
"OpenTelemetrySdk"],
145+
"OpenTelemetrySdk",
146+
"OpenTelemetryTestUtils"],
144147
path: "Tests/OpenTelemetrySdkTests"),
145148
.testTarget(name: "ResourceExtensionTests",
146149
dependencies: ["ResourceExtension", "OpenTelemetrySdk"],

0 commit comments

Comments
 (0)