-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support micrometer context-propagation : Skeleton code
- Loading branch information
1 parent
db3973d
commit faffdd6
Showing
10 changed files
with
908 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
dependencies { | ||
api libs.reactor.core | ||
implementation 'io.micrometer:context-propagation:1.1.1' | ||
} |
55 changes: 55 additions & 0 deletions
55
reactor3/src/main/java/com/linecorp/armeria/common/reactor3/RequestContextAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.linecorp.armeria.common.reactor3; | ||
|
||
import java.util.Objects; | ||
|
||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.internal.common.RequestContextUtil; | ||
|
||
import io.micrometer.context.ThreadLocalAccessor; | ||
|
||
public class RequestContextAccessor implements ThreadLocalAccessor<RequestContext> { | ||
|
||
private static final String KEY = "ARMERIA_REQUEST_CONTEXT"; | ||
private static final RequestContextAccessor instance = createInstance(); | ||
|
||
private static RequestContextAccessor createInstance() { | ||
return new RequestContextAccessor(); | ||
} | ||
|
||
public static RequestContextAccessor getInstance() { | ||
return instance; | ||
} | ||
|
||
private RequestContextAccessor() { | ||
} | ||
|
||
@Override | ||
public Object key() { | ||
return KEY; | ||
} | ||
|
||
@Override | ||
public RequestContext getValue() { | ||
return RequestContextUtil.get(); | ||
} | ||
|
||
@Override | ||
public void setValue(RequestContext value) { | ||
RequestContextUtil.getAndSet(value); | ||
} | ||
|
||
@Override | ||
public void setValue() { | ||
// NO Operation. | ||
} | ||
|
||
@Override | ||
public void restore(RequestContext previousValue) { | ||
RequestContextUtil.getAndSet(previousValue); | ||
} | ||
|
||
@Override | ||
public void restore() { | ||
RequestContextUtil.pop(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...or3/src/main/java/com/linecorp/armeria/common/reactor3/RequestContextPropagationHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.linecorp.armeria.common.reactor3; | ||
|
||
|
||
import io.micrometer.context.ContextRegistry; | ||
import reactor.core.publisher.Hooks; | ||
|
||
public class RequestContextPropagationHook { | ||
|
||
private static boolean enabled; | ||
|
||
public static synchronized void enable() { | ||
if (enabled) { | ||
return; | ||
} | ||
ContextRegistry.getInstance().registerThreadLocalAccessor(RequestContextAccessor.getInstance()); | ||
Hooks.enableAutomaticContextPropagation(); | ||
|
||
enabled = true; | ||
} | ||
|
||
public static boolean isEnable() { | ||
return enabled; | ||
} | ||
|
||
public static synchronized void disable() { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
Hooks.disableAutomaticContextPropagation(); | ||
enabled = false; | ||
} | ||
} |
Oops, something went wrong.