Skip to content

Commit

Permalink
add metric attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
GonzaloGuasch authored and GonzaloGuasch committed Jun 19, 2024
1 parent 0a231e5 commit 3e0ba7d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `opentelemetry-instrumentation-flask` Add http route to metric attributes
([#2506](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2506))
- `opentelemetry-sdk-extension-aws` Register AWS resource detectors under the
`opentelemetry_resource_detector` entry point
([#2382](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2382))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +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 (
_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
Expand All @@ -268,6 +267,9 @@ def response_hook(span: Span, status: str, response_headers: List):
from opentelemetry.instrumentation.utils import _start_internal_or_server_span
from opentelemetry.metrics import get_meter
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.metrics.http_metrics import (
HTTP_SERVER_REQUEST_DURATION,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.util.http import (
get_excluded_urls,
Expand All @@ -282,6 +284,7 @@ def response_hook(span: Span, status: str, response_headers: List):
_ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key"
_ENVIRON_REQCTX_REF_KEY = "opentelemetry-flask.reqctx_ref_key"
_ENVIRON_TOKEN = "opentelemetry-flask.token"
_ENVIRON_REQUEST_ROUTE_KEY = "request-route_key"

_excluded_urls_from_env = get_excluded_urls("FLASK")

Expand Down Expand Up @@ -344,6 +347,11 @@ def _start_response(status, response_headers, *args, **kwargs):
excluded_urls is None
or not excluded_urls.url_disabled(flask.request.url)
):
if flask.request.url_rule:
wrapped_app_environ[_ENVIRON_REQUEST_ROUTE_KEY] = str(
flask.request.url_rule
)

span = flask.request.environ.get(_ENVIRON_SPAN_KEY)

propagator = get_global_response_propagator()
Expand Down Expand Up @@ -386,13 +394,25 @@ def _start_response(status, response_headers, *args, **kwargs):
duration_attrs_old = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.DEFAULT
)

if wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY, None):
duration_attrs_old[SpanAttributes.HTTP_ROUTE] = (
wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY)
)

duration_histogram_old.record(
max(round(duration_s * 1000), 0), duration_attrs_old
)
if duration_histogram_new:
duration_attrs_new = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.HTTP
)

if wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY, None):
duration_attrs_new[SpanAttributes.HTTP_ROUTE] = (
wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY)
)

duration_histogram_new.record(
max(duration_s, 0), duration_attrs_new
)
Expand Down Expand Up @@ -553,7 +573,7 @@ def __init__(self, *args, **kwargs):
duration_histogram_new = None
if _report_new(_InstrumentedFlask._sem_conv_opt_in_mode):
duration_histogram_new = meter.create_histogram(
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
name=HTTP_SERVER_REQUEST_DURATION,
unit="s",
description="measures the duration of the inbound HTTP request",
)
Expand Down Expand Up @@ -684,7 +704,7 @@ def instrument_app(
duration_histogram_new = None
if _report_new(sem_conv_opt_in_mode):
duration_histogram_new = meter.create_histogram(
name=_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
name=HTTP_SERVER_REQUEST_DURATION,
unit="s",
description="measures the duration of the inbound HTTP request",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
_SPAN_ATTRIBUTES_ERROR_TYPE,
OTEL_SEMCONV_STABILITY_OPT_IN,
_OpenTelemetrySemanticConventionStability,
_server_active_requests_count_attrs_new,
Expand All @@ -40,6 +39,7 @@
NumberDataPoint,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.util.http import (
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_internal_error_new_semconv(self):
SpanAttributes.URL_PATH: "/hello/500",
SpanAttributes.HTTP_ROUTE: "/hello/<int:helloid>",
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
_SPAN_ATTRIBUTES_ERROR_TYPE: "500",
ERROR_TYPE: "500",
SpanAttributes.URL_SCHEME: "http",
}
)
Expand All @@ -405,7 +405,7 @@ def test_internal_error_both_semconv(self):
{
SpanAttributes.URL_PATH: "/hello/500",
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 500,
_SPAN_ATTRIBUTES_ERROR_TYPE: "500",
ERROR_TYPE: "500",
SpanAttributes.URL_SCHEME: "http",
}
)
Expand Down Expand Up @@ -459,6 +459,7 @@ def test_exclude_lists_from_explicit(self):
self.assertEqual(len(span_list), 1)

def test_flask_metrics(self):
_server_duration_attrs_old.append("http.route")
start = default_timer()
self.client.get("/hello/123")
self.client.get("/hello/321")
Expand Down Expand Up @@ -570,6 +571,7 @@ def test_basic_metric_success(self):
self.client.get("/hello/756")
expected_duration_attributes = {
"http.method": "GET",
"http.route": "/hello/<int:helloid>",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
Expand Down Expand Up @@ -597,6 +599,7 @@ def test_basic_metric_success_new_semconv(self):
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,
}
Expand Down

0 comments on commit 3e0ba7d

Please sign in to comment.