Skip to content

Commit ab02af3

Browse files
committed
opentelemetry-instrumentation-falcon: prepare tests for v4
1 parent e8ae1dc commit ab02af3

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

instrumentation/opentelemetry-instrumentation-falcon/tests/app.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
# pylint:disable=R0201,W0613,E0602
55

66

7+
_parsed_falcon_version = package_version.parse(falcon.__version__)
8+
79
class HelloWorldResource:
810
def _handle_request(self, _, resp):
911
# pylint: disable=no-member
1012
resp.status = falcon.HTTP_201
1113

12-
_parsed_falcon_version = package_version.parse(falcon.__version__)
1314
if _parsed_falcon_version < package_version.parse("3.0.0"):
1415
# Falcon 1 and Falcon 2
1516
resp.body = "Hello World"
@@ -65,11 +66,15 @@ class UserResource:
6566
def on_get(self, req, resp, user_id):
6667
# pylint: disable=no-member
6768
resp.status = falcon.HTTP_200
68-
resp.body = f"Hello user {user_id}"
69+
70+
if _parsed_falcon_version < package_version.parse("3.0.0"):
71+
# Falcon 1 and Falcon 2
72+
resp.body = f"Hello user {user_id}"
73+
else:
74+
resp.text = f"Hello user {user_id}"
6975

7076

7177
def make_app():
72-
_parsed_falcon_version = package_version.parse(falcon.__version__)
7378
if _parsed_falcon_version < package_version.parse("3.0.0"):
7479
# Falcon 1 and Falcon 2
7580
app = falcon.API()

instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,30 @@ def _test_method(self, method):
124124
SpanAttributes.HTTP_SCHEME: "http",
125125
SpanAttributes.NET_HOST_PORT: 80,
126126
SpanAttributes.HTTP_HOST: "falconframework.org",
127-
SpanAttributes.HTTP_TARGET: "/",
128127
SpanAttributes.NET_PEER_PORT: 65133,
129128
SpanAttributes.HTTP_FLAVOR: "1.1",
130129
"falcon.resource": "HelloWorldResource",
131130
SpanAttributes.HTTP_STATUS_CODE: 201,
132131
},
133132
)
134133
# In falcon<3, NET_PEER_IP is always set by default to 127.0.0.1
135-
# In falcon>3, NET_PEER_IP is not set to anything by default to
134+
# In falcon>=3, NET_PEER_IP is not set to anything by default
136135
# https://github.com/falconry/falcon/blob/5233d0abed977d9dab78ebadf305f5abe2eef07c/falcon/testing/helpers.py#L1168-L1172 # noqa
137136
if SpanAttributes.NET_PEER_IP in span.attributes:
138137
self.assertEqual(
139138
span.attributes[SpanAttributes.NET_PEER_IP], "127.0.0.1"
140139
)
140+
# In falcon<3.1.2, HTTP_TARGET is always set to / in TestClient
141+
# In falcon>=3.1.2, HTTP_TARGET is set to unencoded path by default
142+
# https://github.com/falconry/falcon/blob/69cdcd6edd2ee33f4ac9f7793e1cc3c4f99da692/falcon/testing/helpers.py#L1153-1156 # noqa
143+
if package_version.parse(_falcon_version) < package_version.parse("3.1.2"):
144+
self.assertEqual(
145+
span.attributes[SpanAttributes.HTTP_TARGET], "/"
146+
)
147+
else:
148+
self.assertEqual(
149+
span.attributes[SpanAttributes.HTTP_TARGET], "/hello"
150+
)
141151
self.memory_exporter.clear()
142152

