Skip to content

Commit

Permalink
Use generated symbols from semantic conventions package (#2611)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Jun 17, 2024
1 parent daa84a6 commit b94c590
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 161 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2572](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2572))
- `opentelemetry-instrumentation-aiohttp-server`, `opentelemetry-instrumentation-httpx` Ensure consistently use of suppress_instrumentation utils
([#2590](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2590))
- Reference symbols from generated semantic conventions
([#2611](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2611))

## Version 1.25.0/0.46b0 (2024-05-31)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ def response_hook(span: Span, status: str, response_headers: List):
import opentelemetry.instrumentation.wsgi as otel_wsgi
from opentelemetry import context, trace
from opentelemetry.instrumentation._semconv import (
_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
Expand All @@ -268,6 +267,9 @@ 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.metrics import MetricInstruments
from opentelemetry.semconv.metrics.http_metrics import (
HTTP_SERVER_REQUEST_DURATION,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.util.http import (
get_excluded_urls,
Expand Down Expand Up @@ -553,7 +555,7 @@ def __init__(self, *args, **kwargs):
duration_histogram_new = None
if _report_new(_InstrumentedFlask._sem_conv_opt_in_mode):
duration_histogram_new = meter.create_histogram(
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
name=HTTP_SERVER_REQUEST_DURATION,
unit="s",
description="measures the duration of the inbound HTTP request",
)
Expand Down Expand Up @@ -684,7 +686,7 @@ def instrument_app(
duration_histogram_new = None
if _report_new(sem_conv_opt_in_mode):
duration_histogram_new = meter.create_histogram(
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
name=HTTP_SERVER_REQUEST_DURATION,
unit="s",
description="measures the duration of the inbound HTTP request",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
_SPAN_ATTRIBUTES_ERROR_TYPE,
OTEL_SEMCONV_STABILITY_OPT_IN,
_OpenTelemetrySemanticConventionStability,
_server_active_requests_count_attrs_new,
Expand All @@ -40,6 +39,7 @@
NumberDataPoint,
)
from opentelemetry.sdk.resources import Resource
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 (
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_internal_error_new_semconv(self):
SpanAttributes.URL_PATH: "/hello/500",
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
_SPAN_ATTRIBUTES_ERROR_TYPE: "500",
ERROR_TYPE: "500",
SpanAttributes.URL_SCHEME: "http",
}
)
Expand All @@ -405,7 +405,7 @@ def test_internal_error_both_semconv(self):
{
SpanAttributes.URL_PATH: "/hello/500",
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
_SPAN_ATTRIBUTES_ERROR_TYPE: "500",
ERROR_TYPE: "500",
SpanAttributes.URL_SCHEME: "http",
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@
from requests.structures import CaseInsensitiveDict

from opentelemetry.instrumentation._semconv import (
_METRIC_ATTRIBUTES_CLIENT_DURATION_NAME,
_SPAN_ATTRIBUTES_ERROR_TYPE,
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS,
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT,
_client_duration_attrs_new,
_client_duration_attrs_old,
_filter_semconv_duration_attrs,
Expand Down Expand Up @@ -91,7 +87,15 @@
)
from opentelemetry.metrics import Histogram, get_meter
from opentelemetry.propagate import inject
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
from opentelemetry.semconv.attributes.network_attributes import (
NETWORK_PEER_ADDRESS,
NETWORK_PEER_PORT,
)
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.metrics.http_metrics import (
HTTP_CLIENT_REQUEST_DURATION,
)
from opentelemetry.trace import SpanKind, Tracer, get_tracer
from opentelemetry.trace.span import Span
from opentelemetry.trace.status import StatusCode
Expand Down Expand Up @@ -191,9 +195,7 @@ def get_or_create_headers():
sem_conv_opt_in_mode,
)
# Use semconv library when available
span_attributes[_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS] = (
parsed_url.hostname
)
span_attributes[NETWORK_PEER_ADDRESS] = parsed_url.hostname
if parsed_url.port:
_set_http_peer_port_client(
metric_labels, parsed_url.port, sem_conv_opt_in_mode
Expand All @@ -203,9 +205,7 @@ def get_or_create_headers():
span_attributes, parsed_url.port, sem_conv_opt_in_mode
)
# Use semconv library when available
span_attributes[_SPAN_ATTRIBUTES_NETWORK_PEER_PORT] = (
parsed_url.port
)
span_attributes[NETWORK_PEER_PORT] = parsed_url.port
except ValueError:
pass

Expand Down Expand Up @@ -250,12 +250,8 @@ def get_or_create_headers():
_report_new(sem_conv_opt_in_mode)
and status_code is StatusCode.ERROR
):
span_attributes[_SPAN_ATTRIBUTES_ERROR_TYPE] = str(
result.status_code
)
metric_labels[_SPAN_ATTRIBUTES_ERROR_TYPE] = str(
result.status_code
)
span_attributes[ERROR_TYPE] = str(result.status_code)
metric_labels[ERROR_TYPE] = str(result.status_code)

if result.raw is not None:
version = getattr(result.raw, "version", None)
Expand All @@ -278,12 +274,8 @@ def get_or_create_headers():
response_hook(span, request, result)

if exception is not None and _report_new(sem_conv_opt_in_mode):
span.set_attribute(
_SPAN_ATTRIBUTES_ERROR_TYPE, type(exception).__qualname__
)
metric_labels[_SPAN_ATTRIBUTES_ERROR_TYPE] = type(
exception
).__qualname__
span.set_attribute(ERROR_TYPE, type(exception).__qualname__)
metric_labels[ERROR_TYPE] = type(exception).__qualname__

if duration_histogram_old is not None:
duration_attrs_old = _filter_semconv_duration_attrs(
Expand Down Expand Up @@ -403,7 +395,7 @@ def _instrument(self, **kwargs):
duration_histogram_new = None
if _report_new(semconv_opt_in_mode):
duration_histogram_new = meter.create_histogram(
name=_METRIC_ATTRIBUTES_CLIENT_DURATION_NAME,
name=HTTP_CLIENT_REQUEST_DURATION,
unit="s",
description="Duration of HTTP client requests.",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import opentelemetry.instrumentation.requests
from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
_SPAN_ATTRIBUTES_ERROR_TYPE,
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS,
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT,
OTEL_SEMCONV_STABILITY_OPT_IN,
_OpenTelemetrySemanticConventionStability,
)
Expand All @@ -36,6 +33,21 @@
)
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.sdk import resources
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
from opentelemetry.semconv.attributes.http_attributes import (
HTTP_REQUEST_METHOD,
HTTP_RESPONSE_STATUS_CODE,
)
from opentelemetry.semconv.attributes.network_attributes import (
NETWORK_PEER_ADDRESS,
NETWORK_PEER_PORT,
NETWORK_PROTOCOL_VERSION,
)
from opentelemetry.semconv.attributes.server_attributes import (
SERVER_ADDRESS,
SERVER_PORT,
)
from opentelemetry.semconv.attributes.url_attributes import URL_FULL
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.mock_textmap import MockTextMapPropagator
from opentelemetry.test.test_base import TestBase
Expand Down Expand Up @@ -176,14 +188,14 @@ def test_basic_new_semconv(self):
self.assertEqual(
span.attributes,
{
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
SpanAttributes.URL_FULL: url_with_port,
SpanAttributes.SERVER_ADDRESS: "mock",
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS: "mock",
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
SpanAttributes.SERVER_PORT: 80,
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT: 80,
HTTP_REQUEST_METHOD: "GET",
URL_FULL: url_with_port,
SERVER_ADDRESS: "mock",
NETWORK_PEER_ADDRESS: "mock",
HTTP_RESPONSE_STATUS_CODE: 200,
NETWORK_PROTOCOL_VERSION: "1.1",
SERVER_PORT: 80,
NETWORK_PEER_PORT: 80,
},
)

Expand Down Expand Up @@ -213,19 +225,19 @@ def test_basic_both_semconv(self):
span.attributes,
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
HTTP_REQUEST_METHOD: "GET",
SpanAttributes.HTTP_URL: url_with_port,
SpanAttributes.URL_FULL: url_with_port,
URL_FULL: url_with_port,
SpanAttributes.HTTP_HOST: "mock",
SpanAttributes.SERVER_ADDRESS: "mock",
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS: "mock",
SERVER_ADDRESS: "mock",
NETWORK_PEER_ADDRESS: "mock",
SpanAttributes.NET_PEER_PORT: 80,
SpanAttributes.HTTP_STATUS_CODE: 200,
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
HTTP_RESPONSE_STATUS_CODE: 200,
SpanAttributes.HTTP_FLAVOR: "1.1",
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
SpanAttributes.SERVER_PORT: 80,
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT: 80,
NETWORK_PROTOCOL_VERSION: "1.1",
SERVER_PORT: 80,
NETWORK_PEER_PORT: 80,
},
)

Expand Down Expand Up @@ -328,12 +340,8 @@ def test_not_foundbasic_new_semconv(self):

span = self.assert_span()

self.assertEqual(
span.attributes.get(SpanAttributes.HTTP_RESPONSE_STATUS_CODE), 404
)
self.assertEqual(
span.attributes.get(_SPAN_ATTRIBUTES_ERROR_TYPE), "404"
)
self.assertEqual(span.attributes.get(HTTP_RESPONSE_STATUS_CODE), 404)
self.assertEqual(span.attributes.get(ERROR_TYPE), "404")

self.assertIs(
span.status.status_code,
Expand All @@ -355,12 +363,8 @@ def test_not_foundbasic_both_semconv(self):
self.assertEqual(
span.attributes.get(SpanAttributes.HTTP_STATUS_CODE), 404
)
self.assertEqual(
span.attributes.get(SpanAttributes.HTTP_RESPONSE_STATUS_CODE), 404
)
self.assertEqual(
span.attributes.get(_SPAN_ATTRIBUTES_ERROR_TYPE), "404"
)
self.assertEqual(span.attributes.get(HTTP_RESPONSE_STATUS_CODE), 404)
self.assertEqual(span.attributes.get(ERROR_TYPE), "404")

self.assertIs(
span.status.status_code,
Expand Down Expand Up @@ -527,13 +531,13 @@ def test_requests_exception_new_semconv(self, *_, **__):
self.assertEqual(
span.attributes,
{
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
SpanAttributes.URL_FULL: url_with_port,
SpanAttributes.SERVER_ADDRESS: "mock",
SpanAttributes.SERVER_PORT: 80,
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT: 80,
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS: "mock",
_SPAN_ATTRIBUTES_ERROR_TYPE: "RequestException",
HTTP_REQUEST_METHOD: "GET",
URL_FULL: url_with_port,
SERVER_ADDRESS: "mock",
SERVER_PORT: 80,
NETWORK_PEER_PORT: 80,
NETWORK_PEER_ADDRESS: "mock",
ERROR_TYPE: "RequestException",
},
)
self.assertEqual(span.status.status_code, StatusCode.ERROR)
Expand Down Expand Up @@ -724,11 +728,11 @@ def test_basic_metric_new_semconv(self):
self.perform_request(self.URL)

expected_attributes = {
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
SpanAttributes.SERVER_ADDRESS: "examplehost",
SpanAttributes.SERVER_PORT: 8000,
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
HTTP_RESPONSE_STATUS_CODE: 200,
SERVER_ADDRESS: "examplehost",
SERVER_PORT: 8000,
HTTP_REQUEST_METHOD: "GET",
NETWORK_PROTOCOL_VERSION: "1.1",
}
for (
resource_metrics
Expand Down Expand Up @@ -760,11 +764,11 @@ def test_basic_metric_both_semconv(self):
}

expected_attributes_new = {
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
SpanAttributes.SERVER_ADDRESS: "examplehost",
SpanAttributes.SERVER_PORT: 8000,
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
HTTP_RESPONSE_STATUS_CODE: 200,
SERVER_ADDRESS: "examplehost",
SERVER_PORT: 8000,
HTTP_REQUEST_METHOD: "GET",
NETWORK_PROTOCOL_VERSION: "1.1",
}

for (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he

from opentelemetry import context, trace
from opentelemetry.instrumentation._semconv import (
_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
_SPAN_ATTRIBUTES_ERROR_TYPE,
_filter_semconv_active_request_count_attr,
_filter_semconv_duration_attrs,
_get_schema_url,
Expand Down Expand Up @@ -244,7 +242,11 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
from opentelemetry.instrumentation.wsgi.version import __version__
from opentelemetry.metrics import get_meter
from opentelemetry.propagators.textmap import Getter
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
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.trace.status import Status, StatusCode
from opentelemetry.util.http import (
Expand Down Expand Up @@ -573,7 +575,7 @@ def __init__(
self.duration_histogram_new = None
if _report_new(sem_conv_opt_in_mode):
self.duration_histogram_new = self.meter.create_histogram(
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
name=HTTP_SERVER_REQUEST_DURATION,
unit="s",
description="measures the duration of the inbound HTTP request",
)
Expand Down Expand Up @@ -670,11 +672,9 @@ def __call__(self, environ, start_response):
return _end_span_after_iterating(iterable, span, token)
except Exception as ex:
if _report_new(self._sem_conv_opt_in_mode):
req_attrs[_SPAN_ATTRIBUTES_ERROR_TYPE] = type(ex).__qualname__
req_attrs[ERROR_TYPE] = type(ex).__qualname__
if span.is_recording():
span.set_attribute(
_SPAN_ATTRIBUTES_ERROR_TYPE, type(ex).__qualname__
)
span.set_attribute(ERROR_TYPE, type(ex).__qualname__)
span.set_status(Status(StatusCode.ERROR, str(ex)))
span.end()
if token is not None:
Expand Down
Loading

0 comments on commit b94c590

Please sign in to comment.