Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP semantic convention stability migration for django #2714

Merged
merged 46 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
27e4c2c
Update .pylintrc
lzchen Apr 3, 2024
c6b4c05
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 5, 2024
f690fc8
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 9, 2024
008c36a
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 16, 2024
b36c916
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 22, 2024
2a30d60
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 24, 2024
4cd5349
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen May 13, 2024
5a7b9ad
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen May 22, 2024
b721434
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen May 23, 2024
6ce38bd
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen May 28, 2024
df3275c
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen May 31, 2024
cffc12f
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jun 4, 2024
df80e8a
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jun 5, 2024
71df253
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jun 7, 2024
4c6d52a
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jun 12, 2024
0730c81
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jun 14, 2024
91b16eb
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jun 17, 2024
2b6f107
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 3, 2024
d3aee65
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 8, 2024
b4f600d
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 11, 2024
1796b7c
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 15, 2024
c9731e9
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 15, 2024
9be1808
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 16, 2024
45a8808
django
lzchen Jul 16, 2024
bfa3292
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 16, 2024
cd714f9
wsgi
lzchen Jul 18, 2024
240e994
chagelog
lzchen Jul 18, 2024
a454db0
asgi
lzchen Jul 18, 2024
b94396d
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 18, 2024
80e1ce3
lint
lzchen Jul 18, 2024
e76d724
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Jul 18, 2024
83964d9
lint
lzchen Jul 18, 2024
1ba98d5
Update CHANGELOG.md
lzchen Jul 19, 2024
6b9acaf
Update CHANGELOG.md
lzchen Jul 19, 2024
c719502
Update CHANGELOG.md
lzchen Jul 19, 2024
ae17c7c
lint
lzchen Jul 19, 2024
31fe38f
Update test_middleware.py
lzchen Jul 19, 2024
1a38232
lint
lzchen Jul 22, 2024
9adb588
lint
lzchen Jul 22, 2024
985b28e
Merge branch 'main' into django
lzchen Jul 22, 2024
3af7e5d
fixes
lzchen Jul 22, 2024
4622d74
Merge branch 'django' of https://github.com/lzchen/opentelemetry-pyth…
lzchen Jul 22, 2024
cf1ec9e
test
lzchen Jul 22, 2024
9f0ae9a
Update test_wsgi_middleware.py
lzchen Jul 22, 2024
fef7677
tests
lzchen Jul 22, 2024
c4c9d31
Update test_middleware.py
lzchen Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lint
  • Loading branch information
lzchen committed Jul 18, 2024
commit 80e1ce349769524cdfcb7a18a92318f51a5ce2ab
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ def _instrument(self, **kwargs):
description="Duration of HTTP server requests.",
unit="s",
)
_DjangoMiddleware._active_request_counter = create_http_server_active_requests(
meter
_DjangoMiddleware._active_request_counter = (
create_http_server_active_requests(meter)
)
# This can not be solved, but is an inherent problem of this approach:
# the order of middleware entries matters, and here you have no control
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ class _DjangoMiddleware(MiddlewareMixin):
_sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT

_otel_request_hook: Callable[[Span, HttpRequest], None] = None
_otel_response_hook: Callable[[Span, HttpRequest, HttpResponse], None] = (
None
)
_otel_response_hook: Callable[
[Span, HttpRequest, HttpResponse], None
] = None

@staticmethod
def _get_span_name(request):
Expand Down Expand Up @@ -245,9 +245,9 @@ def process_request(self, request):
self._sem_conv_opt_in_mode,
)

request.META[self._environ_active_request_attr_key] = (
active_requests_count_attrs
)
request.META[
self._environ_active_request_attr_key
] = active_requests_count_attrs
# Pass all of attributes to duration key because we will filter during response
request.META[self._environ_duration_attr_key] = attributes
self._active_request_counter.add(1, active_requests_count_attrs)
Expand Down Expand Up @@ -459,6 +459,7 @@ def process_response(self, request, response):

return response


def _parse_duration_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ def test_traced_get_new_semconv(self):
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
if DJANGO_2_2:
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

