Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Implement B3 propagation#265

Merged
reyang merged 7 commits intocensus-instrumentation:masterfrom
jpoehnelt:feat/b3-propagation
Feb 7, 2019
Merged

Implement B3 propagation#265
reyang merged 7 commits intocensus-instrumentation:masterfrom
jpoehnelt:feat/b3-propagation

Conversation

@kornholi
Copy link
Copy Markdown
Contributor

Supersedes #168.

This works for our usecase (in-app spans for requests in an Istio mesh, exported directly to Stackdriver), but it's not correct since ParentSpanId is not propagated correctly.

See https://github.com/openzipkin/b3-propagation/blob/682d1fc9caa31fa25678674a3a7a1ecd4e9f7813/README.md#why-is-parentspanid-propagated

Should to_headers take a parent SpanContext, or should the parent be included in SpanContext?

@kornholi
Copy link
Copy Markdown
Contributor Author

@songy23 @liyanhui1228 ptal when you have a chance

@@ -0,0 +1,89 @@
# Copyright 2017, OpenCensus Authors
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: s/2017/2018

@@ -0,0 +1,104 @@
# Copyright 2017, OpenCensus Authors
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

from opencensus.trace.span_context import SpanContext, INVALID_SPAN_ID
from opencensus.trace.trace_options import TraceOptions

_TRACE_ID_KEY = 'x-b3-traceid'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A naive question: does the case matter here? I saw the Zipkin specs used "X-B3-TraceId", "X-B3-SpanId", "X-B3-ParentSpanId" and "X-B3-Sampled" here.

/cc @adriancole @bogdandrutu

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Header names are case-insensitive (and forced lower case in HTTP/2)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

most send X-B3 in the case format you mention even in case sensitive contexts. the new "b3" header should always be lowercase.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe it'd be worth adding support for the new b3 single header in this PR too?

_SPAN_ID_KEY = 'x-b3-spanid'
_PARENT_SPAN_ID_KEY = 'x-b3-parentspanid'
_SAMPLED_KEY = 'x-b3-sampled'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there's also a "X-B3-Flags" header.

@liyanhui1228
Copy link
Copy Markdown
Contributor

liyanhui1228 commented Aug 23, 2018

Can we also combine the code in #168 to this PR to let the flask, django, pyramid integrations be able to recognize the B3 format? Or we can put that part in a separate PR. This PR LGTM.

@songy23
Copy link
Copy Markdown
Contributor

songy23 commented Aug 23, 2018

Should to_headers take a parent SpanContext, or should the parent be included in SpanContext?

Just took a look at the Java implementation, looks like ParentSpanId should not be propagated: https://github.com/census-instrumentation/opencensus-java/pull/889/files#r155878293.

/cc @adriancole

@codefromthecrypt
Copy link
Copy Markdown

codefromthecrypt commented Aug 23, 2018 via email

@kornholi
Copy link
Copy Markdown
Contributor Author

Can we also combine the code in #168 to this PR to let the flask, django, pyramid integrations be able to recognize the B3 format? Or we can put that part in a separate PR. This PR LGTM.

It already works because of @reyang's refactoring. Just have to set PROPAGATOR to B3Propagator in OPENCENSUS_TRACE config.

x-b3-flags means force trace and really only ever set to 1 (header left out
otherwise). if there is no force/debug trace feature main thing is just
propagating what if any was incoming

Right, the issue here is that our propagators basically act as (de)serializers for SpanContext, and SpanContext currently does not encode that information. I'm trying to figure out the best way to refactor this.

@reyang
Copy link
Copy Markdown
Contributor

reyang commented Aug 24, 2018

@kornholi regarding "SpanContext currently does not encode that information", would you try putting the info in SpanContext.tracestate (which is an ordered-key-value-pair intended for extensibility)?

@codefromthecrypt
Copy link
Copy Markdown

codefromthecrypt commented Aug 25, 2018 via email

@kornholi
Copy link
Copy Markdown
Contributor Author

kornholi commented Sep 4, 2018

I think this is ready to be merged.

Regarding ParentSpanId and Debug headers, the other implementations strip them away so we should do the same here. See https://github.com/census-instrumentation/opencensus-go/blob/264a2a48d94c062252389fffbc308ba555e35166/plugin/ochttp/propagation/b3/b3.go#L35-L43 etc. It's still unclear to me why we're piggybacking on the B3 name if it's not actually B3 (split client/server spans).

"""

def from_headers(self, headers):
"""Generate a SpanContext object using the trace context headers.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comment has "trace context headers" while the code is using Zipkin headers.
Perhaps we should remove these excessive comments? I don't find them quite useful as I can tell what the function is doing from its name "from_headers".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree. However they are short enough so I just clarified the B3 part. We need to set some guidelines as right now the comment quality/quantity varies greatly.

Copy link
Copy Markdown

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

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

X-B3-Flags is only valid if set to '1' and when set implied sampled and also should set span.debug=true when sent out of band.


trace_id = headers.get(_TRACE_ID_KEY)
span_id = headers.get(_SPAN_ID_KEY)
sampled = headers.get(_SAMPLED_KEY) == '1'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

the header can be absent. when it is it should be sampled using whatever your heuristic is not set to unsampled. make sense?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

From what I understand this will work if it's missing, because later we call .enabled or .should_sample here

@codefromthecrypt
Copy link
Copy Markdown

if it is still open (looks the case), then yeah probably worth adding b3 single indeed

@akoshochrein
Copy link
Copy Markdown

Hey, I'd love this to get merged. What is required exactly?

@kornholi
Copy link
Copy Markdown
Contributor Author

kornholi commented Nov 3, 2018

Support for the single-header b3 format has been added, but I have no way to test it in the real world. I think at this point the PR should be good to go. There are some remaining issues, but I don't think this is the place to solve them right now.

From the top of my mind:

  • Debug traces can't be encoded properly in the current SpanContext/TraceOptions.
  • There's no way to deny sampling. Even if it is set to 0 in the TraceOptions, the tracer might still sample depending on the configuration.
  • Serialization into the single-header format.

@kornholi
Copy link
Copy Markdown
Contributor Author

@liyanhui1228 how should we move forward?

@c24t
Copy link
Copy Markdown
Member

c24t commented Nov 12, 2018

Hey @kornholi, I'm working with @liyanhui1228 and can take a look at this PR this week.

@kornholi
Copy link
Copy Markdown
Contributor Author

kornholi commented Dec 4, 2018

@c24t ping

@c24t
Copy link
Copy Markdown
Member

c24t commented Dec 4, 2018

Sorry to hold you up here, will take a look soon.

@reyang
Copy link
Copy Markdown
Contributor

reyang commented Feb 6, 2019

@kornholi would you help to update the branch? Let's get this merged given the major part looks good. We can track issues in case there are open questions, we shouldn't get blocked here entirely. (once the branch got updated, we will wait for 24 hours to see if people have strong objections before I go ahead and merge)
Adding @akoskaaa for awareness.

Once this got this PR merged, we can close #168 since it is a duplicated effort. @JustinWP

@kornholi
Copy link
Copy Markdown
Contributor Author

kornholi commented Feb 6, 2019

Thanks @reyang, I rebased the branch. I agree on fixing the rest of issues separately, this has been sitting here too long :)

@reyang reyang merged commit f029bcd into census-instrumentation:master Feb 7, 2019
@reyang reyang mentioned this pull request Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants