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

application/openmetrics-text encoding cannot be selected in asgi app using the Accept header #861

Closed
jezekra1 opened this issue Nov 30, 2022 · 2 comments
Labels

Comments

@jezekra1
Copy link
Contributor

jezekra1 commented Nov 30, 2022

The openmetrics format cannot be selected using the Accept: application/openmetrics-text header, because there is the extra header key in the beginning in asgi.py:make_asgi_app which I believe is incorrect. Compare with the accept_encoding_header just below.
https://github.com/prometheus/client_python/blob/master/prometheus_client/asgi.py#L15:

         # Accept header key here:
        accept_header = "Accept: " + ",".join([
            value.decode("utf8") for (name, value) in scope.get('headers')
            if name.decode("utf8").lower() == 'accept'
        ])
        # No header key here:
        accept_encoding_header = ",".join([
            value.decode("utf8") for (name, value) in scope.get('headers')
            if name.decode("utf8").lower() == 'accept-encoding'
        ])

The condition in exposition.py is thus comparing the full header:

accepted.split(';')[0].strip() results to Accept: application/openmetrics-text.

def choose_encoder(accept_header: str) -> Tuple[Callable[[CollectorRegistry], bytes], str]:
    accept_header = accept_header or ''
    for accepted in accept_header.split(','):
        if accepted.split(';')[0].strip() == 'application/openmetrics-text':
            return (openmetrics.generate_latest,
                    openmetrics.CONTENT_TYPE_LATEST)
    return generate_latest, CONTENT_TYPE_LATEST
@csmarchbanks
Copy link
Member

Good find! Are you interested in creating a PR?

@csmarchbanks
Copy link
Member

Fixed in #864

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

No branches or pull requests

2 participants