Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cae7e53

Browse files
committedSep 21, 2021
Rebase and apply @owais comments
1 parent d204f39 commit cae7e53

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed
 

‎CHANGELOG.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Added
1313
- `opentelemetry-instrumentation-elasticsearch` Added `response_hook` and `request_hook` callbacks
1414
([#670](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/670))
15+
- `opentelemetry-instrumentation-django` Add ASGI support
16+
([#391](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/391))
1517

1618
### Added
1719
- `opentelemetry-instrumentation-redis` added request_hook and response_hook callbacks passed as arguments to the instrument method.
@@ -33,10 +35,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3335
### Added
3436
- `opentelemetry-sdk-extension-aws` Add AWS resource detectors to extension package
3537
([#586](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/586))
36-
- `opentelemetry-instrumentation-asgi`, `opentelemetry-instrumentation-aiohttp-client`, `openetelemetry-instrumentation-fastapi`,
37-
`opentelemetry-instrumentation-starlette`, `opentelemetry-instrumentation-urllib`, `opentelemetry-instrumentation-urllib3` Added `request_hook` and `response_hook` callbacks
38+
- `opentelemetry-instrumentation-asgi`, `opentelemetry-instrumentation-aiohttp-client`, `openetelemetry-instrumentation-fastapi`,
39+
`opentelemetry-instrumentation-starlette`, `opentelemetry-instrumentation-urllib`, `opentelemetry-instrumentation-urllib3` Added `request_hook` and `response_hook` callbacks
3840
([#576](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/576))
39-
41+
4042
### Changed
4143

4244
- `opentelemetry-instrumentation-fastapi` Allow instrumentation of newer FastAPI versions.
@@ -100,8 +102,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
100102
### Added
101103
- `opentelemetry-instrumentation-httpx` Add `httpx` instrumentation
102104
([#461](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/461))
103-
- `opentelemetry-instrumentation-django` Add ASGI support
104-
([#391](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/391))
105105

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

‎instrumentation/opentelemetry-instrumentation-django/setup.cfg

+1-1
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

+13-21
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)
Please sign in to comment.