-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liuganghuan
committed
Nov 6, 2023
1 parent
b7fa2ef
commit 44d6ca2
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/com/arloor/forwardproxy/trace/OtelContextDemo.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,81 @@ | ||
package com.arloor.forwardproxy.trace; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.context.ImplicitContextKeyed; | ||
import io.opentelemetry.context.Scope; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class OtelContextDemo { | ||
|
||
/** | ||
* @see io.opentelemetry.api.trace.SpanContextKey | ||
* @see io.opentelemetry.api.baggage.BaggageContextKey | ||
*/ | ||
public static class XrayContextKey { | ||
static final ContextKey<XrayContext> KEY = ContextKey.named("xray-context-key"); | ||
|
||
private XrayContextKey() { | ||
} | ||
} | ||
|
||
/** | ||
* implements ImplicitContextKeyed | ||
* @see Baggage#storeInContext(Context) | ||
*/ | ||
public static class XrayContext implements ImplicitContextKeyed { | ||
private String payload; | ||
|
||
public String getPayload() { | ||
return payload; | ||
} | ||
|
||
public void setPayload(String payload) { | ||
this.payload = payload; | ||
} | ||
|
||
public XrayContext(String payload) { | ||
this.payload = payload; | ||
} | ||
|
||
@Override | ||
public Context storeInContext(Context context) { | ||
return context.with(XrayContextKey.KEY, this); | ||
} | ||
} | ||
|
||
private static final ExecutorService poolWrapped = Context.taskWrapping(Executors.newCachedThreadPool()); // OpenTelemetry增强的线程池 | ||
private static final ExecutorService poolUnwrapped = Executors.newCachedThreadPool(); | ||
|
||
public static void main(String[] args) { | ||
Context root = Context.current().with(new XrayContext("some value")); // 设置Context | ||
XrayContext contextOutSideOfScope = Context.current().get(XrayContextKey.KEY); | ||
System.out.println("outside context is " + Optional.ofNullable(contextOutSideOfScope).map(XrayContext::getPayload).orElse(null)); | ||
try (Scope scope = root.makeCurrent()) { // 放置到threadlocal | ||
XrayContext contextInScope = Context.current().get(XrayContextKey.KEY); | ||
System.out.println("inner context is " + Optional.ofNullable(contextInScope).map(XrayContext::getPayload).orElse(null)); | ||
poolWrapped.execute(() -> { | ||
XrayContext xrayContext = Context.current().get(XrayContextKey.KEY); | ||
System.out.println("pool wrapped context is " + Optional.ofNullable(xrayContext).map(XrayContext::getPayload).orElse(null)); | ||
}); | ||
poolUnwrapped.execute(() -> { | ||
XrayContext xrayContext = Context.current().get(XrayContextKey.KEY); | ||
System.out.println("pool unwrapped context is " + Optional.ofNullable(xrayContext).map(XrayContext::getPayload).orElse(null)); | ||
}); | ||
} | ||
try { | ||
poolUnwrapped.shutdown(); | ||
poolWrapped.shutdown(); | ||
poolUnwrapped.awaitTermination(1, TimeUnit.SECONDS); | ||
poolWrapped.awaitTermination(1, TimeUnit.SECONDS); | ||
} catch (Throwable e) { | ||
poolUnwrapped.shutdownNow(); | ||
poolWrapped.shutdownNow(); | ||
} | ||
} | ||
} |