Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,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.metrics import MetricInstruments
from opentelemetry.semconv.metrics.http_metrics import (
HTTP_SERVER_REQUEST_DURATION,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.http_attributes import (
HTTP_TARGET,
HTTP_ROUTE
)
from opentelemetry.util._importlib_metadata import version
from opentelemetry.util.http import (
get_excluded_urls,
Expand Down Expand Up @@ -404,7 +406,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(
duration_attrs_old[HTTP_TARGET] = str(
request_route
)

Expand Down Expand Up @@ -449,7 +451,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,36 @@
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.http_attributes import (
HTTP_METHOD,
HTTP_SERVER_NAME,
HTTP_SCHEME,
HTTP_HOST,
HTTP_TARGET,
HTTP_FLAVOR,
HTTP_STATUS_CODE,
HTTP_REQUEST_METHOD,
HTTP_RESPONSE_STATUS_CODE,
HTTP_ROUTE,


)
from opentelemetry.semconv._incubating.attributes.net_attributes import (
NET_HOST_PORT,
NET_HOST_NAME,

)
from opentelemetry.semconv._incubating.attributes.server_attributes import (
SERVER_PORT,
SERVER_ADDRESS,
)
from opentelemetry.semconv._incubating.attributes.url_attributes import (
URL_PATH,
URL_SCHEME,
)
from opentelemetry.semconv._incubating.attributes.network_attributes import (
NETWORK_PROTOCOL_VERSION
)
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.util.http import (
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
Expand All @@ -57,15 +86,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
Expand All @@ -74,12 +103,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
Expand Down Expand Up @@ -220,8 +249,8 @@ def assert_environ():
def test_simple(self):
expected_attrs = expected_attributes(
{
SpanAttributes.HTTP_TARGET: "/hello/123",
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
HTTP_TARGET: "/hello/123",
HTTP_ROUTE: "/hello/<int:helloid>",
}
)
self.client.get("/hello/123")
Expand All @@ -235,8 +264,8 @@ def test_simple(self):
def test_simple_new_semconv(self):
expected_attrs = expected_attributes_new(
{
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
SpanAttributes.URL_SCHEME: "http",
HTTP_ROUTE: "/hello/<int:helloid>",
URL_SCHEME: "http",
}
)
self.client.get("/hello/123")
Expand All @@ -250,15 +279,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/<int:helloid>",
HTTP_TARGET: "/hello/123",
HTTP_ROUTE: "/hello/<int:helloid>",
}
)
expected_attrs.update(
expected_attributes_new(
{
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
SpanAttributes.URL_SCHEME: "http",
HTTP_ROUTE: "/hello/<int:helloid>",
URL_SCHEME: "http",
}
)
)
Expand Down Expand Up @@ -301,9 +330,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,
}
)

Expand All @@ -319,10 +348,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",
}
)

Expand All @@ -338,18 +367,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",
}
)
)
Expand All @@ -366,9 +395,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/<int:helloid>",
SpanAttributes.HTTP_STATUS_CODE: 500,
HTTP_TARGET: "/hello/500",
HTTP_ROUTE: "/hello/<int:helloid>",
HTTP_STATUS_CODE: 500,
}
)
resp = self.client.get("/hello/500")
Expand All @@ -383,11 +412,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/<int:helloid>",
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
URL_PATH: "/hello/500",
HTTP_ROUTE: "/hello/<int:helloid>",
HTTP_RESPONSE_STATUS_CODE: 500,
ERROR_TYPE: "500",
SpanAttributes.URL_SCHEME: "http",
URL_SCHEME: "http",
}
)
resp = self.client.get("/hello/500")
Expand All @@ -402,18 +431,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/<int:helloid>",
SpanAttributes.HTTP_STATUS_CODE: 500,
HTTP_TARGET: "/hello/500",
HTTP_ROUTE: "/hello/<int:helloid>",
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",
}
)
)
Expand Down
Loading