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

add OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED flag #2597

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
pr review
zeitlinger committed Sep 11, 2024
commit 36373e4e4057ea997b82c5734a5572be890cc7d4
Original file line number Diff line number Diff line change
@@ -555,10 +555,6 @@ def __init__(
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
_OpenTelemetryStabilitySignalType.HTTP,
)
self.server_active_requests_count_attrs_new = (
_server_active_requests_count_attrs_new_effective
)
self.server_duration_attrs_new = _server_duration_attrs_new_effective
self.app = guarantee_single_callable(app)
self.tracer = (
trace.get_tracer(
@@ -702,7 +698,7 @@ async def __call__(
context_getter=asgi_getter,
attributes=attributes,
)
active_requests_count_attrs = self._parse_active_request_count_attrs(
active_requests_count_attrs = _parse_active_request_count_attrs(
attributes,
self._sem_conv_opt_in_mode,
)
@@ -758,12 +754,12 @@ async def __call__(
self._sem_conv_opt_in_mode,
)
duration_s = default_timer() - start
duration_attrs_old = self._parse_duration_attrs(
duration_attrs_old = _parse_duration_attrs(
attributes, _HTTPStabilityMode.DEFAULT
)
if target:
duration_attrs_old[SpanAttributes.HTTP_TARGET] = target
duration_attrs_new = self._parse_duration_attrs(
duration_attrs_new = _parse_duration_attrs(
attributes, _HTTPStabilityMode.HTTP
)
if self.duration_histogram_old:
@@ -962,22 +958,24 @@ async def otel_send(message: dict[str, Any]):

return otel_send

def _parse_duration_attrs(
self, req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
self.server_duration_attrs_new,
sem_conv_opt_in_mode,
)

def _parse_active_request_count_attrs(
self, req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
return _filter_semconv_active_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
self.server_active_requests_count_attrs_new,
sem_conv_opt_in_mode,
)
def _parse_duration_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
_server_duration_attrs_new_effective,
sem_conv_opt_in_mode,
)


def _parse_active_request_count_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
return _filter_semconv_active_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
_server_active_requests_count_attrs_new_effective,
sem_conv_opt_in_mode,
)
Original file line number Diff line number Diff line change
@@ -23,11 +23,15 @@

from opentelemetry.context import detach
from opentelemetry.instrumentation._semconv import (
_filter_semconv_active_server_request_count_attr,
_filter_semconv_server_duration_attrs,
_filter_semconv_active_request_count_attr,
_filter_semconv_duration_attrs,
_HTTPStabilityMode,
_report_new,
_report_old,
_server_active_requests_count_attrs_new_effective,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new_effective,
_server_duration_attrs_old,
)
from opentelemetry.instrumentation.propagators import (
get_global_response_propagator,
@@ -220,11 +224,9 @@ def process_request(self, request):
attributes=attributes,
)

active_requests_count_attrs = (
_filter_semconv_active_server_request_count_attr(
attributes,
self._sem_conv_opt_in_mode,
)
active_requests_count_attrs = _parse_active_request_count_attrs(
attributes,
self._sem_conv_opt_in_mode,
)

request.META[self._environ_active_request_attr_key] = (
@@ -422,9 +424,8 @@ def process_response(self, request, response):
if request_start_time is not None:
duration_s = default_timer() - request_start_time
if self._duration_histogram_old:
duration_attrs_old = _filter_semconv_server_duration_attrs(
duration_attrs,
_HTTPStabilityMode.DEFAULT,
duration_attrs_old = _parse_duration_attrs(
duration_attrs, _HTTPStabilityMode.DEFAULT
)
# http.target to be included in old semantic conventions
target = duration_attrs.get(SpanAttributes.HTTP_TARGET)
@@ -434,9 +435,8 @@ def process_response(self, request, response):
max(round(duration_s * 1000), 0), duration_attrs_old
)
if self._duration_histogram_new:
duration_attrs_new = _filter_semconv_server_duration_attrs(
duration_attrs,
_HTTPStabilityMode.HTTP,
duration_attrs_new = _parse_duration_attrs(
duration_attrs, _HTTPStabilityMode.HTTP
)
self._duration_histogram_new.record(
max(duration_s, 0), duration_attrs_new
@@ -447,3 +447,25 @@ def process_response(self, request, response):
request.META.pop(self._environ_token)

return response


def _parse_duration_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
_server_duration_attrs_new_effective,
sem_conv_opt_in_mode,
)


def _parse_active_request_count_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
return _filter_semconv_active_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
_server_active_requests_count_attrs_new_effective,
sem_conv_opt_in_mode,
)
Original file line number Diff line number Diff line change
@@ -193,11 +193,6 @@ def response_hook(span, req, resp):

import opentelemetry.instrumentation.wsgi as otel_wsgi
from opentelemetry import context, trace
from opentelemetry.instrumentation._semconv import (
_filter_semconv_active_server_request_count_attr,
_filter_semconv_server_duration_attrs,
_HTTPStabilityMode,
)
from opentelemetry.instrumentation.falcon.package import _instruments
from opentelemetry.instrumentation.falcon.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -352,15 +347,9 @@ def __call__(self, env, start_response):
)
attributes = otel_wsgi.collect_request_attributes(env)
active_requests_count_attrs = (
_filter_semconv_active_server_request_count_attr(
attributes,
_HTTPStabilityMode.DEFAULT,
)
)
duration_attrs = _filter_semconv_server_duration_attrs(
attributes,
_HTTPStabilityMode.DEFAULT,
otel_wsgi._parse_active_request_count_attrs(attributes)
)
duration_attrs = otel_wsgi._parse_duration_attrs(attributes)
self.active_requests_counter.add(1, active_requests_count_attrs)

if span.is_recording():
Original file line number Diff line number Diff line change
@@ -251,8 +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 (
_filter_semconv_active_server_request_count_attr,
_filter_semconv_server_duration_attrs,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
@@ -336,7 +334,7 @@ def _wrapped_app(wrapped_app_environ, start_response):
wrapped_app_environ, sem_conv_opt_in_mode
)
active_requests_count_attrs = (
_filter_semconv_active_server_request_count_attr(
otel_wsgi._parse_active_request_count_attrs(
attributes,
sem_conv_opt_in_mode,
)
@@ -392,9 +390,8 @@ def _start_response(status, response_headers, *args, **kwargs):
result = wsgi_app(wrapped_app_environ, _start_response)
duration_s = default_timer() - start
if duration_histogram_old:
duration_attrs_old = _filter_semconv_server_duration_attrs(
attributes,
_HTTPStabilityMode.DEFAULT,
duration_attrs_old = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.DEFAULT
)

if request_route:
@@ -407,9 +404,8 @@ def _start_response(status, response_headers, *args, **kwargs):
max(round(duration_s * 1000), 0), duration_attrs_old
)
if duration_histogram_new:
duration_attrs_new = _filter_semconv_server_duration_attrs(
attributes,
_HTTPStabilityMode.HTTP,
duration_attrs_new = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.HTTP
)

if request_route:
Original file line number Diff line number Diff line change
@@ -23,11 +23,6 @@

import opentelemetry.instrumentation.wsgi as otel_wsgi
from opentelemetry import context, trace
from opentelemetry.instrumentation._semconv import (
_filter_semconv_active_server_request_count_attr,
_filter_semconv_server_duration_attrs,
_HTTPStabilityMode,
)
from opentelemetry.instrumentation.propagators import (
get_global_response_propagator,
)
@@ -177,14 +172,9 @@ def trace_tween(request):
request.environ[_ENVIRON_ENABLED_KEY] = True
request.environ[_ENVIRON_STARTTIME_KEY] = time_ns()
active_requests_count_attrs = (
_filter_semconv_active_server_request_count_attr(
attributes,
)
)
duration_attrs = _filter_semconv_server_duration_attrs(
attributes,
_HTTPStabilityMode.DEFAULT,
otel_wsgi._parse_active_request_count_attrs(attributes)
)
duration_attrs = otel_wsgi._parse_duration_attrs(attributes)

start = default_timer()
active_requests_counter.add(1, active_requests_count_attrs)
Original file line number Diff line number Diff line change
@@ -83,7 +83,9 @@ def response_hook(span, request_obj, response)
from requests.structures import CaseInsensitiveDict

from opentelemetry.instrumentation._semconv import (
_filter_semconv_client_duration_attrs,
_client_duration_attrs_new,
_client_duration_attrs_old,
_filter_semconv_duration_attrs,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
@@ -306,17 +308,21 @@ def get_or_create_headers():
metric_labels[ERROR_TYPE] = type(exception).__qualname__

if duration_histogram_old is not None:
duration_attrs_old = _filter_semconv_client_duration_attrs(
duration_attrs_old = _filter_semconv_duration_attrs(
metric_labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
_HTTPStabilityMode.DEFAULT,
)
duration_histogram_old.record(
max(round(elapsed_time * 1000), 0),
attributes=duration_attrs_old,
)
if duration_histogram_new is not None:
duration_attrs_new = _filter_semconv_client_duration_attrs(
duration_attrs_new = _filter_semconv_duration_attrs(
metric_labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
_HTTPStabilityMode.HTTP,
)
duration_histogram_new.record(
Original file line number Diff line number Diff line change
@@ -86,7 +86,9 @@ def response_hook(span, request_obj, response)
)

from opentelemetry.instrumentation._semconv import (
_filter_semconv_client_duration_attrs,
_client_duration_attrs_new,
_client_duration_attrs_old,
_filter_semconv_duration_attrs,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
@@ -301,11 +303,16 @@ def _instrumented_open_call(
span.set_attribute(ERROR_TYPE, type(exception).__qualname__)
labels[ERROR_TYPE] = type(exception).__qualname__

duration_attrs_old = _filter_semconv_client_duration_attrs(
duration_attrs_old = _filter_semconv_duration_attrs(
labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT,
)
duration_attrs_new = _filter_semconv_client_duration_attrs(
duration_attrs_new = _filter_semconv_duration_attrs(
labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP,
)

Original file line number Diff line number Diff line change
@@ -94,7 +94,9 @@ def response_hook(
import wrapt

from opentelemetry.instrumentation._semconv import (
_filter_semconv_client_duration_attrs,
_client_duration_attrs_new,
_client_duration_attrs_old,
_filter_semconv_duration_attrs,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
@@ -521,12 +523,17 @@ def _filter_attributes_semconv(
duration_attrs_old = None
duration_attrs_new = None
if _report_old(sem_conv_opt_in_mode):
duration_attrs_old = _filter_semconv_client_duration_attrs(
duration_attrs_old = _filter_semconv_duration_attrs(
metric_attributes,
_client_duration_attrs_old,
_client_duration_attrs_new,
_HTTPStabilityMode.DEFAULT,
)
if _report_new(sem_conv_opt_in_mode):
duration_attrs_new = _filter_semconv_client_duration_attrs(
duration_attrs_new = _filter_semconv_duration_attrs(
metric_attributes,
_client_duration_attrs_old,
_client_duration_attrs_new,
_HTTPStabilityMode.HTTP,
)

Loading