Skip to content

Commit

Permalink
Fix the empty headers use case for OTTracePropagator (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinzaremba authored Mar 25, 2021
1 parent f8e51c4 commit 2f088f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python-contrib/compare/v0.18b0...HEAD)
- Updated instrumentations to use `opentelemetry.trace.use_span` instead of `Tracer.use_span()`
([#364](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/364))
- `opentelemetry-propagator-ot-trace` Do not throw an exception when headers are not present
([#378](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/378))

### Changed
- Rename `IdsGenerator` to `IdGenerator`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from re import compile as re_compile
from typing import Iterable, Optional
from typing import Any, Iterable, Optional

from opentelemetry.baggage import get_all, set_baggage
from opentelemetry.context import Context
Expand Down Expand Up @@ -55,10 +55,12 @@ def extract(
) -> Context:

traceid = _extract_first_element(
getter.get(carrier, OT_TRACE_ID_HEADER)
getter.get(carrier, OT_TRACE_ID_HEADER), INVALID_TRACE_ID
)

spanid = _extract_first_element(getter.get(carrier, OT_SPAN_ID_HEADER))
spanid = _extract_first_element(
getter.get(carrier, OT_SPAN_ID_HEADER), INVALID_SPAN_ID
)

sampled = _extract_first_element(
getter.get(carrier, OT_SAMPLED_HEADER)
Expand Down Expand Up @@ -163,8 +165,8 @@ def fields(self):


def _extract_first_element(
items: Iterable[TextMapPropagatorT],
items: Iterable[TextMapPropagatorT], default: Any = None,
) -> Optional[TextMapPropagatorT]:
if items is None:
return None
return default
return next(iter(items), None)
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,12 @@ def test_extract_baggage(self):

self.assertEqual(baggage["abc"], "abc")
self.assertEqual(baggage["def"], "def")

def test_extract_empty(self):
"Test extraction when no headers are present"

span_context = get_current_span(
self.ot_trace_propagator.extract(carrier_getter, {})
).get_span_context()

self.assertEqual(span_context, INVALID_SPAN_CONTEXT)

0 comments on commit 2f088f8

Please sign in to comment.