-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for custom context storage #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.opencensus.trace; | ||
|
|
||
| public interface ContextManager { | ||
| Ctx currentContext(); | ||
| Ctx withValue(Ctx ctx, @javax.annotation.Nullable Span span); | ||
| Span getValue(Ctx ctx); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package io.opencensus.trace; | ||
|
|
||
| public interface Ctx { | ||
| Ctx attach(); | ||
| void detach(Ctx ctx); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package io.opencensus.trace.unsafe; | ||
|
|
||
| import io.grpc.Context; | ||
| import io.opencensus.trace.ContextManager; | ||
| import io.opencensus.trace.Ctx; | ||
| import io.opencensus.trace.Span; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Default {@code ContextManager} implementation using {@see io.grpc.Context} | ||
| */ | ||
| public class ContextManagerImpl implements ContextManager { | ||
|
|
||
| @Override | ||
| public Ctx currentContext() { | ||
| return wrapContext(Context.current()); | ||
| } | ||
|
|
||
| @Override | ||
| public Ctx withValue(Ctx ctx, @Nullable Span span) { | ||
| return wrapContext(ContextUtils.withValue(unwrapContext(ctx), span)); | ||
| } | ||
|
|
||
| @Override | ||
| public Span getValue(Ctx ctx) { | ||
| return ContextUtils.getValue(unwrapContext(ctx)); | ||
| } | ||
|
|
||
| private static Ctx wrapContext(Context context) { | ||
| return new CtxImpl(context); | ||
| } | ||
|
|
||
| private static Context unwrapContext(Ctx ctx) { | ||
| return ((CtxImpl)ctx).getContext(); | ||
| } | ||
|
|
||
| protected ContextManagerImpl() { | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ | |
| * | ||
| * @since 0.5 | ||
| */ | ||
| public final class ContextUtils { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hiding |
||
| final class ContextUtils { | ||
| // No instance of this class. | ||
| private ContextUtils() {} | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package io.opencensus.trace.unsafe; | ||
|
|
||
| import io.grpc.Context; | ||
| import io.opencensus.trace.Ctx; | ||
|
|
||
| /** | ||
| * {@code Ctx} implementation using {@see io.grpc.Context} | ||
| */ | ||
| class CtxImpl implements Ctx { | ||
| private final Context context; | ||
|
|
||
| public CtxImpl(Context context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| Context getContext() { | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public Ctx attach() { | ||
| return new CtxImpl(context.attach()); | ||
| } | ||
|
|
||
| @Override | ||
| public void detach(Ctx ctx) { | ||
| CtxImpl impl = (CtxImpl) ctx; | ||
| context.detach(impl.context); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package io.opencensus.trace.unsafe; | ||
|
|
||
| import io.opencensus.trace.ContextManager; | ||
| import io.opencensus.trace.Ctx; | ||
| import io.opencensus.trace.Span; | ||
|
|
||
| public class CtxUtils { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally wanted to keep I've introduced the |
||
| // No instance of this class. | ||
| private CtxUtils() {} | ||
|
|
||
| private static final ContextManager DEFAULT_CONTEXT_MANAGER = new ContextManagerImpl(); | ||
| private static ContextManager contextManager = DEFAULT_CONTEXT_MANAGER; | ||
|
|
||
| /** | ||
| * Overrides context manager with a custom implementation | ||
| * @param cm custom {@code ContextManager} to be used instead of a default one. | ||
| */ | ||
| public static void setContextManager(ContextManager cm) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the key change in the OpenCensus Java code, allowing us to set a custom implementation, which can be hosted in the The implementation may be customized to make it thread safe later, keeping it simple for now. |
||
| contextManager = cm; | ||
| } | ||
|
|
||
| public static Ctx currentContext() { | ||
| return contextManager.currentContext(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@code Ctx} with the given value set. | ||
| * | ||
| * @param context the parent {@code Ctx}. | ||
| * @param span the value to be set. | ||
| * @return a new context with the given value set. | ||
| */ | ||
| public static Ctx withValue(Ctx context, @javax.annotation.Nullable Span span) { | ||
| return contextManager.withValue(context, span); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value from the specified {@code Ctx}. | ||
| * | ||
| * @param context the specified {@code Ctx}. | ||
| * @return the value from the specified {@code Ctx}. | ||
| */ | ||
| public static Span getValue(Ctx context) { | ||
| return contextManager.getValue(context); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default implementation simply redirects all calls to
ContextUtils(original implementation).