Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ThreadLocalAccessor for TwContext #49

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions build.libraries.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
version=2.0.1
version=2.1.0

1 change: 1 addition & 0 deletions tw-context/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
implementation libraries.slf4jApi
implementation libraries.guava
implementation libraries.twBaseUtils
implementation libraries.micrometerContextPropagation

api libraries.micrometerCore
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,9 +36,14 @@ public class TwContext {
private static final ThreadLocal<Optional<TwContext>> contextTl = new ThreadLocal<>();
private static final List<TwContextExecutionInterceptor> interceptors = new CopyOnWriteArrayList<>();
private static final List<TwContextAttributePutListener> 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably might be better to put this logic into the starter package and using an autoconfiguration mechanism for that. Using the static block is probably too hard coded in this case.

ContextRegistry.getInstance().registerThreadLocalAccessor(new TwContextThreadLocalAccessor());
}

public static TwContext current() {
Optional<TwContext> twContext = contextTl.get();
return twContext == null || !twContext.isPresent() ? ROOT_CONTEXT : twContext.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TwContext> {

@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();
}
}
Loading