17
17
18
18
from opentelemetry .context .context import Context
19
19
20
- TextMapPropagatorT = typing .TypeVar ("TextMapPropagatorT " )
20
+ CarrierT = typing .TypeVar ("CarrierT " )
21
21
CarrierValT = typing .Union [typing .List [str ], str ]
22
22
23
- Setter = typing .Callable [[TextMapPropagatorT , str , str ], None ]
24
23
25
-
26
- class Getter (typing .Generic [TextMapPropagatorT ]):
24
+ class Getter (abc .ABC ):
27
25
"""This class implements a Getter that enables extracting propagated
28
26
fields from a carrier.
29
27
"""
30
28
29
+ @abc .abstractmethod
31
30
def get (
32
- self , carrier : TextMapPropagatorT , key : str
31
+ self , carrier : CarrierT , key : str
33
32
) -> typing .Optional [typing .List [str ]]:
34
33
"""Function that can retrieve zero
35
34
or more values from the carrier. In the case that
@@ -42,9 +41,9 @@ def get(
42
41
Returns: first value of the propagation key or None if the key doesn't
43
42
exist.
44
43
"""
45
- raise NotImplementedError ()
46
44
47
- def keys (self , carrier : TextMapPropagatorT ) -> typing .List [str ]:
45
+ @abc .abstractmethod
46
+ def keys (self , carrier : CarrierT ) -> typing .List [str ]:
48
47
"""Function that can retrieve all the keys in a carrier object.
49
48
50
49
Args:
@@ -53,17 +52,33 @@ def keys(self, carrier: TextMapPropagatorT) -> typing.List[str]:
53
52
Returns:
54
53
list of keys from the carrier.
55
54
"""
56
- raise NotImplementedError ()
57
55
58
56
59
- class DictGetter (Getter [typing .Dict [str , CarrierValT ]]):
60
- def get (
61
- self , carrier : typing .Dict [str , CarrierValT ], key : str
57
+ class Setter (abc .ABC ):
58
+ """This class implements a Setter that enables injecting propagated
59
+ fields into a carrier.
60
+ """
61
+
62
+ @abc .abstractmethod
63
+ def set (self , carrier : CarrierT , key : str , value : str ) -> None :
64
+ """Function that can set a value into a carrier""
65
+
66
+ Args:
67
+ carrier: An object which contains values that are used to
68
+ construct a Context.
69
+ key: key of a field in carrier.
70
+ value: value for a field in carrier.
71
+ """
72
+
73
+
74
+ class DefaultGetter (Getter ):
75
+ def get ( # type: ignore
76
+ self , carrier : typing .Mapping [str , CarrierValT ], key : str
62
77
) -> typing .Optional [typing .List [str ]]:
63
78
"""Getter implementation to retrieve a value from a dictionary.
64
79
65
80
Args:
66
- carrier: dictionary in which header
81
+ carrier: dictionary in which to get value
67
82
key: the key used to get the value
68
83
Returns:
69
84
A list with a single string with the value if it exists, else None.
@@ -75,11 +90,36 @@ def get(
75
90
return list (val )
76
91
return [val ]
77
92
78
- def keys (self , carrier : typing .Dict [str , CarrierValT ]) -> typing .List [str ]:
93
+ def keys ( # type: ignore
94
+ self , carrier : typing .Dict [str , CarrierValT ]
95
+ ) -> typing .List [str ]:
79
96
"""Keys implementation that returns all keys from a dictionary."""
80
97
return list (carrier .keys ())
81
98
82
99
100
+ default_getter = DefaultGetter ()
101
+
102
+
103
+ class DefaultSetter (Setter ):
104
+ def set ( # type: ignore
105
+ self ,
106
+ carrier : typing .MutableMapping [str , CarrierValT ],
107
+ key : str ,
108
+ value : CarrierValT ,
109
+ ) -> None :
110
+ """Setter implementation to set a value into a dictionary.
111
+
112
+ Args:
113
+ carrier: dictionary in which to set value
114
+ key: the key used to set the value
115
+ value: the value to set
116
+ """
117
+ carrier [key ] = value
118
+
119
+
120
+ default_setter = DefaultSetter ()
121
+
122
+
83
123
class TextMapPropagator (abc .ABC ):
84
124
"""This class provides an interface that enables extracting and injecting
85
125
context into headers of HTTP requests. HTTP frameworks and clients
@@ -92,9 +132,9 @@ class TextMapPropagator(abc.ABC):
92
132
@abc .abstractmethod
93
133
def extract (
94
134
self ,
95
- getter : Getter [TextMapPropagatorT ],
96
- carrier : TextMapPropagatorT ,
135
+ carrier : CarrierT ,
97
136
context : typing .Optional [Context ] = None ,
137
+ getter : Getter = default_getter ,
98
138
) -> Context :
99
139
"""Create a Context from values in the carrier.
100
140
@@ -120,25 +160,25 @@ def extract(
120
160
@abc .abstractmethod
121
161
def inject (
122
162
self ,
123
- set_in_carrier : Setter [TextMapPropagatorT ],
124
- carrier : TextMapPropagatorT ,
163
+ carrier : CarrierT ,
125
164
context : typing .Optional [Context ] = None ,
165
+ setter : Setter = default_setter ,
126
166
) -> None :
127
167
"""Inject values from a Context into a carrier.
128
168
129
169
inject enables the propagation of values into HTTP clients or
130
170
other objects which perform an HTTP request. Implementations
131
- should use the set_in_carrier method to set values on the
171
+ should use the `Setter` 's set method to set values on the
132
172
carrier.
133
173
134
174
Args:
135
- set_in_carrier: A setter function that can set values
136
- on the carrier.
137
175
carrier: An object that a place to define HTTP headers.
138
- Should be paired with set_in_carrier , which should
176
+ Should be paired with setter , which should
139
177
know how to set header values on the carrier.
140
178
context: an optional Context to use. Defaults to current
141
179
context if not set.
180
+ setter: An optional `Setter` object that can set values
181
+ on the carrier.
142
182
143
183
"""
144
184
0 commit comments