Skip to content

Commit 7728ca0

Browse files
committed
Remove setters and getters
Fixes open-telemetry#1644
1 parent c81fd5e commit 7728ca0

File tree

9 files changed

+143
-298
lines changed

9 files changed

+143
-298
lines changed

opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py

+12-25
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class W3CBaggagePropagator(textmap.TextMapPropagator):
3131

3232
def extract(
3333
self,
34-
getter: textmap.Getter[textmap.TextMapPropagatorT],
3534
carrier: textmap.TextMapPropagatorT,
3635
context: typing.Optional[Context] = None,
3736
) -> Context:
@@ -44,11 +43,14 @@ def extract(
4443
if context is None:
4544
context = get_current()
4645

47-
header = _extract_first_element(
48-
getter.get(carrier, self._BAGGAGE_HEADER_NAME)
49-
)
46+
value = carrier.get(self._BAGGAGE_HEADER_NAME)
5047

51-
if not header or len(header) > self._MAX_HEADER_LENGTH:
48+
if value is None:
49+
header = None
50+
else:
51+
header = next(iter(value), None)
52+
53+
if header is None or len(header) > self._MAX_HEADER_LENGTH:
5254
return context
5355

5456
baggage_entries = header.split(",")
@@ -73,7 +75,6 @@ def extract(
7375

7476
def inject(
7577
self,
76-
set_in_carrier: textmap.Setter[textmap.TextMapPropagatorT],
7778
carrier: textmap.TextMapPropagatorT,
7879
context: typing.Optional[Context] = None,
7980
) -> None:
@@ -83,28 +84,14 @@ def inject(
8384
`opentelemetry.propagators.textmap.TextMapPropagator.inject`
8485
"""
8586
baggage_entries = baggage.get_all(context=context)
86-
if not baggage_entries:
87-
return
8887

89-
baggage_string = _format_baggage(baggage_entries)
90-
set_in_carrier(carrier, self._BAGGAGE_HEADER_NAME, baggage_string)
88+
if baggage_entries:
89+
carrier[self._BAGGAGE_HEADER_NAME] = ",".join(
90+
key + "=" + urllib.parse.quote_plus(str(value))
91+
for key, value in baggage_entries.items()
92+
)
9193

9294
@property
9395
def fields(self) -> typing.Set[str]:
9496
"""Returns a set with the fields set in `inject`."""
9597
return {self._BAGGAGE_HEADER_NAME}
96-
97-
98-
def _format_baggage(baggage_entries: typing.Mapping[str, object]) -> str:
99-
return ",".join(
100-
key + "=" + urllib.parse.quote_plus(str(value))
101-
for key, value in baggage_entries.items()
102-
)
103-
104-
105-
def _extract_first_element(
106-
items: typing.Optional[typing.Iterable[textmap.TextMapPropagatorT]],
107-
) -> typing.Optional[textmap.TextMapPropagatorT]:
108-
if items is None:
109-
return None
110-
return next(iter(items), None)

opentelemetry-api/src/opentelemetry/propagate/__init__.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,12 @@
4040
PROPAGATOR = propagators.get_global_textmap()
4141
4242
43-
def get_header_from_flask_request(request, key):
44-
return request.headers.get_all(key)
45-
46-
def set_header_into_requests_request(request: requests.Request,
47-
key: str, value: str):
48-
request.headers[key] = value
49-
5043
def example_route():
51-
context = PROPAGATOR.extract(
52-
get_header_from_flask_request,
53-
flask.request
54-
)
44+
context = PROPAGATOR.extract(flask.request)
5545
request_to_downstream = requests.Request(
5646
"GET", "http://httpbin.org/get"
5747
)
5848
PROPAGATOR.inject(
59-
set_header_into_requests_request,
6049
request_to_downstream,
6150
context=context
6251
)
@@ -82,7 +71,6 @@ def example_route():
8271

8372

8473
def extract(
85-
getter: textmap.Getter[textmap.TextMapPropagatorT],
8674
carrier: textmap.TextMapPropagatorT,
8775
context: typing.Optional[Context] = None,
8876
) -> Context:
@@ -99,26 +87,22 @@ def extract(
9987
context: an optional Context to use. Defaults to current
10088
context if not set.
10189
"""
102-
return get_global_textmap().extract(getter, carrier, context)
90+
return get_global_textmap().extract(carrier, context)
10391

10492

10593
def inject(
106-
set_in_carrier: textmap.Setter[textmap.TextMapPropagatorT],
10794
carrier: textmap.TextMapPropagatorT,
10895
context: typing.Optional[Context] = None,
10996
) -> None:
11097
"""Uses the configured propagator to inject a Context into the carrier.
11198
11299
Args:
113-
set_in_carrier: A setter function that can set values
114-
on the carrier.
115-
carrier: An object that contains a representation of HTTP
116-
headers. Should be paired with set_in_carrier, which
117-
should know how to set header values on the carrier.
100+
carrier: A dict-like object that contains a representation of HTTP
101+
headers.
118102
context: an optional Context to use. Defaults to current
119103
context if not set.
120104
"""
121-
get_global_textmap().inject(set_in_carrier, carrier, context)
105+
get_global_textmap().inject(carrier, context)
122106

123107

124108
try:

opentelemetry-api/src/opentelemetry/propagators/composite.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15-
import typing
15+
from typing import Optional, Set, Sequence
1616

1717
from opentelemetry.context.context import Context
1818
from opentelemetry.propagators import textmap
@@ -29,32 +29,31 @@ class CompositeHTTPPropagator(textmap.TextMapPropagator):
2929
"""
3030

3131
def __init__(
32-
self, propagators: typing.Sequence[textmap.TextMapPropagator]
32+
self, propagators: Sequence[textmap.TextMapPropagator]
3333
) -> None:
3434
self._propagators = propagators
3535

3636
def extract(
3737
self,
38-
getter: textmap.Getter[textmap.TextMapPropagatorT],
3938
carrier: textmap.TextMapPropagatorT,
40-
context: typing.Optional[Context] = None,
39+
context: Optional[Context] = None,
4140
) -> Context:
42-
"""Run each of the configured propagators with the given context and carrier.
41+
"""Run each of the configured propagators with the given context and
42+
carrier.
4343
Propagators are run in the order they are configured, if multiple
44-
propagators write the same context key, the propagator later in the list
45-
will override previous propagators.
44+
propagators write the same context key, the propagator later in the
45+
list will override previous propagators.
4646
4747
See `opentelemetry.propagators.textmap.TextMapPropagator.extract`
4848
"""
4949
for propagator in self._propagators:
50-
context = propagator.extract(getter, carrier, context)
50+
context = propagator.extract(carrier, context)
5151
return context # type: ignore
5252

5353
def inject(
5454
self,
55-
set_in_carrier: textmap.Setter[textmap.TextMapPropagatorT],
5655
carrier: textmap.TextMapPropagatorT,
57-
context: typing.Optional[Context] = None,
56+
context: Optional[Context] = None,
5857
) -> None:
5958
"""Run each of the configured propagators with the given context and carrier.
6059
Propagators are run in the order they are configured, if multiple
@@ -64,10 +63,10 @@ def inject(
6463
See `opentelemetry.propagators.textmap.TextMapPropagator.inject`
6564
"""
6665
for propagator in self._propagators:
67-
propagator.inject(set_in_carrier, carrier, context)
66+
propagator.inject(carrier, context)
6867

6968
@property
70-
def fields(self) -> typing.Set[str]:
69+
def fields(self) -> Set[str]:
7170
"""Returns a set with the fields set in `inject`.
7271
7372
See

opentelemetry-api/src/opentelemetry/propagators/textmap.py

+1-72
Original file line numberDiff line numberDiff line change
@@ -18,66 +18,6 @@
1818
from opentelemetry.context.context import Context
1919

2020
TextMapPropagatorT = typing.TypeVar("TextMapPropagatorT")
21-
CarrierValT = typing.Union[typing.List[str], str]
22-
23-
Setter = typing.Callable[[TextMapPropagatorT, str, str], None]
24-
25-
26-
class Getter(typing.Generic[TextMapPropagatorT]):
27-
"""This class implements a Getter that enables extracting propagated
28-
fields from a carrier.
29-
"""
30-
31-
def get(
32-
self, carrier: TextMapPropagatorT, key: str
33-
) -> typing.Optional[typing.List[str]]:
34-
"""Function that can retrieve zero
35-
or more values from the carrier. In the case that
36-
the value does not exist, returns None.
37-
38-
Args:
39-
carrier: An object which contains values that are used to
40-
construct a Context.
41-
key: key of a field in carrier.
42-
Returns: first value of the propagation key or None if the key doesn't
43-
exist.
44-
"""
45-
raise NotImplementedError()
46-
47-
def keys(self, carrier: TextMapPropagatorT) -> typing.List[str]:
48-
"""Function that can retrieve all the keys in a carrier object.
49-
50-
Args:
51-
carrier: An object which contains values that are
52-
used to construct a Context.
53-
Returns:
54-
list of keys from the carrier.
55-
"""
56-
raise NotImplementedError()
57-
58-
59-
class DictGetter(Getter[typing.Dict[str, CarrierValT]]):
60-
def get(
61-
self, carrier: typing.Dict[str, CarrierValT], key: str
62-
) -> typing.Optional[typing.List[str]]:
63-
"""Getter implementation to retrieve a value from a dictionary.
64-
65-
Args:
66-
carrier: dictionary in which header
67-
key: the key used to get the value
68-
Returns:
69-
A list with a single string with the value if it exists, else None.
70-
"""
71-
val = carrier.get(key, None)
72-
if val is None:
73-
return None
74-
if isinstance(val, typing.Iterable) and not isinstance(val, str):
75-
return list(val)
76-
return [val]
77-
78-
def keys(self, carrier: typing.Dict[str, CarrierValT]) -> typing.List[str]:
79-
"""Keys implementation that returns all keys from a dictionary."""
80-
return list(carrier.keys())
8121

8222

8323
class TextMapPropagator(abc.ABC):
@@ -92,7 +32,6 @@ class TextMapPropagator(abc.ABC):
9232
@abc.abstractmethod
9333
def extract(
9434
self,
95-
getter: Getter[TextMapPropagatorT],
9635
carrier: TextMapPropagatorT,
9736
context: typing.Optional[Context] = None,
9837
) -> Context:
@@ -103,9 +42,6 @@ def extract(
10342
Context value and return it.
10443
10544
Args:
106-
getter: a function that can retrieve zero
107-
or more values from the carrier. In the case that
108-
the value does not exist, return an empty list.
10945
carrier: and object which contains values that are
11046
used to construct a Context. This object
11147
must be paired with an appropriate getter
@@ -120,23 +56,16 @@ def extract(
12056
@abc.abstractmethod
12157
def inject(
12258
self,
123-
set_in_carrier: Setter[TextMapPropagatorT],
12459
carrier: TextMapPropagatorT,
12560
context: typing.Optional[Context] = None,
12661
) -> None:
12762
"""Inject values from a Context into a carrier.
12863
12964
inject enables the propagation of values into HTTP clients or
13065
other objects which perform an HTTP request. Implementations
131-
should use the set_in_carrier method to set values on the
132-
carrier.
13366
13467
Args:
135-
set_in_carrier: A setter function that can set values
136-
on the carrier.
137-
carrier: An object that a place to define HTTP headers.
138-
Should be paired with set_in_carrier, which should
139-
know how to set header values on the carrier.
68+
carrier: An dict-like object where to store HTTP headers.
14069
context: an optional Context to use. Defaults to current
14170
context if not set.
14271

0 commit comments

Comments
 (0)