forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_falcon.py
282 lines (240 loc) · 10.1 KB
/
test_falcon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import Mock, patch
from falcon import testing
from opentelemetry import trace
from opentelemetry.instrumentation.falcon import FalconInstrumentor
from opentelemetry.instrumentation.propagators import (
TraceResponsePropagator,
get_global_response_propagator,
set_global_response_propagator,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.test_base import TestBase
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace import StatusCode
from .app import make_app
class TestFalconBase(TestBase):
def setUp(self):
super().setUp()
self.env_patch = patch.dict(
"os.environ",
{
"OTEL_PYTHON_FALCON_EXCLUDED_URLS": "ping",
"OTEL_PYTHON_FALCON_TRACED_REQUEST_ATTRS": "query_string",
},
)
self.env_patch.start()
FalconInstrumentor().instrument(
request_hook=getattr(self, "request_hook", None),
response_hook=getattr(self, "response_hook", None),
)
self.app = make_app()
def client(self):
return testing.TestClient(self.app)
def tearDown(self):
super().tearDown()
with self.disable_logging():
FalconInstrumentor().uninstrument()
self.env_patch.stop()
class TestFalconInstrumentation(TestFalconBase, WsgiTestBase):
def test_get(self):
self._test_method("GET")
def test_post(self):
self._test_method("POST")
def test_patch(self):
self._test_method("PATCH")
def test_put(self):
self._test_method("PUT")
def test_delete(self):
self._test_method("DELETE")
def test_head(self):
self._test_method("HEAD")
def _test_method(self, method):
self.client().simulate_request(
method=method, path="/hello", remote_addr="127.0.0.1"
)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, f"HelloWorldResource.on_{method.lower()}")
self.assertEqual(span.status.status_code, StatusCode.UNSET)
self.assertEqual(
span.status.description,
None,
)
self.assertSpanHasAttributes(
span,
{
SpanAttributes.HTTP_METHOD: method,
SpanAttributes.HTTP_SERVER_NAME: "falconframework.org",
SpanAttributes.HTTP_SCHEME: "http",
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.HTTP_HOST: "falconframework.org",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.NET_PEER_PORT: "65133",
SpanAttributes.HTTP_FLAVOR: "1.1",
"falcon.resource": "HelloWorldResource",
SpanAttributes.HTTP_STATUS_CODE: 201,
},
)
self.memory_exporter.clear()
def test_404(self):
self.client().simulate_get("/does-not-exist", remote_addr="127.0.0.1")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, "HTTP GET")
self.assertEqual(span.status.status_code, StatusCode.UNSET)
self.assertSpanHasAttributes(
span,
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_SERVER_NAME: "falconframework.org",
SpanAttributes.HTTP_SCHEME: "http",
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.HTTP_HOST: "falconframework.org",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.NET_PEER_PORT: "65133",
SpanAttributes.HTTP_FLAVOR: "1.1",
SpanAttributes.HTTP_STATUS_CODE: 404,
},
)
def test_500(self):
try:
self.client().simulate_get("/error", remote_addr="127.0.0.1")
except NameError:
pass
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.name, "ErrorResource.on_get")
self.assertFalse(span.status.is_ok)
self.assertEqual(span.status.status_code, StatusCode.ERROR)
self.assertEqual(
span.status.description,
"NameError: name 'non_existent_var' is not defined",
)
self.assertSpanHasAttributes(
span,
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_SERVER_NAME: "falconframework.org",
SpanAttributes.HTTP_SCHEME: "http",
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.HTTP_HOST: "falconframework.org",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.NET_PEER_PORT: "65133",
SpanAttributes.HTTP_FLAVOR: "1.1",
SpanAttributes.HTTP_STATUS_CODE: 500,
},
)
def test_uninstrument(self):
self.client().simulate_get(path="/hello")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
self.memory_exporter.clear()
FalconInstrumentor().uninstrument()
self.app = make_app()
self.client().simulate_get(path="/hello")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
def test_exclude_lists(self):
self.client().simulate_get(path="/ping")
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 0)
self.client().simulate_get(path="/hello")
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
def test_traced_request_attributes(self):
self.client().simulate_get(path="/hello?q=abc")
span = self.memory_exporter.get_finished_spans()[0]
self.assertIn("query_string", span.attributes)
self.assertEqual(span.attributes["query_string"], "q=abc")
self.assertNotIn("not_available_attr", span.attributes)
def test_trace_response(self):
orig = get_global_response_propagator()
set_global_response_propagator(TraceResponsePropagator())
response = self.client().simulate_get(path="/hello?q=abc")
self.assertTraceResponseHeaderMatchesSpan(
response.headers, self.memory_exporter.get_finished_spans()[0]
)
set_global_response_propagator(orig)
def test_traced_not_recording(self):
mock_tracer = Mock()
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
self.client().simulate_get(path="/hello?q=abc")
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)
class TestFalconInstrumentationWithTracerProvider(TestBase):
def setUp(self):
super().setUp()
resource = Resource.create({"resource-key": "resource-value"})
result = self.create_tracer_provider(resource=resource)
tracer_provider, exporter = result
self.exporter = exporter
FalconInstrumentor().instrument(tracer_provider=tracer_provider)
self.app = make_app()
def client(self):
return testing.TestClient(self.app)
def tearDown(self):
super().tearDown()
with self.disable_logging():
FalconInstrumentor().uninstrument()
def test_traced_request(self):
self.client().simulate_request(method="GET", path="/hello")
spans = self.exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(
span.resource.attributes["resource-key"], "resource-value"
)
self.exporter.clear()
class TestFalconInstrumentationHooks(TestFalconBase):
# pylint: disable=no-self-use
def request_hook(self, span, req):
span.set_attribute("request_hook_attr", "value from hook")
def response_hook(self, span, req, resp):
span.update_name("set from hook")
def test_hooks(self):
self.client().simulate_get(path="/hello?q=abc")
span = self.memory_exporter.get_finished_spans()[0]
self.assertEqual(span.name, "set from hook")
self.assertIn("request_hook_attr", span.attributes)
self.assertEqual(
span.attributes["request_hook_attr"], "value from hook"
)
class TestFalconInstrumentationWrappedWithOtherFramework(TestFalconBase):
def test_mark_span_internal_in_presence_of_span_from_other_framework(self):
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span(
"test", kind=trace.SpanKind.SERVER
) as parent_span:
self.client().simulate_request(method="GET", path="/hello")
span = self.memory_exporter.get_finished_spans()[0]
assert span.status.is_ok
self.assertEqual(trace.SpanKind.INTERNAL, span.kind)
self.assertEqual(
span.parent.span_id, parent_span.get_span_context().span_id
)