Skip to content

Commit c605154

Browse files
committed
Fix some of the pending unit tests
1 parent 60222c2 commit c605154

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Added
2020
- `opentelemetry-instrumentation-urllib3` Add urllib3 instrumentation
2121
([#299](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/299))
22+
- `opentelemetry-instrumentation-django` Add ASGI support
23+
([#391](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/391))
2224

2325
## [0.19b0](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.19b0) - 2021-03-26
2426

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def get_host_port_url_tuple(scope):
113113
"""
114114
server = scope.get("server") or ["0.0.0.0", 80]
115115
port = server[1]
116-
server_host = server[0] + (":" + str(port) if port != 80 else "")
116+
server_host = server[0] + (":" + str(port) if str(port) != "80" else "")
117117
full_path = scope.get("root_path", "") + scope.get("path", "")
118118
http_url = scope.get("scheme", "http") + "://" + server_host + full_path
119119
return server_host, port, http_url

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
collect_request_attributes as asgi_collect_request_attributes,
5252
set_status_code,
5353
)
54+
5455
_is_asgi_supported = True
5556
except ImportError:
5657
asgi_getter = None
@@ -201,11 +202,15 @@ def process_response(self, request, response):
201202
and self._environ_span_key in request.META.keys()
202203
):
203204
if is_asgi_request:
204-
set_status_code(request.META[self._environ_span_key], response.status_code)
205+
set_status_code(
206+
request.META[self._environ_span_key], response.status_code
207+
)
205208
else:
206209
add_response_attributes(
207210
request.META[self._environ_span_key],
208-
"{} {}".format(response.status_code, response.reason_phrase),
211+
"{} {}".format(
212+
response.status_code, response.reason_phrase
213+
),
209214
response,
210215
)
211216

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

Lines changed: 14 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,7 @@ 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(reason="TODO: Traced request attributes not supported yet.")
266266
async def test_traced_request_attrs(self):
267267
await self.async_client.get("/span_name/1234/", CONTENT_TYPE="test/ct")
268268
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)