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 Jun 11, 2024
1 parent 91b9e72 commit 0199d61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
OTEL_SEMCONV_STABILITY_OPT_IN,
_OpenTelemetrySemanticConventionStability,
_server_active_requests_count_attrs_new,
_server_duration_attrs_new_with_server_attributes,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_old,
Expand Down Expand Up @@ -99,7 +100,7 @@ def expected_attributes_new(override_attributes):
}
_recommended_metrics_attrs_new = {
"http.server.active_requests": _server_active_requests_count_attrs_new,
"http.server.request.duration": _server_duration_attrs_new,
"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 @@ -132,6 +133,7 @@ def setUp(self):
"os.environ",
{
"OTEL_PYTHON_FLASK_EXCLUDED_URLS": "http://localhost/env_excluded_arg/123,env_excluded_noarg",
"OTEL_ENABLE_SERVER_ATTRIBUTES_FOR_REQUEST_DURATION": "true",
OTEL_SEMCONV_STABILITY_OPT_IN: sem_conv_mode,
},
)
Expand Down Expand Up @@ -519,6 +521,9 @@ def test_flask_metrics_new_semconv(self):
histogram_data_point_seen = True
if isinstance(point, NumberDataPoint):
number_data_point_seen = True
for attr in _recommended_metrics_attrs_new[metric.name]:
if attr != _SPAN_ATTRIBUTES_ERROR_TYPE:
self.assertIn(attr, point.attributes)
for attr in point.attributes:
self.assertIn(
attr,
Expand Down Expand Up @@ -596,9 +601,12 @@ 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",
"network.protocol.version": "1.1",
"http.response.status_code": 200,
"server.address": "localhost",
"server.port": 80,
}
expected_requests_count_attributes = {
"http.request.method": "GET",
Expand Down Expand Up @@ -642,6 +650,8 @@ 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",
Expand All @@ -667,6 +677,8 @@ 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
"""

import functools
import os
import typing
import wsgiref.util as wsgiref_util
from timeit import default_timer
Expand All @@ -225,6 +226,7 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
_report_new,
_report_old,
_server_active_requests_count_attrs_new,
_server_duration_attrs_new_with_server_attributes,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_old,
Expand Down Expand Up @@ -463,7 +465,9 @@ def _parse_duration_attrs(
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_with_server_attributes
if os.environ.get("OTEL_ENABLE_SERVER_ATTRIBUTES_FOR_REQUEST_DURATION")
else _server_duration_attrs_new,
sem_conv_opt_in_mode,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@
SpanAttributes.URL_SCHEME,
]

_server_duration_attrs_new_with_server_attributes = [
_SPAN_ATTRIBUTES_ERROR_TYPE,
SpanAttributes.HTTP_REQUEST_METHOD,
SpanAttributes.HTTP_RESPONSE_STATUS_CODE,
SpanAttributes.HTTP_ROUTE,
SpanAttributes.NETWORK_PROTOCOL_VERSION,
SpanAttributes.URL_SCHEME,
SpanAttributes.SERVER_ADDRESS,
SpanAttributes.SERVER_PORT,
]

_server_active_requests_count_attrs_old = [
SpanAttributes.HTTP_METHOD,
SpanAttributes.HTTP_HOST,
Expand Down

0 comments on commit 0199d61

Please sign in to comment.