Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Conversation

@songy23
Copy link
Contributor

@songy23 songy23 commented Apr 25, 2019

@songy23 songy23 added this to the Release 0.21.0 milestone Apr 25, 2019
@songy23 songy23 requested a review from bogdandrutu April 25, 2019 19:48
@songy23 songy23 requested review from a team, dinooliva and rghetia as code owners April 25, 2019 19:48
@Deprecated
public static final Context.Key</*@Nullable*/ Span> CONTEXT_SPAN_KEY =
Context.key("opencensus-trace-span-key");
Context.<Span>key("opencensus-trace-span-key");
Copy link
Contributor Author

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.

Copy link
Contributor

@bogdandrutu bogdandrutu Apr 25, 2019

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

Copy link
Contributor Author

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

Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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) {
Copy link
Contributor

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

Copy link
Contributor

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"));
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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"));
Copy link
Contributor

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.

Copy link
Contributor Author

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"));
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor Author

@songy23 songy23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted 6acf9d9 in favor of .799a838

* @return a new context with the given value set.
* @since 0.21
*/
public static Context withValue(Context context, TagContext tagContext) {
Copy link
Contributor Author

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"));
Copy link
Contributor Author

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);
Copy link
Contributor Author

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"));
Copy link
Contributor Author

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"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@songy23 songy23 merged commit 916adcb into census-instrumentation:master Apr 26, 2019
@songy23 songy23 deleted the context-key branch April 26, 2019 00:20
zhangkun83 pushed a commit to grpc/grpc-java that referenced this pull request May 8, 2019
Also updated CensusModule to use the new helper methods ContextUtils.withValue() instead of directly manipulating the context keys. See census-instrumentation/opencensus-java#1864.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't expose gRPC context key directly. Use APIs instead.

4 participants