143153
def test_404(self):
@@ -155,19 +165,29 @@ def test_404(self):
155165
SpanAttributes.HTTP_SCHEME: "http",
156166
SpanAttributes.NET_HOST_PORT: 80,
157167
SpanAttributes.HTTP_HOST: "falconframework.org",
158-
SpanAttributes.HTTP_TARGET: "/",
159168
SpanAttributes.NET_PEER_PORT: 65133,
160169
SpanAttributes.HTTP_FLAVOR: "1.1",
161170
SpanAttributes.HTTP_STATUS_CODE: 404,
162171
},
163172
)
164173
# In falcon<3, NET_PEER_IP is always set by default to 127.0.0.1
165-
# In falcon>3, NET_PEER_IP is not set to anything by default to
174+
# In falcon>=3.1.2, HTTP_TARGET is set to unencoded path by default
166175
# https://github.com/falconry/falcon/blob/5233d0abed977d9dab78ebadf305f5abe2eef07c/falcon/testing/helpers.py#L1168-L1172 # noqa
167176
if SpanAttributes.NET_PEER_IP in span.attributes:
168177
self.assertEqual(
169178
span.attributes[SpanAttributes.NET_PEER_IP], "127.0.0.1"
170179
)
180+
# In falcon<3.1.2, HTTP_TARGET is always set to / in TestClient
181+
# In falcon>=3.1.2, HTTP_TARGET is set to unencoded path by default
182+
# https://github.com/falconry/falcon/blob/69cdcd6edd2ee33f4ac9f7793e1cc3c4f99da692/falcon/testing/helpers.py#L1153-1156 # noqa
183+
if package_version.parse(_falcon_version) < package_version.parse("3.1.2"):
184+
self.assertEqual(
185+
span.attributes[SpanAttributes.HTTP_TARGET], "/"
186+
)
187+
else:
188+
self.assertEqual(
189+
span.attributes[SpanAttributes.HTTP_TARGET], "/does-not-exist"
190+
)
171191

172192
def test_500(self):
173193
try:
@@ -192,19 +212,29 @@ def test_500(self):
192212
SpanAttributes.HTTP_SCHEME: "http",
193213
SpanAttributes.NET_HOST_PORT: 80,
194214
SpanAttributes.HTTP_HOST: "falconframework.org",
195-
SpanAttributes.HTTP_TARGET: "/",
196215
SpanAttributes.NET_PEER_PORT: 65133,
197216
SpanAttributes.HTTP_FLAVOR: "1.1",
198217
SpanAttributes.HTTP_STATUS_CODE: 500,
199218
},
200219
)
201220
# In falcon<3, NET_PEER_IP is always set by default to 127.0.0.1
202-
# In falcon>3, NET_PEER_IP is not set to anything by default to
221+
# In falcon>=3, NET_PEER_IP is not set to anything by default
203222
# https://github.com/falconry/falcon/blob/5233d0abed977d9dab78ebadf305f5abe2eef07c/falcon/testing/helpers.py#L1168-L1172 # noqa
204223
if SpanAttributes.NET_PEER_IP in span.attributes:
205224
self.assertEqual(
206225
span.attributes[SpanAttributes.NET_PEER_IP], "127.0.0.1"
207226
)
227+
# In falcon<3.1.2, HTTP_TARGET is always set to / in TestClient
228+
# In falcon>=3.1.2, HTTP_TARGET is set to unencoded path by default
229+
# https://github.com/falconry/falcon/blob/69cdcd6edd2ee33f4ac9f7793e1cc3c4f99da692/falcon/testing/helpers.py#L1153-1156 # noqa
230+
if package_version.parse(_falcon_version) < package_version.parse("3.1.2"):
231+
self.assertEqual(
232+
span.attributes[SpanAttributes.HTTP_TARGET], "/"
233+
)
234+
else:
235+
self.assertEqual(
236+
span.attributes[SpanAttributes.HTTP_TARGET], "/error"
237+
)
208238

209239
def test_url_template(self):
210240
self.client().simulate_get("/user/123")
@@ -225,13 +255,23 @@ def test_url_template(self):
225255
SpanAttributes.HTTP_SCHEME: "http",
226256
SpanAttributes.NET_HOST_PORT: 80,
227257
SpanAttributes.HTTP_HOST: "falconframework.org",
228-
SpanAttributes.HTTP_TARGET: "/",
229258
SpanAttributes.NET_PEER_PORT: 65133,
230259
SpanAttributes.HTTP_FLAVOR: "1.1",
231260
"falcon.resource": "UserResource",
232261
SpanAttributes.HTTP_STATUS_CODE: 200,
233262
},
234263
)
264+
# In falcon<3.1.2, HTTP_TARGET is always set to / in TestClient
265+
# In falcon>=3.1.2, HTTP_TARGET is set to unencoded path by default
266+
# https://github.com/falconry/falcon/blob/69cdcd6edd2ee33f4ac9f7793e1cc3c4f99da692/falcon/testing/helpers.py#L1153-1156 # noqa
267+
if package_version.parse(_falcon_version) < package_version.parse("3.1.2"):
268+
self.assertEqual(
269+
span.attributes[SpanAttributes.HTTP_TARGET], "/"
270+
)
271+
else:
272+
self.assertEqual(
273+
span.attributes[SpanAttributes.HTTP_TARGET], "/user/123"
274+
)
235275

236276
def test_uninstrument(self):
237277
self.client().simulate_get(path="/hello")

0 commit comments

Comments
 (0)