Skip to content

Commit dc553bf

Browse files
lzchenxrmx
authored andcommitted
Use generated symbols from semantic conventions package (open-telemetry#2611)
1 parent b46ec80 commit dc553bf

File tree

8 files changed

+172
-161
lines changed

8 files changed

+172
-161
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
([#2572](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2572))
2626
- `opentelemetry-instrumentation-aiohttp-server`, `opentelemetry-instrumentation-httpx` Ensure consistently use of suppress_instrumentation utils
2727
([#2590](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2590))
28+
- Reference symbols from generated semantic conventions
29+
([#2611](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2611))
2830

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

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ def response_hook(span: Span, status: str, response_headers: List):
251251
import opentelemetry.instrumentation.wsgi as otel_wsgi
252252
from opentelemetry import context, trace
253253
from opentelemetry.instrumentation._semconv import (
254-
_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
255254
_get_schema_url,
256255
_HTTPStabilityMode,
257256
_OpenTelemetrySemanticConventionStability,
@@ -268,6 +267,9 @@ def response_hook(span: Span, status: str, response_headers: List):
268267
from opentelemetry.instrumentation.utils import _start_internal_or_server_span
269268
from opentelemetry.metrics import get_meter
270269
from opentelemetry.semconv.metrics import MetricInstruments
270+
from opentelemetry.semconv.metrics.http_metrics import (
271+
HTTP_SERVER_REQUEST_DURATION,
272+
)
271273
from opentelemetry.semconv.trace import SpanAttributes
272274
from opentelemetry.util.http import (
273275
get_excluded_urls,
@@ -553,7 +555,7 @@ def __init__(self, *args, **kwargs):
553555
duration_histogram_new = None
554556
if _report_new(_InstrumentedFlask._sem_conv_opt_in_mode):
555557
duration_histogram_new = meter.create_histogram(
556-
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
558+
name=HTTP_SERVER_REQUEST_DURATION,
557559
unit="s",
558560
description="measures the duration of the inbound HTTP request",
559561
)
@@ -684,7 +686,7 @@ def instrument_app(
684686
duration_histogram_new = None
685687
if _report_new(sem_conv_opt_in_mode):
686688
duration_histogram_new = meter.create_histogram(
687-
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
689+
name=HTTP_SERVER_REQUEST_DURATION,
688690
unit="s",
689691
description="measures the duration of the inbound HTTP request",
690692
)

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from opentelemetry import trace
2222
from opentelemetry.instrumentation._semconv import (
23-
_SPAN_ATTRIBUTES_ERROR_TYPE,
2423
OTEL_SEMCONV_STABILITY_OPT_IN,
2524
_OpenTelemetrySemanticConventionStability,
2625
_server_active_requests_count_attrs_new,
@@ -40,6 +39,7 @@
4039
NumberDataPoint,
4140
)
4241
from opentelemetry.sdk.resources import Resource
42+
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
4343
from opentelemetry.semconv.trace import SpanAttributes
4444
from opentelemetry.test.wsgitestutil import WsgiTestBase
4545
from opentelemetry.util.http import (
@@ -379,7 +379,7 @@ def test_internal_error_new_semconv(self):
379379
SpanAttributes.URL_PATH: "/hello/500",
380380
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
381381
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
382-
_SPAN_ATTRIBUTES_ERROR_TYPE: "500",
382+
ERROR_TYPE: "500",
383383
SpanAttributes.URL_SCHEME: "http",
384384
}
385385
)
@@ -405,7 +405,7 @@ def test_internal_error_both_semconv(self):
405405
{
406406
SpanAttributes.URL_PATH: "/hello/500",
407407
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
408-
_SPAN_ATTRIBUTES_ERROR_TYPE: "500",
408+
ERROR_TYPE: "500",
409409
SpanAttributes.URL_SCHEME: "http",
410410
}
411411
)

instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py

+15-23
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@
5959
from requests.structures import CaseInsensitiveDict
6060

6161
from opentelemetry.instrumentation._semconv import (
62-
_METRIC_ATTRIBUTES_CLIENT_DURATION_NAME,
63-
_SPAN_ATTRIBUTES_ERROR_TYPE,
64-
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS,
65-
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT,
6662
_client_duration_attrs_new,
6763
_client_duration_attrs_old,
6864
_filter_semconv_duration_attrs,
@@ -91,7 +87,15 @@
9187
)
9288
from opentelemetry.metrics import Histogram, get_meter
9389
from opentelemetry.propagate import inject
90+
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
91+
from opentelemetry.semconv.attributes.network_attributes import (
92+
NETWORK_PEER_ADDRESS,
93+
NETWORK_PEER_PORT,
94+
)
9495
from opentelemetry.semconv.metrics import MetricInstruments
96+
from opentelemetry.semconv.metrics.http_metrics import (
97+
HTTP_CLIENT_REQUEST_DURATION,
98+
)
9599
from opentelemetry.trace import SpanKind, Tracer, get_tracer
96100
from opentelemetry.trace.span import Span
97101
from opentelemetry.trace.status import StatusCode
@@ -191,9 +195,7 @@ def get_or_create_headers():
191195
sem_conv_opt_in_mode,
192196
)
193197
# Use semconv library when available
194-
span_attributes[_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS] = (
195-
parsed_url.hostname
196-
)
198+
span_attributes[NETWORK_PEER_ADDRESS] = parsed_url.hostname
197199
if parsed_url.port:
198200
_set_http_peer_port_client(
199201
metric_labels, parsed_url.port, sem_conv_opt_in_mode
@@ -203,9 +205,7 @@ def get_or_create_headers():
203205
span_attributes, parsed_url.port, sem_conv_opt_in_mode
204206
)
205207
# Use semconv library when available
206-
span_attributes[_SPAN_ATTRIBUTES_NETWORK_PEER_PORT] = (
207-
parsed_url.port
208-
)
208+
span_attributes[NETWORK_PEER_PORT] = parsed_url.port
209209
except ValueError:
210210
pass
211211

@@ -250,12 +250,8 @@ def get_or_create_headers():
250250
_report_new(sem_conv_opt_in_mode)
251251
and status_code is StatusCode.ERROR
252252
):
253-
span_attributes[_SPAN_ATTRIBUTES_ERROR_TYPE] = str(
254-
result.status_code
255-
)
256-
metric_labels[_SPAN_ATTRIBUTES_ERROR_TYPE] = str(
257-
result.status_code
258-
)
253+
span_attributes[ERROR_TYPE] = str(result.status_code)
254+
metric_labels[ERROR_TYPE] = str(result.status_code)
259255

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

280276
if exception is not None and _report_new(sem_conv_opt_in_mode):
281-
span.set_attribute(
282-
_SPAN_ATTRIBUTES_ERROR_TYPE, type(exception).__qualname__
283-
)
284-
metric_labels[_SPAN_ATTRIBUTES_ERROR_TYPE] = type(
285-
exception
286-
).__qualname__
277+
span.set_attribute(ERROR_TYPE, type(exception).__qualname__)
278+
metric_labels[ERROR_TYPE] = type(exception).__qualname__
287279

288280
if duration_histogram_old is not None:
289281
duration_attrs_old = _filter_semconv_duration_attrs(
@@ -403,7 +395,7 @@ def _instrument(self, **kwargs):
403395
duration_histogram_new = None
404396
if _report_new(semconv_opt_in_mode):
405397
duration_histogram_new = meter.create_histogram(
406-
name=_METRIC_ATTRIBUTES_CLIENT_DURATION_NAME,
398+
name=HTTP_CLIENT_REQUEST_DURATION,
407399
unit="s",
408400
description="Duration of HTTP client requests.",
409401
)

instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py

+52-48
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
import opentelemetry.instrumentation.requests
2424
from opentelemetry import trace
2525
from opentelemetry.instrumentation._semconv import (
26-
_SPAN_ATTRIBUTES_ERROR_TYPE,
27-
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS,
28-
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT,
2926
OTEL_SEMCONV_STABILITY_OPT_IN,
3027
_OpenTelemetrySemanticConventionStability,
3128
)
@@ -36,6 +33,21 @@
3633
)
3734
from opentelemetry.propagate import get_global_textmap, set_global_textmap
3835
from opentelemetry.sdk import resources
36+
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
37+
from opentelemetry.semconv.attributes.http_attributes import (
38+
HTTP_REQUEST_METHOD,
39+
HTTP_RESPONSE_STATUS_CODE,
40+
)
41+
from opentelemetry.semconv.attributes.network_attributes import (
42+
NETWORK_PEER_ADDRESS,
43+
NETWORK_PEER_PORT,
44+
NETWORK_PROTOCOL_VERSION,
45+
)
46+
from opentelemetry.semconv.attributes.server_attributes import (
47+
SERVER_ADDRESS,
48+
SERVER_PORT,
49+
)
50+
from opentelemetry.semconv.attributes.url_attributes import URL_FULL
3951
from opentelemetry.semconv.trace import SpanAttributes
4052
from opentelemetry.test.mock_textmap import MockTextMapPropagator
4153
from opentelemetry.test.test_base import TestBase
@@ -176,14 +188,14 @@ def test_basic_new_semconv(self):
176188
self.assertEqual(
177189
span.attributes,
178190
{
179-
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
180-
SpanAttributes.URL_FULL: url_with_port,
181-
SpanAttributes.SERVER_ADDRESS: "mock",
182-
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS: "mock",
183-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
184-
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
185-
SpanAttributes.SERVER_PORT: 80,
186-
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT: 80,
191+
HTTP_REQUEST_METHOD: "GET",
192+
URL_FULL: url_with_port,
193+
SERVER_ADDRESS: "mock",
194+
NETWORK_PEER_ADDRESS: "mock",
195+
HTTP_RESPONSE_STATUS_CODE: 200,
196+
NETWORK_PROTOCOL_VERSION: "1.1",
197+
SERVER_PORT: 80,
198+
NETWORK_PEER_PORT: 80,
187199
},
188200
)
189201

@@ -213,19 +225,19 @@ def test_basic_both_semconv(self):
213225
span.attributes,
214226
{
215227
SpanAttributes.HTTP_METHOD: "GET",
216-
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
228+
HTTP_REQUEST_METHOD: "GET",
217229
SpanAttributes.HTTP_URL: url_with_port,
218-
SpanAttributes.URL_FULL: url_with_port,
230+
URL_FULL: url_with_port,
219231
SpanAttributes.HTTP_HOST: "mock",
220-
SpanAttributes.SERVER_ADDRESS: "mock",
221-
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS: "mock",
232+
SERVER_ADDRESS: "mock",
233+
NETWORK_PEER_ADDRESS: "mock",
222234
SpanAttributes.NET_PEER_PORT: 80,
223235
SpanAttributes.HTTP_STATUS_CODE: 200,
224-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
236+
HTTP_RESPONSE_STATUS_CODE: 200,
225237
SpanAttributes.HTTP_FLAVOR: "1.1",
226-
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
227-
SpanAttributes.SERVER_PORT: 80,
228-
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT: 80,
238+
NETWORK_PROTOCOL_VERSION: "1.1",
239+
SERVER_PORT: 80,
240+
NETWORK_PEER_PORT: 80,
229241
},
230242
)
231243

@@ -328,12 +340,8 @@ def test_not_foundbasic_new_semconv(self):
328340

329341
span = self.assert_span()
330342

331-
self.assertEqual(
332-
span.attributes.get(SpanAttributes.HTTP_RESPONSE_STATUS_CODE), 404
333-
)
334-
self.assertEqual(
335-
span.attributes.get(_SPAN_ATTRIBUTES_ERROR_TYPE), "404"
336-
)
343+
self.assertEqual(span.attributes.get(HTTP_RESPONSE_STATUS_CODE), 404)
344+
self.assertEqual(span.attributes.get(ERROR_TYPE), "404")
337345

338346
self.assertIs(
339347
span.status.status_code,
@@ -355,12 +363,8 @@ def test_not_foundbasic_both_semconv(self):
355363
self.assertEqual(
356364
span.attributes.get(SpanAttributes.HTTP_STATUS_CODE), 404
357365
)
358-
self.assertEqual(
359-
span.attributes.get(SpanAttributes.HTTP_RESPONSE_STATUS_CODE), 404
360-
)
361-
self.assertEqual(
362-
span.attributes.get(_SPAN_ATTRIBUTES_ERROR_TYPE), "404"
363-
)
366+
self.assertEqual(span.attributes.get(HTTP_RESPONSE_STATUS_CODE), 404)
367+
self.assertEqual(span.attributes.get(ERROR_TYPE), "404")
364368

365369
self.assertIs(
366370
span.status.status_code,
@@ -527,13 +531,13 @@ def test_requests_exception_new_semconv(self, *_, **__):
527531
self.assertEqual(
528532
span.attributes,
529533
{
530-
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
531-
SpanAttributes.URL_FULL: url_with_port,
532-
SpanAttributes.SERVER_ADDRESS: "mock",
533-
SpanAttributes.SERVER_PORT: 80,
534-
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT: 80,
535-
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS: "mock",
536-
_SPAN_ATTRIBUTES_ERROR_TYPE: "RequestException",
534+
HTTP_REQUEST_METHOD: "GET",
535+
URL_FULL: url_with_port,
536+
SERVER_ADDRESS: "mock",
537+
SERVER_PORT: 80,
538+
NETWORK_PEER_PORT: 80,
539+
NETWORK_PEER_ADDRESS: "mock",
540+
ERROR_TYPE: "RequestException",
537541
},
538542
)
539543
self.assertEqual(span.status.status_code, StatusCode.ERROR)
@@ -724,11 +728,11 @@ def test_basic_metric_new_semconv(self):
724728
self.perform_request(self.URL)
725729

726730
expected_attributes = {
727-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
728-
SpanAttributes.SERVER_ADDRESS: "examplehost",
729-
SpanAttributes.SERVER_PORT: 8000,
730-
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
731-
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
731+
HTTP_RESPONSE_STATUS_CODE: 200,
732+
SERVER_ADDRESS: "examplehost",
733+
SERVER_PORT: 8000,
734+
HTTP_REQUEST_METHOD: "GET",
735+
NETWORK_PROTOCOL_VERSION: "1.1",
732736
}
733737
for (
734738
resource_metrics
@@ -760,11 +764,11 @@ def test_basic_metric_both_semconv(self):
760764
}
761765

762766
expected_attributes_new = {
763-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
764-
SpanAttributes.SERVER_ADDRESS: "examplehost",
765-
SpanAttributes.SERVER_PORT: 8000,
766-
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
767-
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
767+
HTTP_RESPONSE_STATUS_CODE: 200,
768+
SERVER_ADDRESS: "examplehost",
769+
SERVER_PORT: 8000,
770+
HTTP_REQUEST_METHOD: "GET",
771+
NETWORK_PROTOCOL_VERSION: "1.1",
768772
}
769773

770774
for (

instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
214214

215215
from opentelemetry import context, trace
216216
from opentelemetry.instrumentation._semconv import (
217-
_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
218-
_SPAN_ATTRIBUTES_ERROR_TYPE,
219217
_filter_semconv_active_request_count_attr,
220218
_filter_semconv_duration_attrs,
221219
_get_schema_url,
@@ -244,7 +242,11 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
244242
from opentelemetry.instrumentation.wsgi.version import __version__
245243
from opentelemetry.metrics import get_meter
246244
from opentelemetry.propagators.textmap import Getter
245+
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
247246
from opentelemetry.semconv.metrics import MetricInstruments
247+
from opentelemetry.semconv.metrics.http_metrics import (
248+
HTTP_SERVER_REQUEST_DURATION,
249+
)
248250
from opentelemetry.semconv.trace import SpanAttributes
249251
from opentelemetry.trace.status import Status, StatusCode
250252
from opentelemetry.util.http import (
@@ -573,7 +575,7 @@ def __init__(
573575
self.duration_histogram_new = None
574576
if _report_new(sem_conv_opt_in_mode):
575577
self.duration_histogram_new = self.meter.create_histogram(
576-
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
578+
name=HTTP_SERVER_REQUEST_DURATION,
577579
unit="s",
578580
description="measures the duration of the inbound HTTP request",
579581
)
@@ -670,11 +672,9 @@ def __call__(self, environ, start_response):
670672
return _end_span_after_iterating(iterable, span, token)
671673
except Exception as ex:
672674
if _report_new(self._sem_conv_opt_in_mode):
673-
req_attrs[_SPAN_ATTRIBUTES_ERROR_TYPE] = type(ex).__qualname__
675+
req_attrs[ERROR_TYPE] = type(ex).__qualname__
674676
if span.is_recording():
675-
span.set_attribute(
676-
_SPAN_ATTRIBUTES_ERROR_TYPE, type(ex).__qualname__
677-
)
677+
span.set_attribute(ERROR_TYPE, type(ex).__qualname__)
678678
span.set_status(Status(StatusCode.ERROR, str(ex)))
679679
span.end()
680680
if token is not None:

0 commit comments

Comments
 (0)