11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
-
14
+ #
15
15
from unittest .mock import Mock , patch
16
16
17
+ import pytest
18
+ from falcon import __version__ as _falcon_verison
17
19
from falcon import testing
20
+ from packaging import version as package_version
18
21
19
22
from opentelemetry import trace
20
23
from opentelemetry .instrumentation .falcon import FalconInstrumentor
@@ -84,9 +87,7 @@ def test_head(self):
84
87
self ._test_method ("HEAD" )
85
88
86
89
def _test_method (self , method ):
87
- self .client ().simulate_request (
88
- method = method , path = "/hello" , remote_addr = "127.0.0.1"
89
- )
90
+ self .client ().simulate_request (method = method , path = "/hello" )
90
91
spans = self .memory_exporter .get_finished_spans ()
91
92
self .assertEqual (len (spans ), 1 )
92
93
span = spans [0 ]
@@ -105,17 +106,23 @@ def _test_method(self, method):
105
106
SpanAttributes .NET_HOST_PORT : 80 ,
106
107
SpanAttributes .HTTP_HOST : "falconframework.org" ,
107
108
SpanAttributes .HTTP_TARGET : "/" ,
108
- SpanAttributes .NET_PEER_IP : "127.0.0.1" ,
109
109
SpanAttributes .NET_PEER_PORT : "65133" ,
110
110
SpanAttributes .HTTP_FLAVOR : "1.1" ,
111
111
"falcon.resource" : "HelloWorldResource" ,
112
112
SpanAttributes .HTTP_STATUS_CODE : 201 ,
113
113
},
114
114
)
115
+ # In falcon<3, NET_PEER_IP is always set by default to 127.0.0.1
116
+ # In falcon>3, NET_PEER_IP is not set to anything by default to
117
+ # https://github.com/falconry/falcon/blob/5233d0abed977d9dab78ebadf305f5abe2eef07c/falcon/testing/helpers.py#L1168-L1172 # noqa
118
+ if SpanAttributes .NET_PEER_IP in span .attributes :
119
+ self .assertEqual (
120
+ span .attributes [SpanAttributes .NET_PEER_IP ], "127.0.0.1"
121
+ )
115
122
self .memory_exporter .clear ()
116
123
117
124
def test_404 (self ):
118
- self .client ().simulate_get ("/does-not-exist" , remote_addr = "127.0.0.1" )
125
+ self .client ().simulate_get ("/does-not-exist" )
119
126
spans = self .memory_exporter .get_finished_spans ()
120
127
self .assertEqual (len (spans ), 1 )
121
128
span = spans [0 ]
@@ -130,16 +137,22 @@ def test_404(self):
130
137
SpanAttributes .NET_HOST_PORT : 80 ,
131
138
SpanAttributes .HTTP_HOST : "falconframework.org" ,
132
139
SpanAttributes .HTTP_TARGET : "/" ,
133
- SpanAttributes .NET_PEER_IP : "127.0.0.1" ,
134
140
SpanAttributes .NET_PEER_PORT : "65133" ,
135
141
SpanAttributes .HTTP_FLAVOR : "1.1" ,
136
142
SpanAttributes .HTTP_STATUS_CODE : 404 ,
137
143
},
138
144
)
145
+ # In falcon<3, NET_PEER_IP is always set by default to 127.0.0.1
146
+ # In falcon>3, NET_PEER_IP is not set to anything by default to
147
+ # https://github.com/falconry/falcon/blob/5233d0abed977d9dab78ebadf305f5abe2eef07c/falcon/testing/helpers.py#L1168-L1172 # noqa
148
+ if SpanAttributes .NET_PEER_IP in span .attributes :
149
+ self .assertEqual (
150
+ span .attributes [SpanAttributes .NET_PEER_IP ], "127.0.0.1"
151
+ )
139
152
140
153
def test_500 (self ):
141
154
try :
142
- self .client ().simulate_get ("/error" , remote_addr = "127.0.0.1" )
155
+ self .client ().simulate_get ("/error" )
143
156
except NameError :
144
157
pass
145
158
spans = self .memory_exporter .get_finished_spans ()
@@ -161,12 +174,18 @@ def test_500(self):
161
174
SpanAttributes .NET_HOST_PORT : 80 ,
162
175
SpanAttributes .HTTP_HOST : "falconframework.org" ,
163
176
SpanAttributes .HTTP_TARGET : "/" ,
164
- SpanAttributes .NET_PEER_IP : "127.0.0.1" ,
165
177
SpanAttributes .NET_PEER_PORT : "65133" ,
166
178
SpanAttributes .HTTP_FLAVOR : "1.1" ,
167
179
SpanAttributes .HTTP_STATUS_CODE : 500 ,
168
180
},
169
181
)
182
+ # In falcon<3, NET_PEER_IP is always set by default to 127.0.0.1
183
+ # In falcon>3, NET_PEER_IP is not set to anything by default to
184
+ # https://github.com/falconry/falcon/blob/5233d0abed977d9dab78ebadf305f5abe2eef07c/falcon/testing/helpers.py#L1168-L1172 # noqa
185
+ if SpanAttributes .NET_PEER_IP in span .attributes :
186
+ self .assertEqual (
187
+ span .attributes [SpanAttributes .NET_PEER_IP ], "127.0.0.1"
188
+ )
170
189
171
190
def test_uninstrument (self ):
172
191
self .client ().simulate_get (path = "/hello" )
@@ -191,7 +210,7 @@ def test_exclude_lists(self):
191
210
self .assertEqual (len (span_list ), 1 )
192
211
193
212
def test_traced_request_attributes (self ):
194
- self .client ().simulate_get (path = "/hello? q=abc" )
213
+ self .client ().simulate_get (path = "/hello" , query_string = " q=abc" )
195
214
span = self .memory_exporter .get_finished_spans ()[0 ]
196
215
self .assertIn ("query_string" , span .attributes )
197
216
self .assertEqual (span .attributes ["query_string" ], "q=abc" )
@@ -201,7 +220,9 @@ def test_trace_response(self):
201
220
orig = get_global_response_propagator ()
202
221
set_global_response_propagator (TraceResponsePropagator ())
203
222
204
- response = self .client ().simulate_get (path = "/hello?q=abc" )
223
+ response = self .client ().simulate_get (
224
+ path = "/hello" , query_string = "q=abc"
225
+ )
205
226
self .assertTraceResponseHeaderMatchesSpan (
206
227
response .headers , self .memory_exporter .get_finished_spans ()[0 ]
207
228
)
@@ -215,7 +236,7 @@ def test_traced_not_recording(self):
215
236
mock_tracer .start_span .return_value = mock_span
216
237
with patch ("opentelemetry.trace.get_tracer" ) as tracer :
217
238
tracer .return_value = mock_tracer
218
- self .client ().simulate_get (path = "/hello? q=abc" )
239
+ self .client ().simulate_get (path = "/hello" , query_string = " q=abc" )
219
240
self .assertFalse (mock_span .is_recording ())
220
241
self .assertTrue (mock_span .is_recording .called )
221
242
self .assertFalse (mock_span .set_attribute .called )
@@ -261,7 +282,7 @@ def response_hook(self, span, req, resp):
261
282
span .update_name ("set from hook" )
262
283
263
284
def test_hooks (self ):
264
- self .client ().simulate_get (path = "/hello? q=abc" )
285
+ self .client ().simulate_get (path = "/hello" , query_string = " q=abc" )
265
286
span = self .memory_exporter .get_finished_spans ()[0 ]
266
287
267
288
self .assertEqual (span .name , "set from hook" )
@@ -343,6 +364,11 @@ def test_custom_request_header_not_added_in_internal_span(self):
343
364
for key , _ in not_expected .items ():
344
365
self .assertNotIn (key , span .attributes )
345
366
367
+ @pytest .mark .skipif (
368
+ condition = package_version .parse (_falcon_verison )
369
+ < package_version .parse ("2.0.0" ),
370
+ reason = "falcon<2 does not implement custom response headers" ,
371
+ )
346
372
def test_custom_response_header_added_in_server_span (self ):
347
373
self .client ().simulate_request (
348
374
method = "GET" , path = "/test_custom_response_headers"
@@ -366,6 +392,11 @@ def test_custom_response_header_added_in_server_span(self):
366
392
for key , _ in not_expected .items ():
367
393
self .assertNotIn (key , span .attributes )
368
394
395
+ @pytest .mark .skipif (
396
+ condition = package_version .parse (_falcon_verison )
397
+ < package_version .parse ("2.0.0" ),
398
+ reason = "falcon<2 does not implement custom response headers" ,
399
+ )
369
400
def test_custom_response_header_not_added_in_internal_span (self ):
370
401
tracer = trace .get_tracer (__name__ )
371
402
with tracer .start_as_current_span ("test" , kind = trace .SpanKind .SERVER ):
0 commit comments