{"id":33179642,"url":"https://github.com/talsma-ict/context-propagation","last_synced_at":"2026-01-11T17:07:16.410Z","repository":{"id":9220089,"uuid":"61292974","full_name":"talsma-ict/context-propagation","owner":"talsma-ict","description":"API propagating context of ThreadLocal values to other threads","archived":false,"fork":false,"pushed_at":"2025-12-22T07:26:09.000Z","size":2647,"stargazers_count":23,"open_issues_count":2,"forks_count":8,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-23T18:53:37.485Z","etag":null,"topics":["concurrency","context-propagation","contextmanager","cross-thread","thread","threadlocal"],"latest_commit_sha":null,"homepage":"","language":"Java","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/talsma-ict.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2016-06-16T12:56:06.000Z","updated_at":"2025-12-22T07:26:11.000Z","dependencies_parsed_at":"2024-11-06T10:42:38.744Z","dependency_job_id":"66e28595-1045-439d-8a82-4052bf7d56b4","html_url":"https://github.com/talsma-ict/context-propagation","commit_stats":null,"previous_names":["talsma-ict/concurrency"],"tags_count":63,"template":false,"template_full_name":null,"purl":"pkg:github/talsma-ict/context-propagation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talsma-ict%2Fcontext-propagation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talsma-ict%2Fcontext-propagation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talsma-ict%2Fcontext-propagation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talsma-ict%2Fcontext-propagation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/talsma-ict","download_url":"https://codeload.github.com/talsma-ict/context-propagation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talsma-ict%2Fcontext-propagation/sbom","scorecard":{"id":867038,"data":{"date":"2025-08-11","repo":{"name":"github.com/talsma-ict/context-propagation","commit":"a3204d77f2e40ca0670bcfe94c03cfc57866a914"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":6.2,"checks":[{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":10,"reason":"30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Code-Review","score":2,"reason":"Found 3/13 approved changesets -- score normalized to 2","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/build.yml:1","Warn: no topLevel permission defined: .github/workflows/deploy.yml:1","Warn: no topLevel permission defined: .github/workflows/release.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/build.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/build.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/build.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/build.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deploy.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/deploy.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deploy.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/deploy.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/release.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:61: update your workflow using https://app.stepsecurity.io/secureworkflow/talsma-ict/context-propagation/release.yml/main?enable=pin","Info:   0 out of   7 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   2 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Signed-Releases","score":8,"reason":"4 out of the last 4 releases have a total of 4 signed artifacts.","details":["Info: signed release artifact: context-propagation-1.1.1.jar.asc: https://github.com/talsma-ict/context-propagation/releases/tag/1.1.1","Info: signed release artifact: context-manager-grpc-2.0.2.jar.asc: https://github.com/talsma-ict/context-propagation/releases/tag/2.0.2","Info: signed release artifact: context-manager-grpc-2.0.1.jar.asc: https://github.com/talsma-ict/context-propagation/releases/tag/2.0.1","Info: signed release artifact: context-manager-grpc-2.0.0.jar.asc: https://github.com/talsma-ict/context-propagation/releases/tag/2.0.0","Warn: release artifact 1.1.1 does not have provenance: https://api.github.com/repos/talsma-ict/context-propagation/releases/237058808","Warn: release artifact 2.0.2 does not have provenance: https://api.github.com/repos/talsma-ict/context-propagation/releases/234845530","Warn: release artifact 2.0.1 does not have provenance: https://api.github.com/repos/talsma-ict/context-propagation/releases/231419176","Warn: release artifact 2.0.0 does not have provenance: https://api.github.com/repos/talsma-ict/context-propagation/releases/231254575"],"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Packaging","score":10,"reason":"packaging workflow detected","details":["Info: Project packages its releases by way of GitHub Actions.: .github/workflows/deploy.yml:10"],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"SAST","score":10,"reason":"SAST tool detected","details":["Info: SAST configuration detected: Sonar","Warn: 12 commits out of 23 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-24T02:55:48.891Z","repository_id":9220089,"created_at":"2025-08-24T02:55:48.891Z","updated_at":"2025-08-24T02:55:48.891Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28314264,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T14:58:17.114Z","status":"ssl_error","status_checked_at":"2026-01-11T14:55:53.580Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["concurrency","context-propagation","contextmanager","cross-thread","thread","threadlocal"],"created_at":"2025-11-16T03:00:36.839Z","updated_at":"2026-01-11T17:07:16.404Z","avatar_url":"https://github.com/talsma-ict.png","language":"Java","funding_links":[],"categories":["并发编程"],"sub_categories":[],"readme":"[![Maven Version][maven-img]][maven]\n[![Javadoc][javadoc-img]][javadoc]\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=talsma-ict_context-propagation\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=talsma-ict_context-propagation)\n[![Coverage Status][coveralls-img]][coveralls]\n\n\u003e [!IMPORTANT]\n\u003e See [what's new in version 2](#new-in-version-2).\n\n# Context propagation library\n\nLibrary to capture a snapshot of one or more `ThreadLocal` values and reactivate them in another thread.\n\nThe library automatically detects supported `ThreadLocal` values to be captured.\nIt defines a `ContextManager` Service Provider Interface (SPI) that can be provided via the Java [ServiceLoader].\n\nThe core module provides several [utility classes](#utility-classes) to safely capture a context snapshot\nin the calling thread and reactivating it in another thread and ensuring proper\ncleanup after its execution finishes. This reduces the chance of 'leaking' thread-local\nvalues.\n\n## API concepts\n\nA brief explanation of the core concepts from the api:\n\n### ContextSnapshot\n\nCaptures active ThreadLocal values from all detected [ContextManager](#contextmanager) implementations.  \nThese values can be _reactivated_ in another thread.\n\n- Calling `ContextSnapshot.capture()` will _capture_ all detected threadlocals to be propagated into a single snapshot.\n- Calling `reactivate()` on this snapshot will _reactivate_ these threadlocal values on the thread it gets called in.  \n  Reactivate returns a _Closeable_ that will undo the reactivation when closed.\n\n**Note:** Reactivated snapshots **must be closed** to avoid leaking context.\n\nAll _context aware_ [utility classes](#utility-classes) in this library are tested\nto make sure they reactivate _and_ close snapshots in a safe way.\nThe `ContextSnapshot.wrap` methods also guarantee closing reactivations.\n\n### ContextManager\n\nManages a [context](#context) by providing a standard way of interacting with `ThreadLocal` values.\n\nManaged thread-local values can be accessed via a ContextManager by:\n\n- Calling `getActiveContextValue()` which _gets_ the current thread-local value.\n- Calling `activate(value)` which _sets_ the given value until `close()` is called on the resulting `Context`.\n- Calling `clear()` which _removes_ the thread-local value.\n\n### Context\n\nAbstraction for an _activated_ thread-local value.\n\nWhen the context manager activates a value, a new `Context` is returned.\n_Closing_ this context will undo this activated value again.\n\n\u003e [!IMPORTANT]\n\u003e It is the responsibility of the one activating a new Context to also close it _from the same thread_.\n\u003e Using every activated context in a 'try-with-resources' block of code is a recommended and safe way to do this.\n\n## Utility classes\n\nThe `context-propagation-core` module contains various utility classes \nthat make it easier to capture context snapshots and reactivate them safely in other threads.\n\nExamples include:\n\n- [`ContextAwareExecutorService`][ContextAwareExecutorService], wrapping any existing `ExecutorService`,\n  automatically capturing and reactivating context snapshots.\n- [`ContextAwareCompletableFuture`][ContextAwareCompletableFuture],\n  propagating context snapshots into each successive `CompletionStage`.\n- Variants of java standard `java.util.function` implementations,\n  executing within a context snapshot.\n- Base class `AbstractThreadLocalContext` that features nesting active values and predictable behaviour for out-of-order closing. \n\n## How to use this library\n\n### Threadpools and ExecutorService\n\nIf your background threads are managed by an ExecutorService,\nyou can use our _context aware_ ExecutorService to wrap your usual threadpool.\n\nThe `ContextAwareExcutorService` automatically captures context snapshots before submitting work. \nThis snapshot is then reactivated (and closed) in the submitted background thread.  \n\nIt can wrap any ExecutorService for the actual thread execution:\n\n```java\nprivate static final ExecutorService THREADPOOL =\n        ContextAwareExecutorService.wrap(Executors.newCachedThreadpool());\n```\n\n### Manually capture and reactivate a context snapshot\n\nJust before creating a new thread, capture a snapshot of all ThreadLocal context\nvalues:\n\n```java\nfinal ContextSnapshot snapshot = ContextSnapshot.capture();\n```\n\nIn the code of your background thread, reactivate the snapshot to have all ThreadLocal\ncontext values set as they were captured:\n\n```java\ntry (ContextSnapshot.Reactivation reactivation = snapshot.reactivate()) {\n    // All ThreadLocal values from the snapshot are available within this block\n}\n\n// or, using a Runnable lambda:\nsnapshot.wrap(() -\u003e {\n    // All ThreadLocal values from the snapshot are available within this block\n}).run();\n```\n\n## Supported contexts\n\nThe following `ThreadLocal`-based contexts are currently supported\nout of the box by this context-propagation library:\n\n- [Spring Security Context]\n- [OpenTelemetry Context][opentelemetry context propagation]\n- [SLF4J MDC (Mapped Diagnostic Context)][slf4j mdc propagation]\n- [gRPC Context](managers/context-manager-grpc)\n- [Log4j 2 Thread Context][log4j2 thread context propagation]\n- [Locale context][locale context]\n- [ServletRequest contexts][servletrequest propagation]\n- .. _Yours?_\n  Feel free to create an issue or pull-request if you know of\n  a ThreadLocal context that should also be included in a context snapshot.\n\n## Custom contexts\n\nAdding your own `Context` type is possible\nby [creating your own context manager](context-propagation-api/README.md#creating-your-own-context-manager).\n\n## Building jars with dependencies\n\nWhen using a build tool or plugin to create an 'uber-jar', i.e. a jar file with all\nthe classes of its dependencies included, you have to make sure that the service\nprovider configuration files under `META-INF/services` are either preserved or\nmerged. Otherwise Java's `ServiceLoader` will not be able to find the context\nimplementations of this library.\n\nIn case you are using the Maven Shade Plugin, you can use the\n[\n`ServicesResourceTransformer`](https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#ServicesResourceTransformer)\nfor this task.\n\n## Performance metrics\n\nNo library is 'free' with regards to performance.\nCapturing a context snapshot and reactivating it in another thread is no different.\nFor insight, the library tracks the overall time used creating and reactivating\ncontext snapshots along with time spent in each individual `ContextManager`.\n\n### Logging performance\n\nOn a development machine, you can get timing for each snapshot by turning on logging\nfor `nl.talsmasoftware.context.api.ContextTimer` at `FINEST` or `TRACE` level\n(depending on your logger of choice).\nPlease **do not** turn this on in production as the logging overhead will most likely\nhave a noticeable impact on your application.\n\n### Metrics reporting\n\nThe context propagation performance will be automatically measured\nwhen you add any of the following modules to your classpath:\n\n- [context-timer-metrics](timers/context-timer-metrics): \n  Uses the [dropwizard metrics](https://metrics.dropwizard.io/) library\n  to instrument Timers for context propagation.\n- [context-timer-micrometer](timers/context-timer-micrometer):\n  Adds [Micrometer] Timers for context propagation.\n- [context-timer-opentelemetry](timers/context-timer-opentelemetry):\n  Creates OpenTelemetry histogram meters for context propagation.\n\n## New in version 2\n\nPurpose of 'v2' of this library has been simplification of both the API\nand the structure of the repository.\n\n- Minimum Java version bumped to 8.\n- Repository module restructuring.\n  - Separate API module containing only the minimum API.\n  - Separate core module for all [utility classes](#utility-classes).\n  - All provided manager implementations moved to `managers` subdirectory.\n  - All context timer implementations moved to `timers` subdirectory.\n- API simplification. \n  - Static `ContextSnapshot.capture()` captures a new snapshot and `ContextSnapshot.reactivate()` reactivates it.\n  - `ContextManager.initializeNewContext(value)` was renamed to `activate(value)`.\n  - `ContextManager.getActiveContext()` was replaced by `getActiveContextValue()`.\n  - `ContextManager.clear()` must be implemented, but is allowed to be a 'no-op' empty implementation. The `Clearable` interface was removed.\n- New `ContextManager` implementations for:\n  - [OpenTelemetry](managers/context-manager-opentelemetry) context\n  - The [gRPC](managers/context-manager-grpc) framework context\n- New `ContextTimer` implementation using:\n  - [OpenTelemetry](timers/context-timer-opentelemetry) histogram meter.\n- All `@Deprecated(forRemoval=true)` items from v1 were removed.\n\n## License\n\n[Apache 2.0 license](LICENSE)\n\n\n  [maven-img]: https://img.shields.io/maven-central/v/nl.talsmasoftware.context/context-propagation\n  [maven]: https://search.maven.org/search?q=g:nl.talsmasoftware.context\n  [release-img]: https://img.shields.io/github/release/talsma-ict/context-propagation.svg\n  [release]: https://github.com/talsma-ict/context-propagation/releases\n  [coveralls-img]: https://coveralls.io/repos/github/talsma-ict/context-propagation/badge.svg\n  [coveralls]: https://coveralls.io/github/talsma-ict/context-propagation\n  [javadoc-img]: https://www.javadoc.io/badge/nl.talsmasoftware.context/context-propagation.svg\n  [javadoc]: https://www.javadoc.io/doc/nl.talsmasoftware.context/context-propagation\n\n  [locale context]: managers/context-manager-locale\n  [log4j2 thread context propagation]: managers/context-manager-log4j2\n  [opentelemetry context propagation]: managers/context-manager-opentelemetry\n  [serviceloader]: https://docs.oracle.com/javase/8/docs/api/index.html?java/util/ServiceLoader.html\n  [servletrequest propagation]: managers/context-manager-servletrequest\n  [slf4j mdc propagation]: managers/context-manager-slf4j\n  [spring security context]: managers/context-manager-spring-security\n  [context propagation metrics]: timers/context-timer-metrics\n  [context propagation micrometer]: timers/context-timer-micrometer\n  [micrometer]: https://micrometer.io\n  [ContextAwareExecutorService]: https://javadoc.io/doc/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/core/concurrent/ContextAwareExecutorService.html\n  [ContextAwareCompletableFuture]: context-propagation-core/README.md#contextawarecompletablefuture\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftalsma-ict%2Fcontext-propagation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftalsma-ict%2Fcontext-propagation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftalsma-ict%2Fcontext-propagation/lists"}