https://github.com/swift-otel/swift-otel
An OpenTelemetry Protocol (OTLP) backend for Swift Log, Swift Metrics, and Swift Distributed Tracing.
https://github.com/swift-otel/swift-otel
distributed-tracing logging metrics observability opentelemetry otel swift swift-distributed-tracing swift-log swift-metrics swift-otel tracing
Last synced: about 1 month ago
JSON representation
An OpenTelemetry Protocol (OTLP) backend for Swift Log, Swift Metrics, and Swift Distributed Tracing.
- Host: GitHub
- URL: https://github.com/swift-otel/swift-otel
- Owner: swift-otel
- License: apache-2.0
- Created: 2020-12-09T14:50:57.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2026-04-29T07:21:41.000Z (about 1 month ago)
- Last Synced: 2026-04-29T09:25:18.817Z (about 1 month ago)
- Topics: distributed-tracing, logging, metrics, observability, opentelemetry, otel, swift, swift-distributed-tracing, swift-log, swift-metrics, swift-otel, tracing
- Language: Swift
- Homepage: https://swiftpackageindex.com/swift-otel/swift-otel
- Size: 9.66 MB
- Stars: 199
- Watchers: 11
- Forks: 46
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
- Notice: NOTICE.txt
Awesome Lists containing this project
README
# Swift OTel
An [OpenTelemetry Protocol (OTLP)][otlp] backend for Swift Log, Swift Metrics, and Swift Distributed Tracing.
> Note: This package does not provide an OTel instrumentation API, or general-purpose OTel SDK.
- 📚 **Documentation** is available on the [Swift Package Index][docs]
- 💻 **Examples** are available in the [Examples][examples] directory
- 🪪 **License** is Apache 2.0, repeated in [LICENSE][license]
- 🔀 **Related Repositories**:
- [`swift-log`][swift-log] Logging API package for Swift.
- [`swift-metrics`][swift-metrics] Metrics API package for Swift.
- [`swift-distributed-tracing`][swift-distributed-tracing] Tracing API package for Swift.
- [`opentelemetry-swift`][opentelemetry-swift] OpenTelemetry API and SDK package.
## Quickstart
Add the dependencies to your package and target:
```swift
// swift-tools-version: 6.1
import PackageDescription
let package = Package(
name: "Application",
platforms: [.macOS("13.0")],
dependencies: [
// ...
.package(url: "https://github.com/swift-otel/swift-otel.git", from: "1.0.0"),
],
targets: [
.executableTarget(
name: "Server",
dependencies: [
// ...
.product(name: "OTel", package: "swift-otel"),
]
)
]
)
```
Then in your target:
```swift
import OTel
// Bootstrap observability backends.
let observability = try OTel.bootstrap()
// Run the observability background tasks, alongside your application logic.
await withThrowingTaskGroup { group in
group.addTask { try await observability.run() }
// Your application logic here...
}
```
### Swift Service Lifecycle integration
The value returned from the bootstrap API conforms to `Service` so it can be run within a `ServiceGroup`:
```swift
import OTel
import ServiceLifecycle
// Bootstrap observability backends.
let observability = try OTel.bootstrap()
// Run observability services in a service group with your services.
let service: Service = // ...
let serviceGroup = ServiceGroup(services: [observability, service], logger: .init(label: "ServiceGroup"))
try await serviceGroup.run()
```
Or, if another dependency has APIs for running additional services, you can use those. For example, using Hummingbird:
```swift
import OTel
import Hummingbird
// Bootstrap observability backends.
let observability = try OTel.bootstrap()
// Create an HTTP server with instrumentation middlewares.
let router = Router()
router.middlewares.add(TracingMiddleware())
router.middlewares.add(MetricsMiddleware())
router.middlewares.add(LogRequestsMiddleware(.info))
router.get("hello") { _, _ in "hello" }
var app = Application(router: router)
// Add the observability service to the Hummingbird service group and run the server.
app.addServices(observability)
try await app.runService()
```
> [!Tip]
> This, and other examples, can be be found in the [Examples][examples] directory.
[otlp]: https://opentelemetry.io/docs/specs/otel/protocol
[docs]: https://swiftpackageindex.com/swift-otel/swift-otel/documentation
[examples]: https://github.com/swift-otel/swift-otel/tree/main/Examples/
[license]: https://github.com/swift-otel/swift-otel/tree/main/LICENSE.txt
[swift-log]: https://github.com/apple/swift-log
[swift-metrics]: https://github.com/apple/swift-metrics
[swift-distributed-tracing]: https://github.com/apple/swift-distributed-tracing
[opentelemetry-swift]: https://github.com/open-telemetry/opentelemetry-swift