Skip to content

Commit 308d2fe

Browse files
committed
Fix some of the pending unit tests
1 parent b58640c commit 308d2fe

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
- `opentelemetry-instrumentation-botocore` now supports
2929
context propagation for lambda invoke via Payload embedded headers.
3030
([#458](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/458))
31+
- `opentelemetry-instrumentation-django` Add ASGI support
32+
([#391](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/391))
3133

3234
## [0.21b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.2.0-0.21b0) - 2021-05-11
3335

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def get_host_port_url_tuple(scope):
115115
"""Returns (host, port, full_url) tuple."""
116116
server = scope.get("server") or ["0.0.0.0", 80]
117117
port = server[1]
118-
server_host = server[0] + (":" + str(port) if port != 80 else "")
118+
server_host = server[0] + (":" + str(port) if str(port) != "80" else "")
119119
full_path = scope.get("root_path", "") + scope.get("path", "")
120120
http_url = scope.get("scheme", "http") + "://" + server_host + full_path
121121
return server_host, port, http_url

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
collect_request_attributes as asgi_collect_request_attributes,
5959
set_status_code,
6060
)
61+
6162
_is_asgi_supported = True
6263
except ImportError:
6364
asgi_getter = None
@@ -231,7 +232,7 @@ def process_response(self, request, response):
231232

232233
if activation and span:
233234
if is_asgi_request:
234-
set_status_code(request.META[self._environ_span_key], response.status_code)
235+
set_status_code(span, response.status_code)
235236
else:
236237
add_response_attributes(
237238
span,

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@
5656
_django_instrumentor = DjangoInstrumentor()
5757

5858

59-
@pytest.mark.skipif(not DJANGO_3_1, reason="AsyncClient implemented since Django 3.1")
59+
@pytest.mark.skipif(
60+
not DJANGO_3_1, reason="AsyncClient implemented since Django 3.1"
61+
)
6062
class TestMiddlewareAsgi(SimpleTestCase, TestBase):
61-
6263
@classmethod
6364
def setUpClass(cls):
6465
super().setUpClass()
@@ -110,7 +111,6 @@ def _remove_databases_failures(cls):
110111
# Disable databases.
111112
pass
112113

113-
@pytest.mark.skip(reason="TODO")
114114
async def test_templated_route_get(self):
115115
await self.async_client.get("/route/2020/template/")
116116

@@ -125,17 +125,17 @@ async def test_templated_route_get(self):
125125
self.assertEqual(span.attributes["http.method"], "GET")
126126
self.assertEqual(
127127
span.attributes["http.url"],
128-
"http://testserver/route/2020/template/",
128+
"http://127.0.0.1/route/2020/template/",
129129
)
130130
self.assertEqual(
131131
span.attributes["http.route"],
132132
"^route/(?P<year>[0-9]{4})/template/$",
133133
)
134134
self.assertEqual(span.attributes["http.scheme"], "http")
135135
self.assertEqual(span.attributes["http.status_code"], 200)
136-
self.assertEqual(span.attributes["http.status_text"], "OK")
136+
# TODO: Add http.status_text to ASGI instrumentation.
137+
# self.assertEqual(span.attributes["http.status_text"], "OK")
137138

138-
@pytest.mark.skip(reason="TODO")
139139
async def test_traced_get(self):
140140
await self.async_client.get("/traced/")
141141

@@ -149,12 +149,13 @@ async def test_traced_get(self):
149149
self.assertEqual(span.status.status_code, StatusCode.UNSET)
150150
self.assertEqual(span.attributes["http.method"], "GET")
151151
self.assertEqual(
152-
span.attributes["http.url"], "http://testserver/traced/"
152+
span.attributes["http.url"], "http://127.0.0.1/traced/"
153153
)
154154
self.assertEqual(span.attributes["http.route"], "^traced/")
155155
self.assertEqual(span.attributes["http.scheme"], "http")
156156
self.assertEqual(span.attributes["http.status_code"], 200)
157-
self.assertEqual(span.attributes["http.status_text"], "OK")
157+
# TODO: Add http.status_text to ASGI instrumentation.
158+
# self.assertEqual(span.attributes["http.status_text"], "OK")
158159

159160
async def test_not_recording(self):
160161
mock_tracer = Mock()
@@ -169,7 +170,6 @@ async def test_not_recording(self):
169170
self.assertFalse(mock_span.set_attribute.called)
170171
self.assertFalse(mock_span.set_status.called)
171172

172-
@pytest.mark.skip(reason="TODO")
173173
async def test_traced_post(self):
174174
await self.async_client.post("/traced/")
175175

@@ -183,14 +183,14 @@ async def test_traced_post(self):
183183
self.assertEqual(span.status.status_code, StatusCode.UNSET)
184184
self.assertEqual(span.attributes["http.method"], "POST")
185185
self.assertEqual(
186-
span.attributes["http.url"], "http://testserver/traced/"
186+
span.attributes["http.url"], "http://127.0.0.1/traced/"
187187
)
188188
self.assertEqual(span.attributes["http.route"], "^traced/")
189189
self.assertEqual(span.attributes["http.scheme"], "http")
190190
self.assertEqual(span.attributes["http.status_code"], 200)
191-
self.assertEqual(span.attributes["http.status_text"], "OK")
191+
# TODO: Add http.status_text to ASGI instrumentation.
192+
# self.assertEqual(span.attributes["http.status_text"], "OK")
192193

193-
@pytest.mark.skip(reason="TODO")
194194
async def test_error(self):
195195
with self.assertRaises(ValueError):
196196
await self.async_client.get("/error/")
@@ -205,7 +205,7 @@ async def test_error(self):
205205
self.assertEqual(span.status.status_code, StatusCode.ERROR)
206206
self.assertEqual(span.attributes["http.method"], "GET")
207207
self.assertEqual(
208-
span.attributes["http.url"], "http://testserver/error/"
208+
span.attributes["http.url"], "http://127.0.0.1/error/"
209209
)
210210
self.assertEqual(span.attributes["http.route"], "^error/")
211211
self.assertEqual(span.attributes["http.scheme"], "http")
@@ -262,7 +262,9 @@ async def test_span_name_404(self):
262262
span = span_list[0]
263263
self.assertEqual(span.name, "HTTP GET")
264264

265-
@pytest.mark.skip(reason="TODO")
265+
@pytest.mark.skip(
266+
reason="TODO: Traced request attributes not supported yet"
267+
)
266268
async def test_traced_request_attrs(self):
267269
await self.async_client.get("/span_name/1234/", CONTENT_TYPE="test/ct")
268270
span_list = self.memory_exporter.get_finished_spans()

instrumentation/opentelemetry-instrumentation-django/tests/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ async def async_traced(request): # pylint: disable=unused-argument
3535
return HttpResponse()
3636

3737

38-
async def async_traced_template(request, year): # pylint: disable=unused-argument
38+
async def async_traced_template(
39+
request, year
40+
): # pylint: disable=unused-argument
3941
return HttpResponse()
4042

4143

0 commit comments

Comments
 (0)