From bddb17155bf3cd8cca2c121b332f0b539ebe28ff Mon Sep 17 00:00:00 2001 From: Hussain Kara Fallah Date: Fri, 8 Nov 2024 14:20:23 +0200 Subject: [PATCH 1/5] hey --- build.libraries.gradle | 1 + tw-context/build.gradle | 1 + .../common/context/TwContext.java | 8 +++++- .../context/TwContextThreadLocalAccessor.java | 28 +++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tw-context/src/main/java/com/transferwise/common/context/TwContextThreadLocalAccessor.java diff --git a/build.libraries.gradle b/build.libraries.gradle index 8dcc715..e4d087c 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/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..e7aa0d9 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 @@ -12,6 +12,7 @@ import java.util.function.Function; import java.util.function.Supplier; import javax.annotation.Nonnull; +import io.micrometer.context.ContextRegistry; import lombok.Getter; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; @@ -35,9 +36,14 @@ 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); + + static { + ContextRegistry.getInstance().registerThreadLocalAccessor(new TwContextThreadLocalAccessor()); + } + 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..286341d --- /dev/null +++ b/tw-context/src/main/java/com/transferwise/common/context/TwContextThreadLocalAccessor.java @@ -0,0 +1,28 @@ +package com.transferwise.common.context; + +import io.micrometer.context.ThreadLocalAccessor; + +import static com.transferwise.common.context.UnitOfWork.TW_CONTEXT_KEY; + +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(); + } +} From 498dacb26dc8c85f6c93df3fe46bbf2d46cc1479 Mon Sep 17 00:00:00 2001 From: Hussain Kara Fallah Date: Fri, 8 Nov 2024 14:25:38 +0200 Subject: [PATCH 2/5] hey --- CHANGELOG.md | 5 +++++ gradle.properties | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d8f680..f404e1a 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-05-28 + +### Changed +* Added ThreadLocalAccessor for TwContext so it can be available for libraries using micrometer context propagation API + ## [2.0.0] - 2024-05-28 ### Changed diff --git a/gradle.properties b/gradle.properties index 97d695b..1d50c83 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=2.0.0 +version=2.1.0 From 6053fa58bb6f32f372dbad0495e86457b4d22c7e Mon Sep 17 00:00:00 2001 From: Hussain Kara Fallah Date: Fri, 8 Nov 2024 14:33:33 +0200 Subject: [PATCH 3/5] happy-checkstyle --- .../main/java/com/transferwise/common/context/TwContext.java | 2 +- .../common/context/TwContextThreadLocalAccessor.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 e7aa0d9..f2d6786 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 @@ -3,6 +3,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.util.concurrent.RateLimiter; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import io.micrometer.context.ContextRegistry; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -12,7 +13,6 @@ import java.util.function.Function; import java.util.function.Supplier; import javax.annotation.Nonnull; -import io.micrometer.context.ContextRegistry; import lombok.Getter; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; 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 index 286341d..bd101e7 100644 --- a/tw-context/src/main/java/com/transferwise/common/context/TwContextThreadLocalAccessor.java +++ b/tw-context/src/main/java/com/transferwise/common/context/TwContextThreadLocalAccessor.java @@ -1,9 +1,9 @@ package com.transferwise.common.context; -import io.micrometer.context.ThreadLocalAccessor; - import static com.transferwise.common.context.UnitOfWork.TW_CONTEXT_KEY; +import io.micrometer.context.ThreadLocalAccessor; + public class TwContextThreadLocalAccessor implements ThreadLocalAccessor { @Override From 5076dff949315d8cae93e7a917d6c383d6e24985 Mon Sep 17 00:00:00 2001 From: Hussain Kara Fallah Date: Thu, 14 Nov 2024 14:08:33 +0200 Subject: [PATCH 4/5] switch to autoconfig --- tw-context-starter/build.gradle | 1 + .../common/context/TwContextAutoConfiguration.java | 4 ++++ .../main/java/com/transferwise/common/context/TwContext.java | 5 ----- 3 files changed, 5 insertions(+), 5 deletions(-) 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..7d18d4a 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 @@ -12,6 +12,10 @@ @Configuration public class TwContextAutoConfiguration { + TwContextAutoConfiguration() { + ContextRegistry.getInstance().registerThreadLocalAccessor(new TwContextThreadLocalAccessor()); + } + @Bean @ConditionalOnMissingBean public UnitOfWorkManager twContextUnitOfWorkManager(IMeterCache meterCache) { 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 f2d6786..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 @@ -3,7 +3,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.util.concurrent.RateLimiter; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import io.micrometer.context.ContextRegistry; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -40,10 +39,6 @@ public class TwContext { static final TwContext ROOT_CONTEXT = new TwContext(null, true); - static { - ContextRegistry.getInstance().registerThreadLocalAccessor(new TwContextThreadLocalAccessor()); - } - public static TwContext current() { Optional twContext = contextTl.get(); return twContext == null || !twContext.isPresent() ? ROOT_CONTEXT : twContext.get(); From a1e6ee527a18912e7748810625d890c2ed0c68b4 Mon Sep 17 00:00:00 2001 From: Hussain Kara Fallah Date: Thu, 14 Nov 2024 14:26:29 +0200 Subject: [PATCH 5/5] ok --- .../transferwise/common/context/TwContextAutoConfiguration.java | 1 + 1 file changed, 1 insertion(+) 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 7d18d4a..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;