From 0199d6196f9932dda16ac3dcf7f8616fb3a4d6b0 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 11 Jun 2024 18:23:15 +0200 Subject: [PATCH] add OTEL_ENABLE_SERVER_ATTRIBUTES_FOR_REQUEST_DURATION flag to add server.address and server.port for http.server.request.duration --- .../tests/test_programmatic.py | 14 +++++++++++++- .../opentelemetry/instrumentation/wsgi/__init__.py | 6 +++++- .../src/opentelemetry/instrumentation/_semconv.py | 11 +++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index d30a100b0e..55b9ec27a7 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py @@ -24,6 +24,7 @@ OTEL_SEMCONV_STABILITY_OPT_IN, _OpenTelemetrySemanticConventionStability, _server_active_requests_count_attrs_new, + _server_duration_attrs_new_with_server_attributes, _server_active_requests_count_attrs_old, _server_duration_attrs_new, _server_duration_attrs_old, @@ -99,7 +100,7 @@ def expected_attributes_new(override_attributes): } _recommended_metrics_attrs_new = { "http.server.active_requests": _server_active_requests_count_attrs_new, - "http.server.request.duration": _server_duration_attrs_new, + "http.server.request.duration": _server_duration_attrs_new_with_server_attributes, } _server_active_requests_count_attrs_both = ( _server_active_requests_count_attrs_old @@ -132,6 +133,7 @@ def setUp(self): "os.environ", { "OTEL_PYTHON_FLASK_EXCLUDED_URLS": "http://localhost/env_excluded_arg/123,env_excluded_noarg", + "OTEL_ENABLE_SERVER_ATTRIBUTES_FOR_REQUEST_DURATION": "true", OTEL_SEMCONV_STABILITY_OPT_IN: sem_conv_mode, }, ) @@ -519,6 +521,9 @@ def test_flask_metrics_new_semconv(self): histogram_data_point_seen = True if isinstance(point, NumberDataPoint): number_data_point_seen = True + for attr in _recommended_metrics_attrs_new[metric.name]: + if attr != _SPAN_ATTRIBUTES_ERROR_TYPE: + self.assertIn(attr, point.attributes) for attr in point.attributes: self.assertIn( attr, @@ -596,9 +601,12 @@ def test_basic_metric_success_new_semconv(self): self.client.get("/hello/756") expected_duration_attributes = { "http.request.method": "GET", + "http.route": "/hello/", "url.scheme": "http", "network.protocol.version": "1.1", "http.response.status_code": 200, + "server.address": "localhost", + "server.port": 80, } expected_requests_count_attributes = { "http.request.method": "GET", @@ -642,6 +650,8 @@ def test_basic_metric_nonstandard_http_method_success_new_semconv(self): "url.scheme": "http", "network.protocol.version": "1.1", "http.response.status_code": 405, + "server.address": "localhost", + "server.port": 80, } expected_requests_count_attributes = { "http.request.method": "_OTHER", @@ -667,6 +677,8 @@ def test_basic_metric_nonstandard_http_method_allowed_success_new_semconv( "url.scheme": "http", "network.protocol.version": "1.1", "http.response.status_code": 405, + "server.address": "localhost", + "server.port": 80, } expected_requests_count_attributes = { "http.request.method": "NONSTANDARD", diff --git a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py index 810a07e315..83250986e8 100644 --- a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py @@ -208,6 +208,7 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he """ import functools +import os import typing import wsgiref.util as wsgiref_util from timeit import default_timer @@ -225,6 +226,7 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he _report_new, _report_old, _server_active_requests_count_attrs_new, + _server_duration_attrs_new_with_server_attributes, _server_active_requests_count_attrs_old, _server_duration_attrs_new, _server_duration_attrs_old, @@ -463,7 +465,9 @@ def _parse_duration_attrs( return _filter_semconv_duration_attrs( req_attrs, _server_duration_attrs_old, - _server_duration_attrs_new, + _server_duration_attrs_new_with_server_attributes + if os.environ.get("OTEL_ENABLE_SERVER_ATTRIBUTES_FOR_REQUEST_DURATION") + else _server_duration_attrs_new, sem_conv_opt_in_mode, ) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py index efe3c75f70..cc7d7e9665 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py @@ -68,6 +68,17 @@ SpanAttributes.URL_SCHEME, ] +_server_duration_attrs_new_with_server_attributes = [ + _SPAN_ATTRIBUTES_ERROR_TYPE, + SpanAttributes.HTTP_REQUEST_METHOD, + SpanAttributes.HTTP_RESPONSE_STATUS_CODE, + SpanAttributes.HTTP_ROUTE, + SpanAttributes.NETWORK_PROTOCOL_VERSION, + SpanAttributes.URL_SCHEME, + SpanAttributes.SERVER_ADDRESS, + SpanAttributes.SERVER_PORT, +] + _server_active_requests_count_attrs_old = [ SpanAttributes.HTTP_METHOD, SpanAttributes.HTTP_HOST,