File tree Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,37 @@ See https://github.com/apple/swift-distributed-tracing for actual instrument typ
1818deploy various cross-cutting instruments all reusing the same baggage type. More information can be found in the
1919[ SSWG meeting notes] ( https://gist.github.com/ktoso/4d160232407e4d5835b5ba700c73de37#swift-baggage-context--distributed-tracing ) .
2020
21+ ## Overview
22+
23+ ` ServiceContext ` serves as currency type for carrying around additional contextual information between Swift tasks and functions.
24+
25+ One generally starts from a "top level" (empty) or the "current" (` ServiceContext.current ` ) context and then adds values to it.
26+
27+ The context is a value type and is propagated using task-local values so it can be safely used from concurrent contexts like this:
28+
29+ ``` swift
30+ var context = ServiceContext.topLevel
31+ context[FirstTestKey.self ] = 42
32+
33+ func exampleFunction () async -> Int {
34+ guard let context = ServiceContext.current {
35+ return 0
36+ }
37+ guard let value = context[FirstTestKey.self ] {
38+ return 0
39+ }
40+ print (" test = \( value ) " ) // test = 42
41+ return value
42+ }
43+
44+ let c = ServiceContext.withValue (context) {
45+ await exampleFunction ()
46+ }
47+ assert (c == 42 )
48+ ```
49+
50+ ` ServiceContext ` is a fundamental building block for how distributed tracing propagages trace identifiers.
51+
2152## Dependency
2253
2354In order to depend on this library you can use the Swift Package Manager, and add the following dependency to your ` Package.swift ` :
@@ -26,7 +57,7 @@ In order to depend on this library you can use the Swift Package Manager, and ad
2657dependencies: [
2758 .package (
2859 url : " https://github.com/apple/swift-service-context.git" ,
29- from : " 0.2 .0"
60+ from : " 1.0 .0"
3061 )
3162]
3263```
You can’t perform that action at this time.
0 commit comments