def test_traced_get_both_semconv(self):
Expand Down Expand Up @@ -286,9 +284,7 @@ def test_traced_get_both_semconv(self):
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
if DJANGO_2_2:
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

def test_not_recording(self):
Expand Down Expand Up @@ -354,9 +350,7 @@ def test_traced_post_new_semconv(self):
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
if DJANGO_2_2:
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

def test_traced_post_both_semconv(self):
Expand All @@ -383,9 +377,7 @@ def test_traced_post_both_semconv(self):
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
if DJANGO_2_2:
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

def test_error(self):
Expand Down Expand Up @@ -436,21 +428,15 @@ def test_error_new_semconv(self):
self.assertEqual(span.status.status_code, StatusCode.ERROR)
self.assertEqual(span.attributes[HTTP_REQUEST_METHOD], "GET")
if DJANGO_2_2:
self.assertEqual(
span.attributes[HTTP_ROUTE], "^error/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^error/")
self.assertEqual(span.attributes[URL_SCHEME], "http")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 500)

self.assertEqual(len(span.events), 1)
event = span.events[0]
self.assertEqual(event.name, "exception")
self.assertEqual(
event.attributes[EXCEPTION_TYPE], "ValueError"
)
self.assertEqual(
event.attributes[EXCEPTION_MESSAGE], "error"
)
self.assertEqual(event.attributes[EXCEPTION_TYPE], "ValueError")
self.assertEqual(event.attributes[EXCEPTION_MESSAGE], "error")

def test_error_both_semconv(self):
with self.assertRaises(ValueError):
Expand All @@ -477,21 +463,15 @@ def test_error_both_semconv(self):
self.assertEqual(span.attributes[SpanAttributes.HTTP_STATUS_CODE], 500)
self.assertEqual(span.attributes[HTTP_REQUEST_METHOD], "GET")
if DJANGO_2_2:
self.assertEqual(
span.attributes[HTTP_ROUTE], "^error/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^error/")
self.assertEqual(span.attributes[URL_SCHEME], "http")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 500)

self.assertEqual(len(span.events), 1)
event = span.events[0]
self.assertEqual(event.name, "exception")
self.assertEqual(
event.attributes[EXCEPTION_TYPE], "ValueError"
)
self.assertEqual(
event.attributes[EXCEPTION_MESSAGE], "error"
)
self.assertEqual(event.attributes[EXCEPTION_TYPE], "ValueError")
self.assertEqual(event.attributes[EXCEPTION_MESSAGE], "error")

def test_exclude_lists(self):
client = Client()
Expand Down Expand Up @@ -558,7 +538,9 @@ def test_span_name_404(self):
self.assertEqual(span.name, "GET")

def test_nonstandard_http_method_span_name(self):
Client().request(REQUEST_METHOD="NONSTANDARD", PATH_INFO="/span_name/1234/")
Client().request(
REQUEST_METHOD="NONSTANDARD", PATH_INFO="/span_name/1234/"
)
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ async def test_traced_get_new_semconv(self):
self.assertEqual(span.attributes[SERVER_PORT], 80)
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

async def test_traced_get_both_semconv(self):
Expand All @@ -304,9 +302,7 @@ async def test_traced_get_both_semconv(self):
self.assertEqual(span.attributes[SERVER_PORT], 80)
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

async def test_not_recording(self):
Expand Down Expand Up @@ -360,10 +356,8 @@ async def test_traced_post_new_semconv(self):
self.assertEqual(span.attributes[SERVER_PORT], 80)
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

async def test_traced_post_both_semconv(self):
await self.async_client.post("/traced/")
Expand All @@ -388,10 +382,8 @@ async def test_traced_post_both_semconv(self):
self.assertEqual(span.attributes[SERVER_PORT], 80)
self.assertEqual(span.attributes[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(span.attributes[NETWORK_PROTOCOL_VERSION], "1.1")
self.assertEqual(
span.attributes[HTTP_ROUTE], "^traced/"
)
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)
self.assertEqual(span.attributes[HTTP_ROUTE], "^traced/")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 200)

