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

Route included in metrics auto-instrumentation ASGI #1316

Open
paulorpdlmillicom opened this issue Sep 6, 2022 · 3 comments
Open

Route included in metrics auto-instrumentation ASGI #1316

paulorpdlmillicom opened this issue Sep 6, 2022 · 3 comments

Comments

@paulorpdlmillicom
Copy link

There is a way to get the http.route included in autoinstrumented metrics for FastAPI?

Looking the following docs: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md#parameterized-attributes

But the implementation of the recommended attributes doesn't include the http.route attribute:

# List of recommended metrics attributes
_duration_attrs = [
SpanAttributes.HTTP_METHOD,
SpanAttributes.HTTP_HOST,
SpanAttributes.HTTP_SCHEME,
SpanAttributes.HTTP_STATUS_CODE,
SpanAttributes.HTTP_FLAVOR,
SpanAttributes.HTTP_SERVER_NAME,
SpanAttributes.NET_HOST_NAME,
SpanAttributes.NET_HOST_PORT,
]
_active_requests_count_attrs = [
SpanAttributes.HTTP_METHOD,
SpanAttributes.HTTP_HOST,
SpanAttributes.HTTP_SCHEME,
SpanAttributes.HTTP_FLAVOR,
SpanAttributes.HTTP_SERVER_NAME,
]

def _parse_active_request_count_attrs(req_attrs):
active_requests_count_attrs = {}
for attr_key in _active_requests_count_attrs:
if req_attrs.get(attr_key) is not None:
active_requests_count_attrs[attr_key] = req_attrs[attr_key]
return active_requests_count_attrs
def _parse_duration_attrs(req_attrs):
duration_attrs = {}
for attr_key in _duration_attrs:
if req_attrs.get(attr_key) is not None:
duration_attrs[attr_key] = req_attrs[attr_key]
return duration_attrs

Also the implementation of ASGI instrumentation, parse attributes for both metrics (duration and active_requests) after the addition of additional attributes (where the http.routes comes):

attributes.update(additional_attributes)
active_requests_count_attrs = _parse_active_request_count_attrs(
attributes
)
duration_attrs = _parse_duration_attrs(attributes)

Is your feature request related to a problem?
Maybe the inclusion of path or route attributes, could be usefull for metrics analysis.

Describe the solution you'd like
Move the additional attributes update in the ASGI instrumentation, after the call of recommended attributes for metrics.

Describe alternatives you've considered
Manually instrumentation of all paths in the API.

@sk-
Copy link
Contributor

sk- commented Sep 9, 2022

That feature would be great! However, it's not straightforward to do it, as that label should have low multiplicity. Unfortunately the data to build the target is not standard and different frameworks expose different data in the asgi scope.

For fastapi/starlette that target can be obtained as:

scope["route"].path_format

In Sanic I couldn't find any data that could serve generating the target. Below is a print of what the scope looks like in sanic.

{
'type': 'http', 
'asgi': {'version': '3.0', 'spec_version': '2.3'}, 
'http_version': '1.1', 
'server': ('127.0.0.1', 8000), 
'client': ('127.0.0.1', 57852), 
'scheme': 'http', 
'root_path': '', 
'headers': [(b'host', b'127.0.0.1:8000'), (b'user-agent', b'curl/7.64.1'), (b'accept', b'*/*')], 
'method': 'GET', 
'path': '/p/123', 
'raw_path': b'/p/123', 
'query_string': b''
}

@lzchen @srikanthccv Would you accept a change that computes the target just for fastapi/starlette?

@srikanthccv
Copy link
Member

Does path_format return the desired value and low cardinal? If so, feel free to send a patch. The target full URL is already available (example here used in traces) but it doesn't satisfy the cardinality requirements so we haven't added that for metric labels.

@sk-
Copy link
Contributor

sk- commented Sep 10, 2022

@srikanthccv #1323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants