Skip to content

Commit

Permalink
add OTEL_ENABLE_SERVER_ATTRIBUTES_FOR_REQUEST_DURATION flag to add se…
Browse files Browse the repository at this point in the history
…rver.address and server.port for http.server.request.duration
  • Loading branch information
zeitlinger committed Aug 27, 2024
1 parent 476f6e2 commit 9662dbc
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,14 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A

from opentelemetry import context, trace
from opentelemetry.instrumentation._semconv import (
_filter_semconv_active_request_count_attr,
_filter_semconv_duration_attrs,
_filter_semconv_active_server_request_count_attr,
_filter_semconv_server_duration_attrs,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilitySignalType,
_report_new,
_report_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_old,
_set_http_flavor_version,
_set_http_host_server,
_set_http_method,
Expand Down Expand Up @@ -920,20 +916,16 @@ async def otel_send(message: dict[str, Any]):
def _parse_duration_attrs(
req_attrs, sem_conv_opt_in_mode=_HTTPStabilityMode.DEFAULT
):
return _filter_semconv_duration_attrs(
return _filter_semconv_server_duration_attrs(
req_attrs,
_server_duration_attrs_old,
_server_duration_attrs_new,
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(
return _filter_semconv_active_server_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
_server_active_requests_count_attrs_new,
sem_conv_opt_in_mode,
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@

from opentelemetry.context import detach
from opentelemetry.instrumentation._semconv import (
_filter_semconv_active_request_count_attr,
_filter_semconv_duration_attrs,
_filter_semconv_active_server_request_count_attr,
_filter_semconv_server_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,
)
from opentelemetry.instrumentation.propagators import (
get_global_response_propagator,
Expand Down Expand Up @@ -224,9 +220,11 @@ def process_request(self, request):
attributes=attributes,
)

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

request.META[self._environ_active_request_attr_key] = (
Expand Down Expand Up @@ -424,8 +422,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 = _parse_duration_attrs(
duration_attrs, _HTTPStabilityMode.DEFAULT
duration_attrs_old = _filter_semconv_server_duration_attrs(
duration_attrs,
)
# http.target to be included in old semantic conventions
target = duration_attrs.get(SpanAttributes.HTTP_TARGET)
Expand All @@ -435,8 +433,9 @@ def process_response(self, request, response):
max(round(duration_s * 1000), 0), duration_attrs_old
)
if self._duration_histogram_new:
duration_attrs_new = _parse_duration_attrs(
duration_attrs, _HTTPStabilityMode.HTTP
duration_attrs_new = _filter_semconv_server_duration_attrs(
duration_attrs,
_HTTPStabilityMode.HTTP,
)
self._duration_histogram_new.record(
max(duration_s, 0), duration_attrs_new
Expand All @@ -447,25 +446,3 @@ 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,
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,
sem_conv_opt_in_mode,
)
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ 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
Expand Down Expand Up @@ -347,9 +352,14 @@ def __call__(self, env, start_response):
)
attributes = otel_wsgi.collect_request_attributes(env)
active_requests_count_attrs = (
otel_wsgi._parse_active_request_count_attrs(attributes)
_filter_semconv_active_server_request_count_attr(
attributes,
_HTTPStabilityMode.DEFAULT,
)
)
duration_attrs = _filter_semconv_server_duration_attrs(
attributes,
)
duration_attrs = otel_wsgi._parse_duration_attrs(attributes)
self.active_requests_counter.add(1, active_requests_count_attrs)

if span.is_recording():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ 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,
Expand Down Expand Up @@ -334,7 +336,7 @@ def _wrapped_app(wrapped_app_environ, start_response):
wrapped_app_environ, sem_conv_opt_in_mode
)
active_requests_count_attrs = (
otel_wsgi._parse_active_request_count_attrs(
_filter_semconv_active_server_request_count_attr(
attributes,
sem_conv_opt_in_mode,
)
Expand Down Expand Up @@ -390,8 +392,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 = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.DEFAULT
duration_attrs_old = _filter_semconv_server_duration_attrs(
attributes,
)

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

if request_route:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
OTEL_SEMCONV_STABILITY_OPT_IN,
_OpenTelemetrySemanticConventionStability,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_new_with_server_attributes,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_with_server_attributes,
_server_duration_attrs_old,
)
from opentelemetry.instrumentation.flask import FlaskInstrumentor
Expand Down Expand Up @@ -104,8 +106,8 @@ def expected_attributes_new(override_attributes):
"http.server.duration": _server_duration_attrs_old_copy,
}
_recommended_metrics_attrs_new = {
"http.server.active_requests": _server_active_requests_count_attrs_new,
"http.server.request.duration": _server_duration_attrs_new_copy,
"http.server.active_requests": _server_active_requests_count_attrs_new_with_server_attributes,
"http.server.request.duration": _server_duration_attrs_new_with_server_attributes,
}
_server_active_requests_count_attrs_both = (
_server_active_requests_count_attrs_old
Expand Down Expand Up @@ -138,6 +140,8 @@ def setUp(self):
"os.environ",
{
"OTEL_PYTHON_FLASK_EXCLUDED_URLS": "http://localhost/env_excluded_arg/123,env_excluded_noarg",
"OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED": "true",
"OTEL_PYTHON_HTTP_SERVER_ACTIVE_REQUESTS_COUNT_SERVER_ATTRIBUTES_ENABLED": "true",
OTEL_SEMCONV_STABILITY_OPT_IN: sem_conv_mode,
},
)
Expand Down Expand Up @@ -514,22 +518,25 @@ def test_flask_metrics_new_semconv(self):
self.assertTrue(len(scope_metric.metrics) != 0)
for metric in scope_metric.metrics:
self.assertIn(metric.name, _expected_metric_names_new)
data_points = list(metric.data.data_points)
self.assertEqual(len(data_points), 1)
for point in data_points:
if isinstance(point, HistogramDataPoint):
self.assertEqual(point.count, 3)
self.assertAlmostEqual(
duration_s, point.sum, places=2
)
histogram_data_point_seen = True
if isinstance(point, NumberDataPoint):
number_data_point_seen = True
for attr in point.attributes:
self.assertIn(
attr,
_recommended_metrics_attrs_new[metric.name],
)
with self.subTest(metric=metric.name):
data_points = list(metric.data.data_points)
self.assertEqual(len(data_points), 1)
for point in data_points:
if isinstance(point, HistogramDataPoint):
self.assertEqual(point.count, 3)
self.assertAlmostEqual(
duration_s, point.sum, places=2
)
histogram_data_point_seen = True
if isinstance(point, NumberDataPoint):
number_data_point_seen = True
for attr in point.attributes:
self.assertIn(
attr,
_recommended_metrics_attrs_new[
metric.name
],
)
self.assertTrue(number_data_point_seen and histogram_data_point_seen)

def test_flask_metric_values(self):
Expand Down Expand Up @@ -601,14 +608,19 @@ def test_basic_metric_success_new_semconv(self):
self.client.get("/hello/756")
expected_duration_attributes = {
"http.request.method": "GET",
"http.route": "/hello/<int:helloid>",
"url.scheme": "http",
"http.route": "/hello/<int:helloid>",
"network.protocol.version": "1.1",
"http.response.status_code": 200,
"server.address": "localhost",
"server.port": 80,
}
expected_requests_count_attributes = {
"http.request.method": "GET",
"url.scheme": "http",
"server.address": "localhost",
"server.port": 80,
}
self._assert_basic_metric(
expected_duration_attributes,
Expand Down Expand Up @@ -646,10 +658,14 @@ def test_basic_metric_nonstandard_http_method_success_new_semconv(self):
"url.scheme": "http",
"network.protocol.version": "1.1",
"http.response.status_code": 405,
"server.address": "localhost",
"server.port": 80,
}
expected_requests_count_attributes = {
"http.request.method": "_OTHER",
"url.scheme": "http",
"server.address": "localhost",
"server.port": 80,
}
self._assert_basic_metric(
expected_duration_attributes,
Expand All @@ -671,10 +687,14 @@ def test_basic_metric_nonstandard_http_method_allowed_success_new_semconv(
"url.scheme": "http",
"network.protocol.version": "1.1",
"http.response.status_code": 405,
"server.address": "localhost",
"server.port": 80,
}
expected_requests_count_attributes = {
"http.request.method": "NONSTANDARD",
"url.scheme": "http",
"server.address": "localhost",
"server.port": 80,
}
self._assert_basic_metric(
expected_duration_attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

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,
)
from opentelemetry.instrumentation.propagators import (
get_global_response_propagator,
)
Expand Down Expand Up @@ -172,9 +176,13 @@ def trace_tween(request):
request.environ[_ENVIRON_ENABLED_KEY] = True
request.environ[_ENVIRON_STARTTIME_KEY] = time_ns()
active_requests_count_attrs = (
otel_wsgi._parse_active_request_count_attrs(attributes)
_filter_semconv_active_server_request_count_attr(
attributes,
)
)
duration_attrs = _filter_semconv_server_duration_attrs(
attributes,
)
duration_attrs = otel_wsgi._parse_duration_attrs(attributes)

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

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

if duration_histogram_old is not None:
duration_attrs_old = _filter_semconv_duration_attrs(
duration_attrs_old = _filter_semconv_client_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_duration_attrs(
duration_attrs_new = _filter_semconv_client_duration_attrs(
metric_labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
_HTTPStabilityMode.HTTP,
)
duration_histogram_new.record(
Expand Down
Loading

0 comments on commit 9662dbc

Please sign in to comment.