Skip to content

Commit

Permalink
Added generic to textmap getter and setter (#2657)
Browse files Browse the repository at this point in the history
* added generic to textmap getter and setter

* added CHANGELOG entry

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
  • Loading branch information
alanisaac and srikanthccv authored May 27, 2022
1 parent eed40f7 commit cad776a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 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/compare/v1.12.0rc1-0.31b0...HEAD)

- Fix type hints for textmap `Getter` and `Setter`
([#2657](https://github.com/open-telemetry/opentelemetry-python/pull/2657))
- Fix LogEmitterProvider.force_flush hanging randomly
([#2714](https://github.com/open-telemetry/opentelemetry-python/pull/2714))
- narrow protobuf dependencies to exclude protobuf >= 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def extract(
self,
carrier: textmap.CarrierT,
context: Optional[Context] = None,
getter: textmap.Getter = textmap.default_getter,
getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter,
) -> Context:
"""Extract Baggage from the carrier.
Expand Down Expand Up @@ -109,7 +109,7 @@ def inject(
self,
carrier: textmap.CarrierT,
context: Optional[Context] = None,
setter: textmap.Setter = textmap.default_setter,
setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter,
) -> None:
"""Injects Baggage into the carrier.
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/propagate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def example_route():
def extract(
carrier: textmap.CarrierT,
context: typing.Optional[Context] = None,
getter: textmap.Getter = textmap.default_getter,
getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter,
) -> Context:
"""Uses the configured propagator to extract a Context from the carrier.
Expand All @@ -105,7 +105,7 @@ def extract(
def inject(
carrier: textmap.CarrierT,
context: typing.Optional[Context] = None,
setter: textmap.Setter = textmap.default_setter,
setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter,
) -> None:
"""Uses the configured propagator to inject a Context into the carrier.
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/propagators/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def extract(
self,
carrier: textmap.CarrierT,
context: typing.Optional[Context] = None,
getter: textmap.Getter = textmap.default_getter,
getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter,
) -> Context:
"""Run each of the configured propagators with the given context and carrier.
Propagators are run in the order they are configured, if multiple
Expand All @@ -56,7 +56,7 @@ def inject(
self,
carrier: textmap.CarrierT,
context: typing.Optional[Context] = None,
setter: textmap.Setter = textmap.default_setter,
setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter,
) -> None:
"""Run each of the configured propagators with the given context and carrier.
Propagators are run in the order they are configured, if multiple
Expand Down
24 changes: 12 additions & 12 deletions opentelemetry-api/src/opentelemetry/propagators/textmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
CarrierValT = typing.Union[typing.List[str], str]


class Getter(abc.ABC):
class Getter(abc.ABC, typing.Generic[CarrierT]):
"""This class implements a Getter that enables extracting propagated
fields from a carrier.
"""
Expand Down Expand Up @@ -54,7 +54,7 @@ def keys(self, carrier: CarrierT) -> typing.List[str]:
"""


class Setter(abc.ABC):
class Setter(abc.ABC, typing.Generic[CarrierT]):
"""This class implements a Setter that enables injecting propagated
fields into a carrier.
"""
Expand All @@ -71,8 +71,8 @@ def set(self, carrier: CarrierT, key: str, value: str) -> None:
"""


class DefaultGetter(Getter):
def get( # type: ignore
class DefaultGetter(Getter[typing.Mapping[str, CarrierValT]]):
def get(
self, carrier: typing.Mapping[str, CarrierValT], key: str
) -> typing.Optional[typing.List[str]]:
"""Getter implementation to retrieve a value from a dictionary.
Expand All @@ -90,18 +90,18 @@ def get( # type: ignore
return list(val)
return [val]

def keys( # type: ignore
self, carrier: typing.Dict[str, CarrierValT]
def keys(
self, carrier: typing.Mapping[str, CarrierValT]
) -> typing.List[str]:
"""Keys implementation that returns all keys from a dictionary."""
return list(carrier.keys())


default_getter = DefaultGetter()
default_getter: Getter[CarrierT] = DefaultGetter() # type: ignore


class DefaultSetter(Setter):
def set( # type: ignore
class DefaultSetter(Setter[typing.MutableMapping[str, CarrierValT]]):
def set(
self,
carrier: typing.MutableMapping[str, CarrierValT],
key: str,
Expand All @@ -117,7 +117,7 @@ def set( # type: ignore
carrier[key] = value


default_setter = DefaultSetter()
default_setter: Setter[CarrierT] = DefaultSetter() # type: ignore


class TextMapPropagator(abc.ABC):
Expand All @@ -134,7 +134,7 @@ def extract(
self,
carrier: CarrierT,
context: typing.Optional[Context] = None,
getter: Getter = default_getter,
getter: Getter[CarrierT] = default_getter,
) -> Context:
"""Create a Context from values in the carrier.
Expand Down Expand Up @@ -162,7 +162,7 @@ def inject(
self,
carrier: CarrierT,
context: typing.Optional[Context] = None,
setter: Setter = default_setter,
setter: Setter[CarrierT] = default_setter,
) -> None:
"""Inject values from a Context into a carrier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def extract(
self,
carrier: textmap.CarrierT,
context: typing.Optional[Context] = None,
getter: textmap.Getter = textmap.default_getter,
getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter,
) -> Context:
"""Extracts SpanContext from the carrier.
Expand Down Expand Up @@ -90,7 +90,7 @@ def inject(
self,
carrier: textmap.CarrierT,
context: typing.Optional[Context] = None,
setter: textmap.Setter = textmap.default_setter,
setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter,
) -> None:
"""Injects SpanContext into the carrier.
Expand Down

0 comments on commit cad776a

Please sign in to comment.