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

Fix the empty headers use case for OTTracePropagator #378

Merged
merged 4 commits into from
Mar 25, 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
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)