Skip to content
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

Added B3 multi-header propagator #1823

Merged
merged 5 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added example for running Django with auto instrumentation.
([#1803](https://github.com/open-telemetry/opentelemetry-python/pull/1803))
- Added `B3SingleFormat` and `B3MultiFormat` propagators to the `opentelemetry-propagator-b3` package.
([#1823](https://github.com/open-telemetry/opentelemetry-python/pull/1823))
- Added support for OTEL_SERVICE_NAME.
([#1829](https://github.com/open-telemetry/opentelemetry-python/pull/1829))

Expand All @@ -24,6 +26,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Propagators use the root context as default for `extract` and do not modify
the context if extracting from carrier does not work.
([#1811](https://github.com/open-telemetry/opentelemetry-python/pull/1811))
- Fixed `b3` propagator entrypoint to point to `B3SingleFormat` propagator.
([#1823](https://github.com/open-telemetry/opentelemetry-python/pull/1823))
- Added `b3multi` propagator entrypoint to point to `B3MultiFormat` propagator.
([#1823](https://github.com/open-telemetry/opentelemetry-python/pull/1823))
- Improve warning when failing to decode byte attribute
([#1810](https://github.com/open-telemetry/opentelemetry-python/pull/1810))
- Fixed inconsistency in parent_id formatting from the ConsoleSpanExporter
Expand Down
4 changes: 3 additions & 1 deletion propagator/opentelemetry-propagator-b3/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ package_dir=
packages=find_namespace:
install_requires =
opentelemetry-api == 1.2.0.dev0
deprecated >= 1.2.6

[options.extras_require]
test =
Expand All @@ -50,4 +51,5 @@ where = src

[options.entry_points]
opentelemetry_propagator =
b3 = opentelemetry.propagators.b3:B3Format
b3 = opentelemetry.propagators.b3:B3SingleFormat
b3multi = opentelemetry.propagators.b3:B3MultiFormat
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import typing
from re import compile as re_compile

from deprecated import deprecated

import opentelemetry.trace as trace
from opentelemetry.context import Context
from opentelemetry.propagators.textmap import (
Expand All @@ -28,10 +30,11 @@
from opentelemetry.trace import format_span_id, format_trace_id


class B3Format(TextMapPropagator):
"""Propagator for the B3 HTTP header format.
class B3MultiFormat(TextMapPropagator):
"""Propagator for the B3 HTTP multi-header format.

See: https://github.com/openzipkin/b3-propagation
https://github.com/openzipkin/b3-propagation#multiple-headers
"""

SINGLE_HEADER_KEY = "b3"
Expand Down Expand Up @@ -165,6 +168,53 @@ def fields(self) -> typing.Set[str]:
}


class B3SingleFormat(B3MultiFormat):
"""Propagator for the B3 HTTP single-header format.

See: https://github.com/openzipkin/b3-propagation
https://github.com/openzipkin/b3-propagation#single-header
"""

def inject(
self,
carrier: CarrierT,
context: typing.Optional[Context] = None,
setter: Setter = default_setter,
) -> None:
span = trace.get_current_span(context=context)

span_context = span.get_span_context()
if span_context == trace.INVALID_SPAN_CONTEXT:
return

sampled = (trace.TraceFlags.SAMPLED & span_context.trace_flags) != 0

fields = [
format_trace_id(span_context.trace_id),
format_span_id(span_context.span_id),
"1" if sampled else "0",
]

span_parent = getattr(span, "parent", None)
if span_parent:
fields.append(format_span_id(span_parent.span_id))

setter.set(carrier, self.SINGLE_HEADER_KEY, "-".join(fields))

@property
def fields(self) -> typing.Set[str]:
return {self.SINGLE_HEADER_KEY}


class B3Format(B3MultiFormat):
@deprecated(
version="1.2.0",
reason="B3Format is deprecated in favor of B3MultiFormat",
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


def _extract_first_element(
items: typing.Iterable[CarrierT],
) -> typing.Optional[CarrierT]:
Expand Down
Loading