Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 9bff871

Browse files
committed
Initial readme update
1 parent aa86152 commit 9bff871

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,74 @@
44

55
Swift Distributed Tracing Extras ships the following extra modules:
66

7-
- [OpenTelemetrySemanticConventions](Sources/OpenTelemetrySemanticConventions)
7+
- [OpenTelemetrySemanticConventions](Sources/TracingOpenTelemetrySemanticConventions)
88

99
### OpenTelemetry Semantic Conventions
1010

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+
1127
The OpenTelemetry Semantic Conventions package provides span attributes to work with [OpenTelemetry's
1228
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

Comments
 (0)