{"id":15036356,"url":"https://github.com/apple/swift-service-context","last_synced_at":"2025-12-11T23:01:40.506Z","repository":{"id":37476900,"uuid":"298378310","full_name":"apple/swift-service-context","owner":"apple","description":"Minimal type-safe context propagation container","archived":false,"fork":false,"pushed_at":"2025-03-13T10:35:48.000Z","size":170,"stargazers_count":84,"open_issues_count":2,"forks_count":22,"subscribers_count":29,"default_branch":"main","last_synced_at":"2025-03-29T03:09:55.014Z","etag":null,"topics":["async-context","baggage","baggage-context","concurrency","context-propagation","distributed-systems","sswg","trace-context"],"latest_commit_sha":null,"homepage":"https://github.com/apple/swift-distributed-tracing","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apple.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-24T19:39:15.000Z","updated_at":"2025-03-18T19:51:46.000Z","dependencies_parsed_at":"2024-11-19T09:19:28.142Z","dependency_job_id":"63511b44-13a3-4f60-9241-3af9b43b448a","html_url":"https://github.com/apple/swift-service-context","commit_stats":{"total_commits":73,"total_committers":12,"mean_commits":6.083333333333333,"dds":0.452054794520548,"last_synced_commit":"94baacc7acea8a60c082b1afe93974f0b45940a2"},"previous_names":["apple/swift-distributed-tracing-baggage"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-service-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-service-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-service-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-service-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apple","download_url":"https://codeload.github.com/apple/swift-service-context/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284951,"owners_count":20913704,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-context","baggage","baggage-context","concurrency","context-propagation","distributed-systems","sswg","trace-context"],"created_at":"2024-09-24T20:30:54.874Z","updated_at":"2025-12-11T23:01:40.500Z","avatar_url":"https://github.com/apple.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Swift Service Context\n\n[![Swift 6.0](https://img.shields.io/badge/Swift-6.0-ED523F.svg?style=flat)](https://swift.org/download/)\n[![Swift 6.1](https://img.shields.io/badge/Swift-6.0-ED523F.svg?style=flat)](https://swift.org/download/) \n[![Swift 6.2](https://img.shields.io/badge/Swift-6.0-ED523F.svg?style=flat)](https://swift.org/download/)\n\n`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.\n\nIt is modeled after the concepts explained in [W3C Baggage](https://w3c.github.io/baggage/) and \nin the spirit of [Tracing Plane](https://cs.brown.edu/~jcmace/papers/mace18universal.pdf) 's \"Baggage Context\" type,\nalthough by itself it does not define a specific serialization format.\n\nSee https://github.com/apple/swift-distributed-tracing for actual instrument types and implementations which can be used to\ndeploy various cross-cutting instruments all reusing the same baggage type. More information can be found in the\n[SSWG meeting notes](https://gist.github.com/ktoso/4d160232407e4d5835b5ba700c73de37#swift-baggage-context--distributed-tracing).\n\n## Overview\n\n`ServiceContext` serves as currency type for carrying around additional contextual information between Swift tasks and functions.\n\nOne generally starts from a \"top level\" (empty) or the \"current\" (`ServiceContext.current`) context and then adds values to it.\n\nThe context is a value type and is propagated using task-local values so it can be safely used from concurrent contexts like this:\n\n```swift\nvar context = ServiceContext.topLevel\ncontext[FirstTestKey.self] = 42\n\nfunc exampleFunction() async -\u003e Int {\n    guard let context = ServiceContext.current else {\n        return 0\n    }\n    guard let value = context[FirstTestKey.self] else {\n        return 0\n    }\n    print(\"test = \\(value)\") // test = 42\n    return value\n}\n\nlet c = await ServiceContext.withValue(context) {\n    await exampleFunction()\n}\nassert(c == 42)\n```\n\n`ServiceContext` is a fundamental building block for how distributed tracing propagates trace identifiers.\n\n## Dependency\n\nIn order to depend on this library you can use the Swift Package Manager, and add the following dependency to your `Package.swift`:\n\n```swift\ndependencies: [\n  .package(\n    url: \"https://github.com/apple/swift-service-context.git\",\n    from: \"1.0.0\"\n  )\n]\n```\n\nand depend on the module in your target:\n\n```swift\ntargets: [\n    .target(\n        name: \"MyAwesomeApp\",\n        dependencies: [\n            .product(\n              name: \"ServiceContextModule\",\n              package: \"swift-service-context\"\n            ),\n        ]\n    ),\n    // ...\n]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapple%2Fswift-service-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapple%2Fswift-service-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapple%2Fswift-service-context/lists"}