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 6, 2021
1 parent 81d80aa commit a0a7769
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 114 deletions.
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.12

[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 a0a7769

Please sign in to comment.