14
14
15
15
import typing
16
16
import unittest
17
+ from unittest .mock import Mock
17
18
18
19
from opentelemetry import trace
19
20
from opentelemetry .context .propagation import tracecontexthttptextformat
@@ -38,7 +39,8 @@ def test_no_traceparent_header(self):
38
39
39
40
RFC 4.2.2:
40
41
41
- If no traceparent header is received, the vendor creates a new trace-id and parent-id that represents the current request.
42
+ If no traceparent header is received, the vendor creates a new
43
+ trace-id and parent-id that represents the current request.
42
44
"""
43
45
output = {} # type:typing.Dict[str, typing.List[str]]
44
46
span_context = FORMAT .extract (get_as_list , output )
@@ -66,8 +68,10 @@ def test_headers_with_tracestate(self):
66
68
span_context .trace_state , {"foo" : "1" , "bar" : "2" , "baz" : "3" }
67
69
)
68
70
71
+ mock_span = Mock ()
72
+ mock_span .configure_mock (** {"get_context.return_value" : span_context })
69
73
output = {} # type:typing.Dict[str, str]
70
- FORMAT .inject (span_context , dict .__setitem__ , output )
74
+ FORMAT .inject (mock_span , dict .__setitem__ , output )
71
75
self .assertEqual (output ["traceparent" ], traceparent_value )
72
76
for pair in ["foo=1" , "bar=2" , "baz=3" ]:
73
77
self .assertIn (pair , output ["tracestate" ])
@@ -81,13 +85,16 @@ def test_invalid_trace_id(self):
81
85
82
86
RFC 3.2.2.3
83
87
84
- If the trace-id value is invalid (for example if it contains non-allowed characters or all
85
- zeros), vendors MUST ignore the traceparent.
88
+ If the trace-id value is invalid (for example if it contains
89
+ non-allowed characters or all zeros), vendors MUST ignore the
90
+ traceparent.
86
91
87
92
RFC 3.3
88
93
89
- If the vendor failed to parse traceparent, it MUST NOT attempt to parse tracestate.
90
- Note that the opposite is not true: failure to parse tracestate MUST NOT affect the parsing of traceparent.
94
+ If the vendor failed to parse traceparent, it MUST NOT attempt to
95
+ parse tracestate.
96
+ Note that the opposite is not true: failure to parse tracestate MUST
97
+ NOT affect the parsing of traceparent.
91
98
"""
92
99
span_context = FORMAT .extract (
93
100
get_as_list ,
@@ -101,19 +108,22 @@ def test_invalid_trace_id(self):
101
108
self .assertEqual (span_context , trace .INVALID_SPAN_CONTEXT )
102
109
103
110
def test_invalid_parent_id (self ):
104
- """If the parent id is invalid, we must ignore the full traceparent header.
111
+ """If the parent id is invalid, we must ignore the full traceparent
112
+ header.
105
113
106
114
Also ignore any tracestate.
107
115
108
116
RFC 3.2.2.3
109
117
110
- Vendors MUST ignore the traceparent when the parent-id is invalid (for example,
111
- if it contains non-lowercase hex characters).
118
+ Vendors MUST ignore the traceparent when the parent-id is invalid (for
119
+ example, if it contains non-lowercase hex characters).
112
120
113
121
RFC 3.3
114
122
115
- If the vendor failed to parse traceparent, it MUST NOT attempt to parse tracestate.
116
- Note that the opposite is not true: failure to parse tracestate MUST NOT affect the parsing of traceparent.
123
+ If the vendor failed to parse traceparent, it MUST NOT attempt to parse
124
+ tracestate.
125
+ Note that the opposite is not true: failure to parse tracestate MUST
126
+ NOT affect the parsing of traceparent.
117
127
"""
118
128
span_context = FORMAT .extract (
119
129
get_as_list ,
@@ -131,15 +141,19 @@ def test_no_send_empty_tracestate(self):
131
141
132
142
RFC 3.3.1.1
133
143
134
- Empty and whitespace-only list members are allowed. Vendors MUST accept empty
135
- tracestate headers but SHOULD avoid sending them.
144
+ Empty and whitespace-only list members are allowed. Vendors MUST accept
145
+ empty tracestate headers but SHOULD avoid sending them.
136
146
"""
137
147
output = {} # type:typing.Dict[str, str]
138
- FORMAT .inject (
139
- trace .SpanContext (self .TRACE_ID , self .SPAN_ID ),
140
- dict .__setitem__ ,
141
- output ,
148
+ mock_span = Mock ()
149
+ mock_span .configure_mock (
150
+ ** {
151
+ "get_context.return_value" : trace .SpanContext (
152
+ self .TRACE_ID , self .SPAN_ID
153
+ )
154
+ }
142
155
)
156
+ FORMAT .inject (mock_span , dict .__setitem__ , output )
143
157
self .assertTrue ("traceparent" in output )
144
158
self .assertFalse ("tracestate" in output )
145
159
@@ -155,22 +169,23 @@ def test_format_not_supported(self):
155
169
get_as_list ,
156
170
{
157
171
"traceparent" : [
158
- "00-12345678901234567890123456789012-1234567890123456-00-residue"
172
+ "00-12345678901234567890123456789012-"
173
+ "1234567890123456-00-residue"
159
174
],
160
175
"tracestate" : ["foo=1,bar=2,foo=3" ],
161
176
},
162
177
)
163
178
self .assertEqual (span_context , trace .INVALID_SPAN_CONTEXT )
164
179
165
180
def test_propagate_invalid_context (self ):
166
- """Do not propagate invalid trace context.
167
- """
181
+ """Do not propagate invalid trace context."""
168
182
output = {} # type:typing.Dict[str, str]
169
- FORMAT .inject (trace .INVALID_SPAN_CONTEXT , dict .__setitem__ , output )
183
+ FORMAT .inject (trace .Span () , dict .__setitem__ , output )
170
184
self .assertFalse ("traceparent" in output )
171
185
172
186
def test_tracestate_empty_header (self ):
173
- """Test tracestate with an additional empty header (should be ignored)"""
187
+ """Test tracestate with an additional empty header (should be ignored)
188
+ """
174
189
span_context = FORMAT .extract (
175
190
get_as_list ,
176
191
{
0 commit comments