Skip to content

Commit

Permalink
otel context demo
Browse files Browse the repository at this point in the history
  • Loading branch information
liuganghuan committed Nov 6, 2023
1 parent b7fa2ef commit 44d6ca2
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/main/java/com/arloor/forwardproxy/trace/OtelContextDemo.java
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();
}
}
}

0 comments on commit 44d6ca2

Please sign in to comment.