swift-service-context

1.2.1

Minimal type-safe context propagation container
apple/swift-service-context

What's New

1.2.1

2025-06-03T12:38:53Z

What's Changed

SemVer Minor

  • forEach can be Sendable values; storage already is Sendable by @ktoso in #63
  • Fix deprecated message cannot handle in swift 5 by @sidepelican in #51

Other Changes

  • Fix sample code in README by @mtj0928 in #55
  • Fix benchmarks thresholds by @czechboy0 in #56
  • Add static SDK CI workflow by @rnro in #57
  • Enable macOS CI on merge to main and daily timer by @rnro in #58
  • Enable macOS CI on pull requests by @rnro in #59
  • Enable Swift 6.1 jobs in CI by @rnro in #60
  • add thresholds for Swift 6.1 from 6.0 thresholds by @rnro in #61
  • Update README.md by @ebariaux in #62

New Contributors

Full Changelog: 1.2.0...1.2.1

Swift Service Context

Swift 5.7 Swift 5.8 Swift 5.9 Swift 5.10 Swift 6.0

ServiceContext is a minimal (zero-dependency) context propagation container, intended to "carry" items for purposes of cross-cutting tools to be built on top of it.

It is modeled after the concepts explained in W3C Baggage and in the spirit of Tracing Plane 's "Baggage Context" type, although by itself it does not define a specific serialization format.

See https://github.com/apple/swift-distributed-tracing for actual instrument types and implementations which can be used to deploy various cross-cutting instruments all reusing the same baggage type. More information can be found in the SSWG meeting notes.

Overview

ServiceContext serves as currency type for carrying around additional contextual information between Swift tasks and functions.

One generally starts from a "top level" (empty) or the "current" (ServiceContext.current) context and then adds values to it.

The context is a value type and is propagated using task-local values so it can be safely used from concurrent contexts like this:

var context = ServiceContext.topLevel
context[FirstTestKey.self] = 42

func exampleFunction() async -> Int {
    guard let context = ServiceContext.current else {
        return 0
    }
    guard let value = context[FirstTestKey.self] else {
        return 0
    }
    print("test = \(value)") // test = 42
    return value
}

let c = await ServiceContext.withValue(context) {
    await exampleFunction()
}
assert(c == 42)

ServiceContext is a fundamental building block for how distributed tracing propagates trace identifiers.

Dependency

In order to depend on this library you can use the Swift Package Manager, and add the following dependency to your Package.swift:

dependencies: [
  .package(
    url: "https://github.com/apple/swift-service-context.git",
    from: "1.0.0"
  )
]

and depend on the module in your target:

targets: [
    .target(
        name: "MyAwesomeApp",
        dependencies: [
            .product(
              name: "ServiceContextModule",
              package: "swift-service-context"
            ),
        ]
    ),
    // ...
]

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Jun 15 2025 07:45:17 GMT-0900 (Hawaii-Aleutian Daylight Time)