-
Notifications
You must be signed in to change notification settings - Fork 651
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
Implementing Propagators API to use Context #446
Changes from 3 commits
0f3b8c1
bfee040
e74972c
1bb75a5
fcabf00
fe6bf4b
6484b1b
914ad6b
8d22934
03ea072
5bc4f60
80c5c07
6c21258
a5ca36f
8869521
a94ee8b
154ab5b
90c0535
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,12 @@ | |
import typing | ||
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. should tracecontext be moved to trace/propagation? It's a valid hierarchy. I don't see anything in the spec that calls out specifically where this should be: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/library-layout.md 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. makes more sense in |
||
|
||
import opentelemetry.trace as trace | ||
from opentelemetry.context.propagation import httptextformat | ||
|
||
_T = typing.TypeVar("_T") | ||
from opentelemetry.context.context import Context | ||
from opentelemetry.trace.propagation import ( | ||
get_span_from_context, | ||
httptextformat, | ||
set_span_in_context, | ||
) | ||
|
||
# Keys and values are strings of up to 256 printable US-ASCII characters. | ||
# Implementations should conform to the `W3C Trace Context - Tracestate`_ | ||
|
@@ -61,32 +64,37 @@ class TraceContextHTTPTextFormat(httptextformat.HTTPTextFormat): | |
|
||
@classmethod | ||
def extract( | ||
cls, get_from_carrier: httptextformat.Getter[_T], carrier: _T | ||
) -> trace.SpanContext: | ||
cls, | ||
get_from_carrier: httptextformat.Getter[ | ||
httptextformat.HTTPTextFormatT | ||
], | ||
carrier: httptextformat.HTTPTextFormatT, | ||
context: typing.Optional[Context] = None, | ||
) -> Context: | ||
"""Extracts a valid SpanContext from the carrier. | ||
""" | ||
header = get_from_carrier(carrier, cls._TRACEPARENT_HEADER_NAME) | ||
|
||
if not header: | ||
return trace.INVALID_SPAN_CONTEXT | ||
return set_span_in_context(trace.INVALID_SPAN, context) | ||
|
||
match = re.search(cls._TRACEPARENT_HEADER_FORMAT_RE, header[0]) | ||
if not match: | ||
return trace.INVALID_SPAN_CONTEXT | ||
return set_span_in_context(trace.INVALID_SPAN, context) | ||
|
||
version = match.group(1) | ||
trace_id = match.group(2) | ||
span_id = match.group(3) | ||
trace_options = match.group(4) | ||
|
||
if trace_id == "0" * 32 or span_id == "0" * 16: | ||
return trace.INVALID_SPAN_CONTEXT | ||
return set_span_in_context(trace.INVALID_SPAN, context) | ||
|
||
if version == "00": | ||
if match.group(5): | ||
return trace.INVALID_SPAN_CONTEXT | ||
return set_span_in_context(trace.INVALID_SPAN, context) | ||
if version == "ff": | ||
return trace.INVALID_SPAN_CONTEXT | ||
return set_span_in_context(trace.INVALID_SPAN, context) | ||
|
||
tracestate_headers = get_from_carrier( | ||
carrier, cls._TRACESTATE_HEADER_NAME | ||
|
@@ -99,29 +107,29 @@ def extract( | |
trace_options=trace.TraceOptions(trace_options), | ||
trace_state=tracestate, | ||
) | ||
|
||
return span_context | ||
return set_span_in_context(trace.DefaultSpan(span_context), context) | ||
|
||
@classmethod | ||
def inject( | ||
cls, | ||
span: trace.Span, | ||
set_in_carrier: httptextformat.Setter[_T], | ||
carrier: _T, | ||
set_in_carrier: httptextformat.Setter[httptextformat.HTTPTextFormatT], | ||
carrier: httptextformat.HTTPTextFormatT, | ||
context: typing.Optional[Context] = None, | ||
) -> None: | ||
span_context = get_span_from_context(context).get_context() | ||
|
||
context = span.get_context() | ||
|
||
if context == trace.INVALID_SPAN_CONTEXT: | ||
if span_context == trace.INVALID_SPAN_CONTEXT: | ||
return | ||
traceparent_string = "00-{:032x}-{:016x}-{:02x}".format( | ||
context.trace_id, context.span_id, context.trace_options | ||
span_context.trace_id, | ||
span_context.span_id, | ||
span_context.trace_options, | ||
) | ||
set_in_carrier( | ||
carrier, cls._TRACEPARENT_HEADER_NAME, traceparent_string | ||
) | ||
if context.trace_state: | ||
tracestate_string = _format_tracestate(context.trace_state) | ||
if span_context.trace_state: | ||
tracestate_string = _format_tracestate(span_context.trace_state) | ||
set_in_carrier( | ||
carrier, cls._TRACESTATE_HEADER_NAME, tracestate_string | ||
) | ||
|
This file was deleted.
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 think we may need to add another line here to extract or pass through correlation context, since we're not attaching this context as we start the activation.
Would it make sense to add context attach / detach to these extensions?
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 think so. added an attach/detach to flask and wsgi extensions.