Skip to content

Commit

Permalink
Add Linux support via a TaskLocalContextManager (#546)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent 2df4725 commit 408af2d
Show file tree
Hide file tree
Showing 65 changed files with 1,575 additions and 408 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/BuildAndTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ jobs:
run: make build-for-testing-watchos
- name: Test for watchOS
run: make test-without-building-watchos
linux:
runs-on: ubuntu-latest
container: swift:5.10
steps:
- uses: actions/checkout@v2
- name: Build tests for Linux
run: swift build --build-tests
- name: Run tests for Linux
run: swift test
66 changes: 66 additions & 0 deletions Examples/ConcurrencyContext/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

#if canImport(_Concurrency)
import OpenTelemetrySdk
import OpenTelemetryConcurrency
import StdoutExporter

let sampleKey = "sampleKey"
let sampleValue = "sampleValue"

// On Apple platforms, the default is the activity based context manager. We want to opt-in to the structured concurrency based context manager instead.
OpenTelemetry.registerDefaultConcurrencyContextManager()

let stdout = StdoutExporter()
OpenTelemetry.registerTracerProvider(
tracerProvider: TracerProviderBuilder().add(
spanProcessor: SimpleSpanProcessor(spanExporter: stdout)
).build()
)

let tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "ConcurrencyContext", instrumentationVersion: "semver:0.1.0")

extension Task where Failure == Never, Success == Never {
static func sleep(seconds: Double) async throws {
try await self.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
}
}

func simpleSpan() async throws {
let span = tracer.spanBuilder(spanName: "SimpleSpan").setSpanKind(spanKind: .client).startSpan()
span.setAttribute(key: sampleKey, value: sampleValue)
try await Task.sleep(seconds: 0.5)
span.end()
}

func childSpan() async throws {
// SpanBuilder's `setActive` method is not available here, since it isn't compatible with structured concurrency based context management
try await tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).withActiveSpan { span in
span.setAttribute(key: sampleKey, value: sampleValue)
await Task.detached {
// A detached task doesn't inherit the task local context, so this span won't have a parent.
let notAChildSpan = tracer.spanBuilder(spanName: "notAChild").setSpanKind(spanKind: .client).startSpan()
notAChildSpan.setAttribute(key: sampleKey, value: sampleValue)
notAChildSpan.end()
}.value

try await Task {
// Normal tasks should still inherit the context.
try await Task.sleep(seconds: 0.2)
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
childSpan.setAttribute(key: sampleKey, value: sampleValue)
try await Task.sleep(seconds: 0.5)
childSpan.end()
}.value
}
}

try await simpleSpan()
try await Task.sleep(seconds: 1)
try await childSpan()
try await Task.sleep(seconds: 1)

#endif
18 changes: 18 additions & 0 deletions Examples/Logging Tracer/LoggingTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,23 @@ class LoggingTracer: Tracer {
func setActive(_ active: Bool) -> Self {
return self
}

func withActiveSpan<T>(_ operation: (any SpanBase) throws -> T) rethrows -> T {
let span = self.startSpan()
defer {
span.end()
}
return try operation(span)
}

#if canImport(_Concurrency)
func withActiveSpan<T>(_ operation: (any SpanBase) async throws -> T) async rethrows -> T {
let span = self.startSpan()
defer {
span.end()
}
return try await operation(span)
}
#endif
}
}
62 changes: 40 additions & 22 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/grpc/grpc-swift.git",
"state" : {
"revision" : "87cecdeb2aae6b359b754d0dc7099e8237cf1824",
"version" : "1.11.0"
"revision" : "6a90b7e77e29f9bda6c2b3a4165a40d6c02cfda1",
"version" : "1.23.0"
}
},
{
Expand All @@ -23,89 +23,107 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "919eb1d83e02121cdb434c7bfc1f0c66ef17febe",
"version" : "1.0.2"
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "f504716c27d2e5d4144fa4794b12129301d17729",
"version" : "1.0.3"
"revision" : "ee97538f5b81ae89698fd95938896dec5217b148",
"version" : "1.1.1"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "9bee2fdb79cc740081abd8ebd80738063d632286",
"version" : "1.1.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "6fe203dc33195667ce1759bf0182975e4653ba1c",
"version" : "1.4.4"
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
}
},
{
"identity" : "swift-metrics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-metrics.git",
"state" : {
"revision" : "3edd2f57afc4e68e23c3e4956bc8b65ca6b5b2ff",
"version" : "2.2.0"
"revision" : "ce594e71e92a1610015017f83f402894df540e51",
"version" : "2.4.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "bc4c55b9f9584f09eb971d67d956e28d08caa9d0",
"version" : "2.43.1"
"revision" : "9428f62793696d9a0cc1f26a63f63bb31da0516d",
"version" : "2.66.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "fb70a0f5e984f23be48b11b4f1909f3bee016178",
"version" : "1.19.1"
"revision" : "a3b640d7dc567225db7c94386a6e71aded1bfa63",
"version" : "1.22.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "00576e6f1efa5c46dca2ca3081dc56dd233b402d",
"version" : "1.23.0"
"revision" : "8d8eb609929aee75336a0a3d2417280786265868",
"version" : "1.32.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "5e68c1ded15619bb281b273fa8c2d8fd7f7b2b7d",
"version" : "2.16.1"
"revision" : "2b09805797f21c380f7dc9bedaab3157c5508efb",
"version" : "2.27.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "e7f5278a26442dc46783ba7e063643d524e414a0",
"version" : "1.11.3"
"revision" : "38ac8221dd20674682148d6451367f89c2652980",
"version" : "1.21.0"
}
},
{
"identity" : "swift-protobuf",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf.git",
"state" : {
"revision" : "88c7d15e1242fdb6ecbafbc7926426a19be1e98a",
"version" : "1.20.2"
"revision" : "9f0c76544701845ad98716f3f6a774a892152bcb",
"version" : "1.26.0"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "f9266c85189c2751589a50ea5aec72799797e471",
"version" : "1.3.0"
}
},
{
Expand Down
30 changes: 16 additions & 14 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ let package = Package(
name: "opentelemetry-swift",
platforms: [
.macOS(.v10_13),
.iOS(.v11),
.tvOS(.v11)
.iOS(.v12),
.tvOS(.v12)
],
products: [
.library(name: "OpenTelemetryApi", type: .static, targets: ["OpenTelemetryApi"]),
Expand All @@ -28,29 +28,31 @@ let package = Package(
.library(name: "InMemoryExporter", type: .static, targets: ["InMemoryExporter"]),
.library(name: "DatadogExporter", type: .static, targets: ["DatadogExporter"]),
.library(name: "NetworkStatus", type: .static, targets: ["NetworkStatus"]),
.library(name: "OTelSwiftLog" type: .static, targets: ["OTelSwiftLog"])
.library(name: "OTelSwiftLog", type: .static, targets: ["OTelSwiftLog"]),
.executable(name: "simpleExporter", targets: ["SimpleExporter"]),
.executable(name: "OTLPExporter", targets: ["OTLPExporter"]),
.executable(name: "OTLPHTTPExporter", targets: ["OTLPHTTPExporter"]),
.executable(name: "loggingTracer", targets: ["LoggingTracer"]),
],
dependencies: [
.package(name: "Opentracing", url: "https://github.com/undefinedlabs/opentracing-objc", exact: "0.5.2"),
.package(name: "Thrift", url: "https://github.com/undefinedlabs/Thrift-Swift", exact: "1.1.1"),
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio.git", exact: "2.0.0"),
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", exact: "1.0.0"),
.package(name: "swift-protobuf", url: "https://github.com/apple/swift-protobuf.git", exact: "1.20.2"),
.package(name: "swift-log", url: "https://github.com/apple/swift-log.git", exact: "1.4.4"),
.package(name: "swift-metrics", url: "https://github.com/apple/swift-metrics.git", exact: "2.1.1"),
.package(name: "Reachability.swift", url: "https://github.com/ashleymills/Reachability.swift", exact: "5.1.0")
.package(url: "https://github.com/undefinedlabs/opentracing-objc", exact: "0.5.2"),
.package(url: "https://github.com/undefinedlabs/Thrift-Swift", exact: "1.1.1"),
.package(url: "https://github.com/apple/swift-nio.git", exact: "2.0.0"),
.package(url: "https://github.com/grpc/grpc-swift.git", exact: "1.0.0"),
.package(url: "https://github.com/apple/swift-protobuf.git", exact: "1.20.2"),
.package(url: "https://github.com/apple/swift-log.git", exact: "1.4.4"),
.package(url: "https://github.com/apple/swift-metrics.git", exact: "2.1.1"),
.package(url: "https://github.com/ashleymills/Reachability.swift", exact: "5.1.0")
],
targets: [
.target(name: "OpenTelemetryApi",
dependencies: []),
.target(name: "OpenTelemetrySdk",
dependencies: ["OpenTelemetryApi"]),
.target(name: "OpenTelemetryTestUtils",
dependencies: ["OpenTelemetryApi", "OpenTelemetrySdk"]),
.target(name: "OTelSwiftLog",
dependencies: ["OpenTelemetryApigi",
dependencies: ["OpenTelemetryApi",
.product(name: "Logging", package: "swift-log")],
path: "Sources/Bridges/OTelSwiftLog"),
.target(name: "ResourceExtension",
Expand All @@ -67,7 +69,7 @@ let package = Package(
.product(name: "Reachability", package: "Reachability.swift")
],
path: "Sources/Instrumentation/NetworkStatus",
linkerSettings: [.linkedFramework("CoreTelephony", .when(platforms: [.iOS], configuration: nil))]),
linkerSettings: [.linkedFramework("CoreTelephony", .when(platforms: [.iOS]))]),
.target(name: "SignPostIntegration",
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Instrumentation/SignPostIntegration",
Expand Down Expand Up @@ -128,7 +130,7 @@ let package = Package(
dependencies: ["NetworkStatus", .product(name: "Reachability", package: "Reachability.swift")],
path: "Tests/InstrumentationTests/NetworkStatusTests"),
.testTarget(name: "OpenTelemetryApiTests",
dependencies: ["OpenTelemetryApi"],
dependencies: ["OpenTelemetryApi", "OpenTelemetryTestUtils"],
path: "Tests/OpenTelemetryApiTests"),
.testTarget(name: "OpenTelemetrySdkTests",
dependencies: ["OpenTelemetrySdk"],
Expand Down
7 changes: 5 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ let package = Package(
dependencies: []),
.target(name: "OpenTelemetrySdk",
dependencies: ["OpenTelemetryApi"]),
.target(name: "OpenTelemetryTestUtils",
dependencies: ["OpenTelemetryApi", "OpenTelemetrySdk"]),
.target(name: "ResourceExtension",
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Instrumentation/SDKResourceExtension",
Expand Down Expand Up @@ -136,11 +138,12 @@ let package = Package(
],
path: "Tests/InstrumentationTests/NetworkStatusTests"),
.testTarget(name: "OpenTelemetryApiTests",
dependencies: ["OpenTelemetryApi"],
dependencies: ["OpenTelemetryApi", "OpenTelemetryTestUtils"],
path: "Tests/OpenTelemetryApiTests"),
.testTarget(name: "OpenTelemetrySdkTests",
dependencies: ["OpenTelemetryApi",
"OpenTelemetrySdk"],
"OpenTelemetrySdk",
"OpenTelemetryTestUtils"],
path: "Tests/OpenTelemetrySdkTests"),
.testTarget(name: "ResourceExtensionTests",
dependencies: ["ResourceExtension", "OpenTelemetrySdk"],
Expand Down
Loading

0 comments on commit 408af2d

Please sign in to comment.