async def test_error(self):
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -423,7 +415,7 @@ async def test_error(self):
self.assertEqual(
event.attributes[SpanAttributes.EXCEPTION_MESSAGE], "error"
)

async def test_error_new_semconv(self):
with self.assertRaises(ValueError):
await self.async_client.get("/error/")
Expand All @@ -437,21 +429,15 @@ async def test_error_new_semconv(self):
self.assertEqual(span.kind, SpanKind.SERVER)
self.assertEqual(span.status.status_code, StatusCode.ERROR)
self.assertEqual(span.attributes[HTTP_REQUEST_METHOD], "GET")
self.assertEqual(
span.attributes[HTTP_ROUTE], "^error/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^error/")
self.assertEqual(span.attributes[URL_SCHEME], "http")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 500)

self.assertEqual(len(span.events), 1)
event = span.events[0]
self.assertEqual(event.name, "exception")
self.assertEqual(
event.attributes[EXCEPTION_TYPE], "ValueError"
)
self.assertEqual(
event.attributes[EXCEPTION_MESSAGE], "error"
)
self.assertEqual(event.attributes[EXCEPTION_TYPE], "ValueError")
self.assertEqual(event.attributes[EXCEPTION_MESSAGE], "error")

async def test_error_both_semconv(self):
with self.assertRaises(ValueError):
Expand All @@ -470,27 +456,19 @@ async def test_error_both_semconv(self):
span.attributes[SpanAttributes.HTTP_URL],
"http://127.0.0.1/error/",
)
self.assertEqual(
span.attributes[SpanAttributes.HTTP_ROUTE], "^error/"
)
self.assertEqual(span.attributes[SpanAttributes.HTTP_ROUTE], "^error/")
self.assertEqual(span.attributes[SpanAttributes.HTTP_SCHEME], "http")
self.assertEqual(span.attributes[SpanAttributes.HTTP_STATUS_CODE], 500)
self.assertEqual(span.attributes[HTTP_REQUEST_METHOD], "GET")
self.assertEqual(
span.attributes[HTTP_ROUTE], "^error/"
)
self.assertEqual(span.attributes[HTTP_ROUTE], "^error/")
self.assertEqual(span.attributes[URL_SCHEME], "http")
self.assertEqual(span.attributes[HTTP_RESPONSE_STATUS_CODE], 500)

self.assertEqual(len(span.events), 1)
event = span.events[0]
self.assertEqual(event.name, "exception")
self.assertEqual(
event.attributes[EXCEPTION_TYPE], "ValueError"
)
self.assertEqual(
event.attributes[EXCEPTION_MESSAGE], "error"
)
self.assertEqual(event.attributes[EXCEPTION_TYPE], "ValueError")
self.assertEqual(event.attributes[EXCEPTION_MESSAGE], "error")

async def test_exclude_lists(self):
await self.async_client.get("/excluded_arg/123")
Expand Down Expand Up @@ -538,7 +516,9 @@ async def test_span_name_404(self):
self.assertEqual(span.name, "GET")

async def test_nonstandard_http_method_span_name(self):
await self.async_client.request(method="NONSTANDARD", path="/span_name/1234/")
await self.async_client.request(
method="NONSTANDARD", path="/span_name/1234/"
)
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def response_with_custom_header(request):
response = HttpResponse()
response["custom-test-header-1"] = "test-header-value-1"
response["custom-test-header-2"] = "test-header-value-2"
response["my-custom-regex-header-1"] = (
"my-custom-regex-value-1,my-custom-regex-value-2"
)
response["my-custom-regex-header-2"] = (
"my-custom-regex-value-3,my-custom-regex-value-4"
)
response[
"my-custom-regex-header-1"
] = "my-custom-regex-value-1,my-custom-regex-value-2"
response[
"my-custom-regex-header-2"
] = "my-custom-regex-value-3,my-custom-regex-value-4"
response["my-secret-header"] = "my-secret-value"
return response

Expand Down
Loading