Skip to content

Commit 20d19a8

Browse files
committed
Rebase and apply @owais comments
1 parent e5a2949 commit 20d19a8

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
([#706](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/706))
2626
- `opentelemetry-instrumentation-requests` added exclude urls functionality
2727
([#714](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/714))
28+
- `opentelemetry-instrumentation-django` Add ASGI support
29+
([#391](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/391))
2830

2931
### Changed
3032
- `opentelemetry-instrumentation-botocore` Make common span attributes compliant with semantic conventions
@@ -57,12 +59,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5759
### Added
5860
- `opentelemetry-sdk-extension-aws` Add AWS resource detectors to extension package
5961
([#586](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/586))
60-
- `opentelemetry-instrumentation-asgi`, `opentelemetry-instrumentation-aiohttp-client`, `openetelemetry-instrumentation-fastapi`,
61-
`opentelemetry-instrumentation-starlette`, `opentelemetry-instrumentation-urllib`, `opentelemetry-instrumentation-urllib3` Added `request_hook` and `response_hook` callbacks
62+
- `opentelemetry-instrumentation-asgi`, `opentelemetry-instrumentation-aiohttp-client`, `openetelemetry-instrumentation-fastapi`,
63+
`opentelemetry-instrumentation-starlette`, `opentelemetry-instrumentation-urllib`, `opentelemetry-instrumentation-urllib3` Added `request_hook` and `response_hook` callbacks
6264
([#576](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/576))
6365
- `opentelemetry-instrumentation-pika` added RabbitMQ's pika module instrumentation.
6466
([#680](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/680))
65-
67+
6668
### Changed
6769

6870
- `opentelemetry-instrumentation-fastapi` Allow instrumentation of newer FastAPI versions.
@@ -126,8 +128,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
126128
### Added
127129
- `opentelemetry-instrumentation-httpx` Add `httpx` instrumentation
128130
([#461](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/461))
129-
- `opentelemetry-instrumentation-django` Add ASGI support
130-
([#391](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/391))
131131

132132
## [0.22b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.3.0-0.22b0) - 2021-06-01
133133

instrumentation/opentelemetry-instrumentation-django/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ install_requires =
4646

4747
[options.extras_require]
4848
asgi =
49-
opentelemetry-instrumentation-asgi == 0.23.dev0
49+
opentelemetry-instrumentation-asgi == 0.24b0
5050
test =
5151
opentelemetry-test == 0.24b0
5252

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __call__(self, request):
7474
else:
7575
ASGIRequest = None
7676

77+
# try/except block exclusive for optional ASGI imports.
7778
try:
7879
from opentelemetry.instrumentation.asgi import asgi_getter
7980
from opentelemetry.instrumentation.asgi import (
@@ -166,7 +167,7 @@ def process_request(self, request):
166167
return
167168

168169
is_asgi_request = _is_asgi_request(request)
169-
if is_asgi_request and not _is_asgi_supported:
170+
if not _is_asgi_supported and is_asgi_request:
170171
return
171172

172173
# pylint:disable=W0212
@@ -175,9 +176,11 @@ def process_request(self, request):
175176
request_meta = request.META
176177

177178
if is_asgi_request:
179+
carrier = request.scope
178180
carrier_getter = asgi_getter
179181
collect_request_attributes = asgi_collect_request_attributes
180182
else:
183+
carrier = request_meta
181184
carrier_getter = wsgi_getter
182185
collect_request_attributes = wsgi_collect_request_attributes
183186

@@ -191,36 +194,25 @@ def process_request(self, request):
191194
),
192195
)
193196

194-
if is_asgi_request:
195-
attributes = collect_request_attributes(request.scope)
196-
else:
197-
attributes = collect_request_attributes(request_meta)
197+
attributes = collect_request_attributes(carrier)
198198

199199
if span.is_recording():
200+
attributes = extract_attributes_from_object(
201+
request, self._traced_request_attrs, attributes
202+
)
200203
if is_asgi_request:
201-
# ASGI requests include extra attributes in request.scope.headers. For this reason,
202-
# we need to build an object with the union of `request` and `request.scope.headers`
203-
# contents, for the extract_attributes_from_object function to be able to retrieve
204-
# attributes from it.
204+
# ASGI requests include extra attributes in request.scope.headers.
205205
attributes = extract_attributes_from_object(
206206
types.SimpleNamespace(
207207
**{
208-
**request.__dict__,
209-
**{
210-
name.decode("latin1"): value.decode("latin1")
211-
for name, value in request.scope.get(
212-
"headers", []
213-
)
214-
},
208+
name.decode("latin1"): value.decode("latin1")
209+
for name, value in request.scope.get("headers", [])
215210
}
216211
),
217212
self._traced_request_attrs,
218213
attributes,
219214
)
220-
else:
221-
attributes = extract_attributes_from_object(
222-
request, self._traced_request_attrs, attributes
223-
)
215+
224216
for key, value in attributes.items():
225217
span.set_attribute(key, value)
226218

@@ -268,7 +260,7 @@ def process_response(self, request, response):
268260
return response
269261

270262
is_asgi_request = _is_asgi_request(request)
271-
if is_asgi_request and not _is_asgi_supported:
263+
if not _is_asgi_supported and is_asgi_request:
272264
return response
273265

274266
activation = request.META.pop(self._environ_activation_key, None)

0 commit comments

Comments
 (0)