diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index 5744e5cc96..9a22383e1b 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -270,12 +270,14 @@ def response_hook(span: Span, status: str, response_headers: List): ) from opentelemetry.instrumentation.utils import _start_internal_or_server_span from opentelemetry.metrics import get_meter -from opentelemetry.semconv.attributes.http_attributes import HTTP_ROUTE +from opentelemetry.semconv._incubating.attributes.http_attributes import ( + HTTP_ROUTE, + HTTP_TARGET, +) from opentelemetry.semconv.metrics import MetricInstruments from opentelemetry.semconv.metrics.http_metrics import ( HTTP_SERVER_REQUEST_DURATION, ) -from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.util._importlib_metadata import version from opentelemetry.util.http import ( get_excluded_urls, @@ -401,9 +403,7 @@ def _start_response(status, response_headers, *args, **kwargs): if request_route: # http.target to be included in old semantic conventions - duration_attrs_old[SpanAttributes.HTTP_TARGET] = str( - request_route - ) + duration_attrs_old[HTTP_TARGET] = str(request_route) duration_histogram_old.record( max(round(duration_s * 1000), 0), duration_attrs_old @@ -446,7 +446,7 @@ def _before_request(): if flask.request.url_rule: # For 404 that result from no route found, etc, we # don't have a url_rule. - attributes[SpanAttributes.HTTP_ROUTE] = flask.request.url_rule.rule + attributes[HTTP_ROUTE] = flask.request.url_rule.rule span, token = _start_internal_or_server_span( tracer=tracer, span_name=span_name, diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index afcf750e0e..60f08c7cd2 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py @@ -40,8 +40,34 @@ NumberDataPoint, ) from opentelemetry.sdk.resources import Resource +from opentelemetry.semconv._incubating.attributes.http_attributes import ( + HTTP_FLAVOR, + HTTP_HOST, + HTTP_METHOD, + HTTP_REQUEST_METHOD, + HTTP_RESPONSE_STATUS_CODE, + HTTP_ROUTE, + HTTP_SCHEME, + HTTP_SERVER_NAME, + HTTP_STATUS_CODE, + HTTP_TARGET, +) +from opentelemetry.semconv._incubating.attributes.net_attributes import ( + NET_HOST_NAME, + NET_HOST_PORT, +) +from opentelemetry.semconv._incubating.attributes.network_attributes import ( + NETWORK_PROTOCOL_VERSION, +) +from opentelemetry.semconv._incubating.attributes.server_attributes import ( + SERVER_ADDRESS, + SERVER_PORT, +) +from opentelemetry.semconv._incubating.attributes.url_attributes import ( + URL_PATH, + URL_SCHEME, +) from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE -from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.wsgitestutil import WsgiTestBase from opentelemetry.util.http import ( OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS, @@ -57,15 +83,15 @@ def expected_attributes(override_attributes): default_attributes = { - SpanAttributes.HTTP_METHOD: "GET", - SpanAttributes.HTTP_SERVER_NAME: "localhost", - SpanAttributes.HTTP_SCHEME: "http", - SpanAttributes.NET_HOST_PORT: 80, - SpanAttributes.NET_HOST_NAME: "localhost", - SpanAttributes.HTTP_HOST: "localhost", - SpanAttributes.HTTP_TARGET: "/", - SpanAttributes.HTTP_FLAVOR: "1.1", - SpanAttributes.HTTP_STATUS_CODE: 200, + HTTP_METHOD: "GET", + HTTP_SERVER_NAME: "localhost", + HTTP_SCHEME: "http", + NET_HOST_PORT: 80, + NET_HOST_NAME: "localhost", + HTTP_HOST: "localhost", + HTTP_TARGET: "/", + HTTP_FLAVOR: "1.1", + HTTP_STATUS_CODE: 200, } for key, val in override_attributes.items(): default_attributes[key] = val @@ -74,12 +100,12 @@ def expected_attributes(override_attributes): def expected_attributes_new(override_attributes): default_attributes = { - SpanAttributes.HTTP_REQUEST_METHOD: "GET", - SpanAttributes.SERVER_PORT: 80, - SpanAttributes.SERVER_ADDRESS: "localhost", - SpanAttributes.URL_PATH: "/hello/123", - SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1", - SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200, + HTTP_REQUEST_METHOD: "GET", + SERVER_PORT: 80, + SERVER_ADDRESS: "localhost", + URL_PATH: "/hello/123", + NETWORK_PROTOCOL_VERSION: "1.1", + HTTP_RESPONSE_STATUS_CODE: 200, } for key, val in override_attributes.items(): default_attributes[key] = val @@ -220,8 +246,8 @@ def assert_environ(): def test_simple(self): expected_attrs = expected_attributes( { - SpanAttributes.HTTP_TARGET: "/hello/123", - SpanAttributes.HTTP_ROUTE: "/hello/", + HTTP_TARGET: "/hello/123", + HTTP_ROUTE: "/hello/", } ) self.client.get("/hello/123") @@ -235,8 +261,8 @@ def test_simple(self): def test_simple_new_semconv(self): expected_attrs = expected_attributes_new( { - SpanAttributes.HTTP_ROUTE: "/hello/", - SpanAttributes.URL_SCHEME: "http", + HTTP_ROUTE: "/hello/", + URL_SCHEME: "http", } ) self.client.get("/hello/123") @@ -250,15 +276,15 @@ def test_simple_new_semconv(self): def test_simple_both_semconv(self): expected_attrs = expected_attributes( { - SpanAttributes.HTTP_TARGET: "/hello/123", - SpanAttributes.HTTP_ROUTE: "/hello/", + HTTP_TARGET: "/hello/123", + HTTP_ROUTE: "/hello/", } ) expected_attrs.update( expected_attributes_new( { - SpanAttributes.HTTP_ROUTE: "/hello/", - SpanAttributes.URL_SCHEME: "http", + HTTP_ROUTE: "/hello/", + URL_SCHEME: "http", } ) ) @@ -301,9 +327,9 @@ def test_not_recording(self): def test_404(self): expected_attrs = expected_attributes( { - SpanAttributes.HTTP_METHOD: "POST", - SpanAttributes.HTTP_TARGET: "/bye", - SpanAttributes.HTTP_STATUS_CODE: 404, + HTTP_METHOD: "POST", + HTTP_TARGET: "/bye", + HTTP_STATUS_CODE: 404, } ) @@ -319,10 +345,10 @@ def test_404(self): def test_404_new_semconv(self): expected_attrs = expected_attributes_new( { - SpanAttributes.HTTP_REQUEST_METHOD: "POST", - SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 404, - SpanAttributes.URL_PATH: "/bye", - SpanAttributes.URL_SCHEME: "http", + HTTP_REQUEST_METHOD: "POST", + HTTP_RESPONSE_STATUS_CODE: 404, + URL_PATH: "/bye", + URL_SCHEME: "http", } ) @@ -338,18 +364,18 @@ def test_404_new_semconv(self): def test_404_both_semconv(self): expected_attrs = expected_attributes( { - SpanAttributes.HTTP_METHOD: "POST", - SpanAttributes.HTTP_TARGET: "/bye", - SpanAttributes.HTTP_STATUS_CODE: 404, + HTTP_METHOD: "POST", + HTTP_TARGET: "/bye", + HTTP_STATUS_CODE: 404, } ) expected_attrs.update( expected_attributes_new( { - SpanAttributes.HTTP_REQUEST_METHOD: "POST", - SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 404, - SpanAttributes.URL_PATH: "/bye", - SpanAttributes.URL_SCHEME: "http", + HTTP_REQUEST_METHOD: "POST", + HTTP_RESPONSE_STATUS_CODE: 404, + URL_PATH: "/bye", + URL_SCHEME: "http", } ) ) @@ -366,9 +392,9 @@ def test_404_both_semconv(self): def test_internal_error(self): expected_attrs = expected_attributes( { - SpanAttributes.HTTP_TARGET: "/hello/500", - SpanAttributes.HTTP_ROUTE: "/hello/", - SpanAttributes.HTTP_STATUS_CODE: 500, + HTTP_TARGET: "/hello/500", + HTTP_ROUTE: "/hello/", + HTTP_STATUS_CODE: 500, } ) resp = self.client.get("/hello/500") @@ -383,11 +409,11 @@ def test_internal_error(self): def test_internal_error_new_semconv(self): expected_attrs = expected_attributes_new( { - SpanAttributes.URL_PATH: "/hello/500", - SpanAttributes.HTTP_ROUTE: "/hello/", - SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500, + URL_PATH: "/hello/500", + HTTP_ROUTE: "/hello/", + HTTP_RESPONSE_STATUS_CODE: 500, ERROR_TYPE: "500", - SpanAttributes.URL_SCHEME: "http", + URL_SCHEME: "http", } ) resp = self.client.get("/hello/500") @@ -402,18 +428,18 @@ def test_internal_error_new_semconv(self): def test_internal_error_both_semconv(self): expected_attrs = expected_attributes( { - SpanAttributes.HTTP_TARGET: "/hello/500", - SpanAttributes.HTTP_ROUTE: "/hello/", - SpanAttributes.HTTP_STATUS_CODE: 500, + HTTP_TARGET: "/hello/500", + HTTP_ROUTE: "/hello/", + HTTP_STATUS_CODE: 500, } ) expected_attrs.update( expected_attributes_new( { - SpanAttributes.URL_PATH: "/hello/500", - SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500, + URL_PATH: "/hello/500", + HTTP_RESPONSE_STATUS_CODE: 500, ERROR_TYPE: "500", - SpanAttributes.URL_SCHEME: "http", + URL_SCHEME: "http", } ) )