diff --git a/CHANGELOG.md b/CHANGELOG.md index d59f11d..7d45ed1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.1.0] - 2024-11-08 + +### Changed +* Added ThreadLocalAccessor for TwContext so it can be available for libraries using micrometer context propagation API + ## [2.0.1] - 2024-07-16 ### Changed diff --git a/build.libraries.gradle b/build.libraries.gradle index d710663..b0eb360 100644 --- a/build.libraries.gradle +++ b/build.libraries.gradle @@ -7,6 +7,7 @@ ext { guava : 'com.google.guava:guava:33.0.0-jre', springBootDependencies : "org.springframework.boot:spring-boot-dependencies:${springBootVersion}", twBaseUtils : "com.transferwise.common:tw-base-utils:1.12.3", + micrometerContextPropagation : "io.micrometer:context-propagation:1.1.2", // versions managed by spring-boot-dependencies platform assertjCore : 'org.assertj:assertj-core', diff --git a/gradle.properties b/gradle.properties index 332be40..8bd1820 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,2 @@ -version=2.0.1 +version=2.1.0 + diff --git a/tw-context-starter/build.gradle b/tw-context-starter/build.gradle index 5afede4..c28c294 100644 --- a/tw-context-starter/build.gradle +++ b/tw-context-starter/build.gradle @@ -7,6 +7,7 @@ apply from: '../build.common.gradle' dependencies { implementation libraries.springBootAutoconfigure implementation libraries.twBaseUtils + implementation libraries.micrometerContextPropagation api project(":tw-context:") testImplementation libraries.springBootStarterTest diff --git a/tw-context-starter/src/main/java/com/transferwise/common/context/TwContextAutoConfiguration.java b/tw-context-starter/src/main/java/com/transferwise/common/context/TwContextAutoConfiguration.java index 8dd689a..8469ddf 100644 --- a/tw-context-starter/src/main/java/com/transferwise/common/context/TwContextAutoConfiguration.java +++ b/tw-context-starter/src/main/java/com/transferwise/common/context/TwContextAutoConfiguration.java @@ -2,6 +2,7 @@ import com.transferwise.common.baseutils.meters.cache.IMeterCache; import com.transferwise.common.baseutils.meters.cache.MeterCache; +import io.micrometer.context.ContextRegistry; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -12,6 +13,10 @@ @Configuration public class TwContextAutoConfiguration { + TwContextAutoConfiguration() { + ContextRegistry.getInstance().registerThreadLocalAccessor(new TwContextThreadLocalAccessor()); + } + @Bean @ConditionalOnMissingBean public UnitOfWorkManager twContextUnitOfWorkManager(IMeterCache meterCache) { diff --git a/tw-context/build.gradle b/tw-context/build.gradle index b00798c..2a257b7 100644 --- a/tw-context/build.gradle +++ b/tw-context/build.gradle @@ -10,6 +10,7 @@ dependencies { implementation libraries.slf4jApi implementation libraries.guava implementation libraries.twBaseUtils + implementation libraries.micrometerContextPropagation api libraries.micrometerCore } \ No newline at end of file diff --git a/tw-context/src/main/java/com/transferwise/common/context/TwContext.java b/tw-context/src/main/java/com/transferwise/common/context/TwContext.java index 4074e0f..6bd0f60 100644 --- a/tw-context/src/main/java/com/transferwise/common/context/TwContext.java +++ b/tw-context/src/main/java/com/transferwise/common/context/TwContext.java @@ -35,9 +35,10 @@ public class TwContext { private static final ThreadLocal> contextTl = new ThreadLocal<>(); private static final List interceptors = new CopyOnWriteArrayList<>(); private static final List attributePutListeners = new CopyOnWriteArrayList<>(); - private static final TwContext ROOT_CONTEXT = new TwContext(null, true); private static final RateLimiter throwableLoggingRateLimiter = RateLimiter.create(2); + static final TwContext ROOT_CONTEXT = new TwContext(null, true); + public static TwContext current() { Optional twContext = contextTl.get(); return twContext == null || !twContext.isPresent() ? ROOT_CONTEXT : twContext.get(); diff --git a/tw-context/src/main/java/com/transferwise/common/context/TwContextThreadLocalAccessor.java b/tw-context/src/main/java/com/transferwise/common/context/TwContextThreadLocalAccessor.java new file mode 100644 index 0000000..bd101e7 --- /dev/null +++ b/tw-context/src/main/java/com/transferwise/common/context/TwContextThreadLocalAccessor.java @@ -0,0 +1,28 @@ +package com.transferwise.common.context; + +import static com.transferwise.common.context.UnitOfWork.TW_CONTEXT_KEY; + +import io.micrometer.context.ThreadLocalAccessor; + +public class TwContextThreadLocalAccessor implements ThreadLocalAccessor { + + @Override + public Object key() { + return TW_CONTEXT_KEY; + } + + @Override + public TwContext getValue() { + return TwContext.current(); + } + + @Override + public void setValue(TwContext context) { + context.attach(); + } + + @Override + public void reset() { + TwContext.ROOT_CONTEXT.attach(); + } +}