|
4 | 4 |
|
5 | 5 | Swift Distributed Tracing Extras ships the following extra modules:
|
6 | 6 |
|
7 |
| -- [OpenTelemetrySemanticConventions](Sources/OpenTelemetrySemanticConventions) |
| 7 | +- [OpenTelemetrySemanticConventions](Sources/TracingOpenTelemetrySemanticConventions) |
8 | 8 |
|
9 | 9 | ### OpenTelemetry Semantic Conventions
|
10 | 10 |
|
| 11 | +#### Getting Started |
| 12 | + |
| 13 | +This module is complementary to [swift-distributed-tracing](https://github.com/apple/swift-distributed-tracing) so you will want to depend on it (the API package). |
| 14 | + |
| 15 | + |
| 16 | +Then, you can depend on the extras package: |
| 17 | + |
| 18 | +```swift |
| 19 | +.package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "..."), |
| 20 | +.package(url: "https://github.com/apple/swift-distributed-tracing-extras.git", from: "..."), |
| 21 | +``` |
| 22 | + |
| 23 | +> If you are writing an application, rather than a library, you'll also want to depend on a specific tracer implementation. For available implementations refer to the [swift-distributed-tracing README.md](https://github.com/apple/swift-distributed-tracing). |
| 24 | +
|
| 25 | +#### Usage |
| 26 | + |
11 | 27 | The OpenTelemetry Semantic Conventions package provides span attributes to work with [OpenTelemetry's
|
12 | 28 | semantic conventions](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/trace/semantic_conventions).
|
| 29 | + |
| 30 | +Semantic attributes enable users of [swift-distributed-tracing](https://github.com/apple/swift-distributed-tracing) to |
| 31 | +set attributes on spans using a type-safe pre-defined and well known attributes, like this: |
| 32 | + |
| 33 | +```swift |
| 34 | +import Tracing |
| 35 | +import TracingOpenTelemetrySemanticConventions |
| 36 | + |
| 37 | +// Example framework code, which handles an incoming request and starts a span for handling a request: |
| 38 | +func wrapHandleRequest(exampleRequest: ExampleHTTPRequest) async throws -> ExampleHTTPResponse { |
| 39 | + try await Tracer.current.withSpan(named: "test") { span in |
| 40 | + |
| 41 | + // Set semantic HTTP attributes before |
| 42 | + span.attributes.http.method = exampleRequest.method |
| 43 | + span.attributes.http.url = exampleRequest.url |
| 44 | + // ... |
| 45 | + span.attributes.http.request.headers = exampleRequest.headers |
| 46 | + |
| 47 | + // Hand off to user code to handle the request; |
| 48 | + // The current span already has all important semantic attributes set. |
| 49 | + let response = try await actualHandleRequest(exampleRequest: exampleRequest) |
| 50 | + |
| 51 | + // ... set any response attributes ... |
| 52 | + |
| 53 | + return response |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +You can also read attributes that are set on a span using the same property syntax: |
| 59 | + |
| 60 | +```swift |
| 61 | +assert(span.http.method == "GET") |
| 62 | +// etc. |
| 63 | +``` |
| 64 | + |
| 65 | +Without the semantic attributes package, one would be able to set the attributes using a type-unsafe approach, like this: |
| 66 | + |
| 67 | +```swift |
| 68 | +span.attributes["http.method"] = "\(exampleRequest.method)" |
| 69 | +span.attributes["http.url"] = "\(exampleRequest.url)" |
| 70 | +``` |
| 71 | + |
| 72 | +however this approach is error-prone as one can accidentally record quite the right type of value, |
| 73 | +or accidentally use a wrong, unexpected, key that would then not be understood by Otel tracing backends. |
| 74 | + |
| 75 | +## Supported versions |
| 76 | + |
| 77 | +This module supports: **Swift 5.4+**, on all platforms Swift is available. |
0 commit comments