Skip to content

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaelias committed Dec 19, 2023
1 parent 42e6183 commit 79f7ab8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion baseplate/frameworks/thrift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from opentelemetry.context import attach
from opentelemetry.context import detach
from opentelemetry.propagate import extract
from opentelemetry.sdk.trace import Tracer
from opentelemetry.semconv.trace import MessageTypeValues
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import Tracer
from prometheus_client import Counter
from prometheus_client import Gauge
from prometheus_client import Histogram
Expand Down
38 changes: 17 additions & 21 deletions baseplate/lib/propagator_redditb3.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging

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

from re import compile as re_compile

from opentelemetry import trace
from opentelemetry.context import Context
from opentelemetry.propagators.textmap import CarrierT
Expand Down Expand Up @@ -40,33 +40,29 @@ def extract(
) -> Context:
if context is None:
context = Context()
trace_id: Optional[int] = trace.INVALID_TRACE_ID
span_id: Optional[int] = trace.INVALID_SPAN_ID
sampled: Optional[int] = "0"
flags: Optional[Iterable[str]] = None

trace_id = _extract_first_element(getter.get(carrier, self.TRACE_ID_KEY), default=trace_id)
extracted_trace_id = _extract_first_element(getter.get(carrier, self.TRACE_ID_KEY))
logger.debug(
"Extracted trace_id from carrier. [carrier=%s, context=%s, trace_id=%s]",
carrier,
context,
trace_id,
extracted_trace_id,
)
span_id = _extract_first_element(getter.get(carrier, self.SPAN_ID_KEY), default=span_id)
extracted_span_id = _extract_first_element(getter.get(carrier, self.SPAN_ID_KEY))
logger.debug(
"Extracted span_id from carrier. [carrier=%s, context=%s, span_id=%s]",
carrier,
context,
span_id,
extracted_span_id,
)
sampled = _extract_first_element(getter.get(carrier, self.SAMPLED_KEY), default=sampled)
sampled = _extract_first_element(getter.get(carrier, self.SAMPLED_KEY), default="0")
logger.debug(
"Extracted sampled from carrier. [carrier=%s, context=%s, sampled=%s]",
carrier,
context,
sampled,
)
flags = _extract_first_element(getter.get(carrier, self.FLAGS_KEY), default=flags)
flags = _extract_first_element(getter.get(carrier, self.FLAGS_KEY))
logger.debug(
"Extracted flags from carrier. [carrier=%s, context=%s, flags=%s]",
carrier,
Expand All @@ -76,23 +72,23 @@ def extract(

# If we receive an invalid `trace_id` according to the w3 spec we return an empty context.
if (
trace_id == trace.INVALID_TRACE_ID
or span_id == trace.INVALID_SPAN_ID
or self._trace_id_regex.fullmatch(trace_id) is None
or self._span_id_regex.fullmatch(span_id) is None
extracted_trace_id is None
or self._trace_id_regex.fullmatch(extracted_trace_id) is None
or extracted_span_id is None
or self._span_id_regex.fullmatch(extracted_span_id) is None
):
logger.debug(
"No valid b3 traces headers in request. Aborting. [carrier=%s, context=%s, trace_id=%s, span_id=%s]",
carrier,
context,
trace_id,
span_id,
extracted_trace_id,
extracted_span_id,
)
return context

# trace and span ids are encoded in hex, so must be converted
trace_id = int(trace_id, 16)
span_id = int(span_id, 16)
trace_id = int(extracted_trace_id, 16)
span_id = int(extracted_span_id, 16)
logger.debug(
"Converted IDs to integers. [carrier=%s, context=%s, trace_id=%s, span_id=%s]",
carrier,
Expand Down Expand Up @@ -154,7 +150,7 @@ def fields(self) -> Set[str]:


def _extract_first_element(
items: Iterable[CarrierT],
items: Optional[Iterable[CarrierT]],
default: Optional[Any] = None,
) -> Optional[CarrierT]:
if items is None:
Expand Down
1 change: 0 additions & 1 deletion requirements-transitive.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ idna==2.10
imagesize==1.2.0
importlib-metadata==6.7.0
iniconfig==1.1.1
ipython==8.13.0
isort==5.7.0
Jinja2==2.11.3
kazoo==2.8.0
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ ignore_missing_imports = True
[mypy-cqlmapper.*]
ignore_missing_imports = True

[mypy-form_observability.*]
ignore_missing_imports = True

[mypy-gevent.*]
ignore_missing_imports = True

Expand Down

0 comments on commit 79f7ab8

Please sign in to comment.