Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare semconv utilities to support database stability opt-in #3111

Merged
merged 10 commits into from
Dec 17, 2024
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Add support to database stability opt-in in `_semconv` utilities and add tests
([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111))

### Fixed

- `opentelemetry-instrumentation-httpx` Fix `RequestInfo`/`ResponseInfo` type hints
([#3105](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3105))


## Version 1.29.0/0.50b0 (2024-12-11)

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ def response_hook(span: Span, params: typing.Union[
from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilitySignalType,
_report_new,
_set_http_method,
_set_http_url,
_set_status,
_StabilityMode,
)
from opentelemetry.instrumentation.aiohttp_client.package import _instruments
from opentelemetry.instrumentation.aiohttp_client.version import __version__
Expand Down Expand Up @@ -142,7 +142,7 @@ def _set_http_status_code_attribute(
span,
status_code,
metric_attributes=None,
sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT,
sem_conv_opt_in_mode=_StabilityMode.DEFAULT,
):
status_code_str = str(status_code)
try:
Expand All @@ -169,7 +169,7 @@ def create_trace_config(
request_hook: _RequestHookT = None,
response_hook: _ResponseHookT = None,
tracer_provider: TracerProvider = None,
sem_conv_opt_in_mode: _HTTPStabilityMode = _HTTPStabilityMode.DEFAULT,
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
) -> aiohttp.TraceConfig:
"""Create an aiohttp-compatible trace configuration.

Expand Down Expand Up @@ -326,7 +326,7 @@ def _instrument(
trace_configs: typing.Optional[
typing.Sequence[aiohttp.TraceConfig]
] = None,
sem_conv_opt_in_mode: _HTTPStabilityMode = _HTTPStabilityMode.DEFAULT,
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
):
"""Enables tracing of all ClientSessions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from opentelemetry.instrumentation import aiohttp_client
from opentelemetry.instrumentation._semconv import (
OTEL_SEMCONV_STABILITY_OPT_IN,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
_StabilityMode,
)
from opentelemetry.instrumentation.aiohttp_client import (
AioHttpClientInstrumentor,
Expand Down Expand Up @@ -150,7 +150,7 @@ def test_status_codes_new_semconv(self):
path = "test-path?query=param#foobar"
host, port = self._http_request(
trace_config=aiohttp_client.create_trace_config(
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP
sem_conv_opt_in_mode=_StabilityMode.HTTP
),
url=f"/{path}",
status_code=status_code,
Expand All @@ -173,7 +173,7 @@ def test_status_codes_both_semconv(self):
path = "test-path?query=param#foobar"
host, port = self._http_request(
trace_config=aiohttp_client.create_trace_config(
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP_DUP
sem_conv_opt_in_mode=_StabilityMode.HTTP_DUP
),
url=f"/{path}",
status_code=status_code,
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_schema_url_new_semconv(self):
with self.subTest(status_code=200):
self._http_request(
trace_config=aiohttp_client.create_trace_config(
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP
sem_conv_opt_in_mode=_StabilityMode.HTTP
),
url="/test-path?query=param#foobar",
status_code=200,
Expand All @@ -230,7 +230,7 @@ def test_schema_url_both_semconv(self):
with self.subTest(status_code=200):
self._http_request(
trace_config=aiohttp_client.create_trace_config(
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP_DUP
sem_conv_opt_in_mode=_StabilityMode.HTTP_DUP
),
url="/test-path?query=param#foobar",
status_code=200,
Expand Down Expand Up @@ -398,7 +398,7 @@ async def request_handler(request):

host, port = self._http_request(
trace_config=aiohttp_client.create_trace_config(
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP
sem_conv_opt_in_mode=_StabilityMode.HTTP
),
url="/test",
request_handler=request_handler,
Expand Down Expand Up @@ -426,7 +426,7 @@ async def request_handler(request):

host, port = self._http_request(
trace_config=aiohttp_client.create_trace_config(
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP_DUP
sem_conv_opt_in_mode=_StabilityMode.HTTP_DUP
),
url="/test",
request_handler=request_handler,
Expand Down Expand Up @@ -546,7 +546,7 @@ async def do_request(url):
def test_nonstandard_http_method_new_semconv(self):
trace_configs = [
aiohttp_client.create_trace_config(
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP
sem_conv_opt_in_mode=_StabilityMode.HTTP
)
]
app = HttpServerMock("nonstandard_method")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
_filter_semconv_active_request_count_attr,
_filter_semconv_duration_attrs,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilitySignalType,
_report_new,
Expand All @@ -225,6 +224,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
_set_http_url,
_set_http_user_agent,
_set_status,
_StabilityMode,
)
from opentelemetry.instrumentation.asgi.types import (
ClientRequestHook,
Expand Down Expand Up @@ -324,7 +324,7 @@ def set(self, carrier: dict, key: str, value: str) -> None: # pylint: disable=n

# pylint: disable=too-many-branches
def collect_request_attributes(
scope, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
scope, sem_conv_opt_in_mode=_StabilityMode.DEFAULT
):
"""Collects HTTP request attributes from the ASGI scope and returns a
dictionary to be used as span creation attributes."""
Expand Down Expand Up @@ -356,7 +356,7 @@ def collect_request_attributes(
_set_http_url(
result,
remove_url_credentials(http_url),
_HTTPStabilityMode.DEFAULT,
_StabilityMode.DEFAULT,
)
http_method = scope.get("method", "")
if http_method:
Expand Down Expand Up @@ -439,7 +439,7 @@ def set_status_code(
span,
status_code,
metric_attributes=None,
sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT,
sem_conv_opt_in_mode=_StabilityMode.DEFAULT,
):
"""Adds HTTP response attributes to span using the status_code argument."""
status_code_str = str(status_code)
Expand Down Expand Up @@ -755,12 +755,12 @@ async def __call__(
)
duration_s = default_timer() - start
duration_attrs_old = _parse_duration_attrs(
attributes, _HTTPStabilityMode.DEFAULT
attributes, _StabilityMode.DEFAULT
)
if target:
duration_attrs_old[SpanAttributes.HTTP_TARGET] = target
duration_attrs_new = _parse_duration_attrs(
attributes, _HTTPStabilityMode.HTTP
attributes, _StabilityMode.HTTP
)
if self.duration_histogram_old:
self.duration_histogram_old.record(
Expand Down Expand Up @@ -960,7 +960,7 @@ async def otel_send(message: dict[str, Any]):


def _parse_duration_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
req_attrs, sem_conv_opt_in_mode=_StabilityMode.DEFAULT
):
return _filter_semconv_duration_attrs(
req_attrs,
Expand All @@ -971,7 +971,7 @@ def _parse_duration_attrs(


def _parse_active_request_count_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
req_attrs, sem_conv_opt_in_mode=_StabilityMode.DEFAULT
):
return _filter_semconv_active_request_count_attr(
req_attrs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
from opentelemetry import trace as trace_api
from opentelemetry.instrumentation._semconv import (
OTEL_SEMCONV_STABILITY_OPT_IN,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_old,
_StabilityMode,
)
from opentelemetry.instrumentation.propagators import (
TraceResponsePropagator,
Expand Down Expand Up @@ -1652,7 +1652,7 @@ def test_request_attributes_new_semconv(self):

attrs = otel_asgi.collect_request_attributes(
self.scope,
_HTTPStabilityMode.HTTP,
_StabilityMode.HTTP,
)

self.assertDictEqual(
Expand All @@ -1677,7 +1677,7 @@ def test_request_attributes_both_semconv(self):

attrs = otel_asgi.collect_request_attributes(
self.scope,
_HTTPStabilityMode.HTTP_DUP,
_StabilityMode.HTTP_DUP,
)

self.assertDictEqual(
Expand Down Expand Up @@ -1715,7 +1715,7 @@ def test_query_string_new_semconv(self):
self.scope["query_string"] = b"foo=bar"
attrs = otel_asgi.collect_request_attributes(
self.scope,
_HTTPStabilityMode.HTTP,
_StabilityMode.HTTP,
)
self.assertEqual(attrs[URL_SCHEME], "http")
self.assertEqual(attrs[CLIENT_ADDRESS], "127.0.0.1")
Expand All @@ -1726,7 +1726,7 @@ def test_query_string_both_semconv(self):
self.scope["query_string"] = b"foo=bar"
attrs = otel_asgi.collect_request_attributes(
self.scope,
_HTTPStabilityMode.HTTP_DUP,
_StabilityMode.HTTP_DUP,
)
self.assertEqual(
attrs[SpanAttributes.HTTP_URL], "http://127.0.0.1/?foo=bar"
Expand Down Expand Up @@ -1762,7 +1762,7 @@ def test_response_attributes_new_semconv(self):
self.span,
404,
None,
_HTTPStabilityMode.HTTP,
_StabilityMode.HTTP,
)
expected = (mock.call(HTTP_RESPONSE_STATUS_CODE, 404),)
self.assertEqual(self.span.set_attribute.call_count, 1)
Expand All @@ -1774,7 +1774,7 @@ def test_response_attributes_both_semconv(self):
self.span,
404,
None,
_HTTPStabilityMode.HTTP_DUP,
_StabilityMode.HTTP_DUP,
)
expected = (mock.call(SpanAttributes.HTTP_STATUS_CODE, 404),)
expected2 = (mock.call(HTTP_RESPONSE_STATUS_CODE, 404),)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
from opentelemetry.instrumentation._semconv import (
_filter_semconv_active_request_count_attr,
_filter_semconv_duration_attrs,
_HTTPStabilityMode,
_report_new,
_report_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_old,
_StabilityMode,
)
from opentelemetry.instrumentation.propagators import (
get_global_response_propagator,
Expand Down Expand Up @@ -158,7 +158,7 @@ class _DjangoMiddleware(MiddlewareMixin):
_duration_histogram_old = None
_duration_histogram_new = None
_active_request_counter = None
_sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT
_sem_conv_opt_in_mode = _StabilityMode.DEFAULT

_otel_request_hook: Callable[[Span, HttpRequest], None] = None
_otel_response_hook: Callable[[Span, HttpRequest, HttpResponse], None] = (
Expand Down Expand Up @@ -430,7 +430,7 @@ def process_response(self, request, response):
duration_s = default_timer() - request_start_time
if self._duration_histogram_old:
duration_attrs_old = _parse_duration_attrs(
duration_attrs, _HTTPStabilityMode.DEFAULT
duration_attrs, _StabilityMode.DEFAULT
)
# http.target to be included in old semantic conventions
target = duration_attrs.get(SpanAttributes.HTTP_TARGET)
Expand All @@ -441,7 +441,7 @@ def process_response(self, request, response):
)
if self._duration_histogram_new:
duration_attrs_new = _parse_duration_attrs(
duration_attrs, _HTTPStabilityMode.HTTP
duration_attrs, _StabilityMode.HTTP
)
self._duration_histogram_new.record(
max(duration_s, 0), duration_attrs_new
Expand All @@ -455,7 +455,7 @@ def process_response(self, request, response):


def _parse_duration_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
req_attrs, sem_conv_opt_in_mode=_StabilityMode.DEFAULT
):
return _filter_semconv_duration_attrs(
req_attrs,
Expand All @@ -466,7 +466,7 @@ def _parse_duration_attrs(


def _parse_active_request_count_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
req_attrs, sem_conv_opt_in_mode=_StabilityMode.DEFAULT
):
return _filter_semconv_active_request_count_attr(
req_attrs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A

from opentelemetry.instrumentation._semconv import (
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilitySignalType,
_StabilityMode,
)
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
from opentelemetry.instrumentation.asgi.types import (
Expand Down Expand Up @@ -362,7 +362,7 @@ class _InstrumentedFastAPI(fastapi.FastAPI):
_client_request_hook: ClientRequestHook = None
_client_response_hook: ClientResponseHook = None
_instrumented_fastapi_apps = set()
_sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT
_sem_conv_opt_in_mode = _StabilityMode.DEFAULT

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
Loading
Loading