-
Notifications
You must be signed in to change notification settings - Fork 195
Restrict access to context key, add helper methods to interact with Context. #1864
Conversation
| @Deprecated | ||
| public static final Context.Key</*@Nullable*/ Span> CONTEXT_SPAN_KEY = | ||
| Context.key("opencensus-trace-span-key"); | ||
| Context.<Span>key("opencensus-trace-span-key"); |
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 value for context span key is still null for backwards-compatibility.
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.
Who needs it? gRPC does not need to be null. We don't change the key but the result of unsafe getValue can apply default logic
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.
gRPC expected this behavior in their tests, e.g https://github.com/grpc/grpc-java/blob/master/core/src/test/java/io/grpc/internal/CensusModulesTest.java#L306. I'm not sure if this is used anywhere else. /cc @zhangkun83
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.
But we don't change the behavior for the key, only for the new API which we can implement whatever we want.
| * @return a new context with the given value set. | ||
| * @since 0.21 | ||
| */ | ||
| public static Context withValue(Context context, TagContext tagContext) { |
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.
Shouldn't you check whether tagContext is null? Or annotate as nullable?
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.
No, because the key is initialized with a default value.
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.
Reverted, though I'm wondering when we would want to set TagContext or Span to null in a 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.
Agreed that the default value does mean the tagContext can be null but still not clear to me that there is a good use-case for this.
If there is, shouldn't it be annotated Nullable?
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.
I added the Nullable annotation to the context value. The new API still returns non-null value though.
| * @return the value from the specified {@code Context}. | ||
| * @since 0.21 | ||
| */ | ||
| public static TagContext getValue(Context 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.
Maybe these names should be less generic since they are doing specific work - e.g. withTagContext and getTagContext
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.
I would keep them as they are to not make confusion with the main APIs.
| * @since 0.21 | ||
| */ | ||
| public static Context withValue(Context context, TagContext tagContext) { | ||
| return Utils.checkNotNull(context, "context").withValue(TAG_CONTEXT_KEY, Utils.checkNotNull(tagContext, "tagContext")); |
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.
tagContext may be null. no need to check.
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.
Reverted.
| // TODO(songy23): make this private once gRPC migrates to use the alternative APIs. | ||
| @Deprecated | ||
| public static final Context.Key<Span> CONTEXT_SPAN_KEY = | ||
| Context.<Span>keyWithDefault("opencensus-trace-span-key", BlankSpan.INSTANCE); |
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.
we cannot change the default for the key.
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.
Makes sense, fixed.
| public static final Context.Key</*@Nullable*/ Span> CONTEXT_SPAN_KEY = | ||
| Context.key("opencensus-trace-span-key"); | ||
| public static Span getValue(Context context) { | ||
| return CONTEXT_SPAN_KEY.get(Utils.checkNotNull(context, "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.
here we implement the default logic.
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.
Updated.
| * @since 0.21 | ||
| */ | ||
| public static Context withValue(Context context, Span span) { | ||
| return Utils.checkNotNull(context, "context").withValue(CONTEXT_SPAN_KEY, Utils.checkNotNull(span, "span")); |
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.
no need to check for not null for the span, because we can support null Span correct?
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.
Fixed.
songy23
left a comment
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.
| * @return a new context with the given value set. | ||
| * @since 0.21 | ||
| */ | ||
| public static Context withValue(Context context, TagContext tagContext) { |
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.
Reverted, though I'm wondering when we would want to set TagContext or Span to null in a context.
| * @since 0.21 | ||
| */ | ||
| public static Context withValue(Context context, TagContext tagContext) { | ||
| return Utils.checkNotNull(context, "context").withValue(TAG_CONTEXT_KEY, Utils.checkNotNull(tagContext, "tagContext")); |
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.
Reverted.
| // TODO(songy23): make this private once gRPC migrates to use the alternative APIs. | ||
| @Deprecated | ||
| public static final Context.Key<Span> CONTEXT_SPAN_KEY = | ||
| Context.<Span>keyWithDefault("opencensus-trace-span-key", BlankSpan.INSTANCE); |
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.
Makes sense, fixed.
| * @since 0.21 | ||
| */ | ||
| public static Context withValue(Context context, Span span) { | ||
| return Utils.checkNotNull(context, "context").withValue(CONTEXT_SPAN_KEY, Utils.checkNotNull(span, "span")); |
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.
Fixed.
| public static final Context.Key</*@Nullable*/ Span> CONTEXT_SPAN_KEY = | ||
| Context.key("opencensus-trace-span-key"); | ||
| public static Span getValue(Context context) { | ||
| return CONTEXT_SPAN_KEY.get(Utils.checkNotNull(context, "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.
Updated.
Also updated CensusModule to use the new helper methods ContextUtils.withValue() instead of directly manipulating the context keys. See census-instrumentation/opencensus-java#1864.
Fixes #1863.
Similar to open-telemetry/opentelemetry-java#211.
/cc @zhangkun83