Skip to content

Commit

Permalink
Added B3 single-header propagator
Browse files Browse the repository at this point in the history
- Added B3MultiFormat and B3SingleFormat propagators.
- B3Format is now deprecated and an alias to B3MultiFormat propagator.
- Fixed `b3` entrypoint to point to `B3SingleFormat`.
- Added `b3multi` entrypoint to point to `B3MultiFormat`.
  • Loading branch information
owais committed May 8, 2021
1 parent 8a1cbf7 commit 1b70f4e
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 132 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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))

### Changed
- Fixed OTLP gRPC exporter silently failing if scheme is not specified in endpoint.
Expand All @@ -18,6 +20,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))

### Removed
- Moved `opentelemetry-instrumentation` to contrib repository.
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

0 comments on commit 1b70f4e

Please sign in to comment.