Skip to content

Commit 680f197

Browse files
authored
opentelemetry-instrumentation-flask: add explicit http duration buckets for stable semconv (#3523)
* opentelemetry-instrumentation-flask: add explicit http duration buckets for stable semconv * Update CHANGELOG.md * Fix pylint
1 parent 1909c91 commit 680f197

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- `opentelemetry-instrumentation-fastapi`: fix wrapping of middlewares
1717
([#3012](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3012))
18+
- `opentelemetry-instrumentation-flask`: proper bucket boundaries in stable semconv http duration
19+
([#3523](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3523))
1820
- `opentelemetry-instrumentation-django`: proper bucket boundaries in stable semconv http duration
1921
([#3524](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3524))
2022
- `opentelemetry-instrumentation-grpc`: support non-list interceptors

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def response_hook(span: Span, status: str, response_headers: List):
257257
import opentelemetry.instrumentation.wsgi as otel_wsgi
258258
from opentelemetry import context, trace
259259
from opentelemetry.instrumentation._semconv import (
260+
HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
260261
_get_schema_url,
261262
_OpenTelemetrySemanticConventionStability,
262263
_OpenTelemetryStabilitySignalType,
@@ -583,6 +584,7 @@ def __init__(self, *args, **kwargs):
583584
name=HTTP_SERVER_REQUEST_DURATION,
584585
unit="s",
585586
description="Duration of HTTP server requests.",
587+
explicit_bucket_boundaries_advisory=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
586588
)
587589
active_requests_counter = meter.create_up_down_counter(
588590
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
@@ -716,6 +718,7 @@ def instrument_app(
716718
name=HTTP_SERVER_REQUEST_DURATION,
717719
unit="s",
718720
description="Duration of HTTP server requests.",
721+
explicit_bucket_boundaries_advisory=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
719722
)
720723
active_requests_counter = meter.create_up_down_counter(
721724
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from opentelemetry import trace
2222
from opentelemetry.instrumentation._semconv import (
23+
HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
2324
OTEL_SEMCONV_STABILITY_OPT_IN,
2425
_OpenTelemetrySemanticConventionStability,
2526
_server_active_requests_count_attrs_new,
@@ -522,6 +523,10 @@ def test_flask_metrics_new_semconv(self):
522523
self.assertAlmostEqual(
523524
duration_s, point.sum, places=1
524525
)
526+
self.assertEqual(
527+
point.explicit_bounds,
528+
HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
529+
)
525530
histogram_data_point_seen = True
526531
if isinstance(point, NumberDataPoint):
527532
number_data_point_seen = True
@@ -552,8 +557,12 @@ def test_flask_metric_values(self):
552557
self.assertEqual(point.value, 0)
553558

554559
def _assert_basic_metric(
555-
self, expected_duration_attributes, expected_requests_count_attributes
560+
self,
561+
expected_duration_attributes,
562+
expected_requests_count_attributes,
563+
expected_histogram_explicit_bounds=None,
556564
):
565+
# pylint: disable=too-many-nested-blocks
557566
metrics_list = self.memory_metrics_reader.get_metrics_data()
558567
for resource_metric in metrics_list.resource_metrics:
559568
for scope_metrics in resource_metric.scope_metrics:
@@ -564,6 +573,11 @@ def _assert_basic_metric(
564573
expected_duration_attributes,
565574
dict(point.attributes),
566575
)
576+
if expected_histogram_explicit_bounds is not None:
577+
self.assertEqual(
578+
expected_histogram_explicit_bounds,
579+
point.explicit_bounds,
580+
)
567581
self.assertEqual(point.count, 1)
568582
elif isinstance(point, NumberDataPoint):
569583
self.assertDictEqual(
@@ -613,6 +627,7 @@ def test_basic_metric_success_new_semconv(self):
613627
self._assert_basic_metric(
614628
expected_duration_attributes,
615629
expected_requests_count_attributes,
630+
expected_histogram_explicit_bounds=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
616631
)
617632

618633
def test_basic_metric_nonstandard_http_method_success(self):
@@ -654,6 +669,7 @@ def test_basic_metric_nonstandard_http_method_success_new_semconv(self):
654669
self._assert_basic_metric(
655670
expected_duration_attributes,
656671
expected_requests_count_attributes,
672+
expected_histogram_explicit_bounds=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
657673
)
658674

659675
@patch.dict(
@@ -679,6 +695,7 @@ def test_basic_metric_nonstandard_http_method_allowed_success_new_semconv(
679695
self._assert_basic_metric(
680696
expected_duration_attributes,
681697
expected_requests_count_attributes,
698+
expected_histogram_explicit_bounds=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
682699
)
683700

684701
def test_metric_uninstrument(self):

0 commit comments

Comments
 (0)