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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Add `OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED` flag to add
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
`server.address` and `server.port` for `http.server.request.duration`
([#2597](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2597))
- Add `OTEL_PYTHON_HTTP_SERVER_ACTIVE_REQUESTS_COUNT_SERVER_ATTRIBUTES_ENABLED` flag to add
`server.address` and `server.port` for `http.server.active_requests`
([#2597](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2597))
- `opentelemetry-instrumentation-fastapi` Add autoinstrumentation mechanism tests.
([#2860](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2860))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
_OpenTelemetryStabilitySignalType,
_report_new,
_report_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_new_effective,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_effective,
_server_duration_attrs_old,
_set_http_flavor_version,
_set_http_host_server,
Expand Down Expand Up @@ -965,7 +965,7 @@ def _parse_duration_attrs(
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_effective(),
sem_conv_opt_in_mode,
)

Expand All @@ -976,6 +976,6 @@ def _parse_active_request_count_attrs(
return _filter_semconv_active_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_new_effective(),
sem_conv_opt_in_mode,
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
_HTTPStabilityMode,
_report_new,
_report_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_new_effective,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_effective,
_server_duration_attrs_old,
)
from opentelemetry.instrumentation.propagators import (
Expand Down Expand Up @@ -455,7 +455,7 @@ def _parse_duration_attrs(
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_effective(),
sem_conv_opt_in_mode,
)

Expand All @@ -466,6 +466,6 @@ def _parse_active_request_count_attrs(
return _filter_semconv_active_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_new_effective(),
sem_conv_opt_in_mode,
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
OTEL_PYTHON_HTTP_SERVER_ACTIVE_REQUESTS_COUNT_SERVER_ATTRIBUTES_ENABLED,
OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED,
OTEL_SEMCONV_STABILITY_OPT_IN,
_OpenTelemetrySemanticConventionStability,
_server_active_requests_count_attrs_new,
Expand Down Expand Up @@ -514,22 +516,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 @@ -615,6 +620,35 @@ def test_basic_metric_success_new_semconv(self):
expected_requests_count_attributes,
)

@patch.dict(
"os.environ",
{
OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED: "1",
OTEL_PYTHON_HTTP_SERVER_ACTIVE_REQUESTS_COUNT_SERVER_ATTRIBUTES_ENABLED: "1",
},
)
def test_basic_metric_success_new_semconv_server_address(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to add tests for django, asgi and wsgi as well?

self.client.get("/hello/756")
expected_duration_attributes = {
"http.request.method": "GET",
"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,
expected_requests_count_attributes,
)

def test_basic_metric_nonstandard_http_method_success(self):
self.client.open("/hello/756", method="NONSTANDARD")
expected_duration_attributes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
_OpenTelemetryStabilitySignalType,
_report_new,
_report_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_new_effective,
_server_active_requests_count_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_effective,
_server_duration_attrs_old,
_set_http_flavor_version,
_set_http_method,
Expand Down Expand Up @@ -454,7 +454,7 @@ def _parse_active_request_count_attrs(
return _filter_semconv_active_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
_server_active_requests_count_attrs_new,
_server_active_requests_count_attrs_new_effective(),
sem_conv_opt_in_mode,
)

Expand All @@ -465,7 +465,7 @@ def _parse_duration_attrs(
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
_server_duration_attrs_new,
_server_duration_attrs_new_effective(),
sem_conv_opt_in_mode,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.status import Status, StatusCode

OTEL_PYTHON_HTTP_SERVER_ACTIVE_REQUESTS_COUNT_SERVER_ATTRIBUTES_ENABLED = (
"OTEL_PYTHON_HTTP_SERVER_ACTIVE_REQUESTS_COUNT_SERVER_ATTRIBUTES_ENABLED"
)
OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED = (
"OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED"
)

# These lists represent attributes for metrics that are currently supported

_client_duration_attrs_old = [
Expand Down Expand Up @@ -90,6 +97,14 @@
URL_SCHEME,
]

_server_duration_attrs_new_with_server_attributes = (
_server_duration_attrs_new
+ [
SERVER_ADDRESS,
SERVER_PORT,
]
)

_server_active_requests_count_attrs_old = [
SpanAttributes.HTTP_METHOD,
SpanAttributes.HTTP_HOST,
Expand All @@ -101,9 +116,37 @@
_server_active_requests_count_attrs_new = [
HTTP_REQUEST_METHOD,
URL_SCHEME,
# TODO: Support SERVER_ADDRESS AND SERVER_PORT
]

_server_active_requests_count_attrs_new_with_server_attributes = (
_server_active_requests_count_attrs_new
+ [
SERVER_ADDRESS,
SERVER_PORT,
]
)


def _server_active_requests_count_attrs_new_effective():
return (
_server_active_requests_count_attrs_new_with_server_attributes
if os.environ.get(
OTEL_PYTHON_HTTP_SERVER_ACTIVE_REQUESTS_COUNT_SERVER_ATTRIBUTES_ENABLED
)
else _server_active_requests_count_attrs_new
)


def _server_duration_attrs_new_effective():
return (
_server_duration_attrs_new_with_server_attributes
if os.environ.get(
OTEL_PYTHON_HTTP_SERVER_REQUEST_DURATION_SERVER_ATTRIBUTES_ENABLED
)
else _server_duration_attrs_new
)


OTEL_SEMCONV_STABILITY_OPT_IN = "OTEL_SEMCONV_STABILITY_OPT_IN"


Expand Down