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

Commit 6a8b708

Browse files
authored
Update readme a little bit (#17)
1 parent 5ff1366 commit 6a8b708

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,73 @@
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 InstrumentationSystem.tracer.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+
46+
// Hand off to user code to handle the request;
47+
// The current span already has all important semantic attributes set.
48+
let response = try await actualHandleRequest(exampleRequest: exampleRequest)
49+
50+
// ... set any response attributes ...
51+
52+
return response
53+
}
54+
}
55+
```
56+
57+
You can also read attributes that are set on a span using the same property syntax:
58+
59+
```swift
60+
assert(span.attributes.http.method == "GET")
61+
// etc.
62+
```
63+
64+
Without the semantic attributes package, one would be able to set the attributes using a type-unsafe approach, like this:
65+
66+
```swift
67+
span.attributes["http.method"] = "\(exampleRequest.method)"
68+
span.attributes["http.url"] = "\(exampleRequest.url)"
69+
```
70+
71+
however this approach is error-prone as one can accidentally record not quite the right type of value,
72+
or accidentally use a wrong, unexpected, key that would then not be understood by Otel tracing backends.
73+
74+
## Supported versions
75+
76+
This module supports: **Swift 5.4+**, on all platforms Swift is available.

0 commit comments

Comments
 (0)