Skip to content

Commit

Permalink
Rename choose_formatter back to choose_encoder
Browse files Browse the repository at this point in the history
Temporarily keep an alias so that choose_formatter will work for the
0.14.x release cycle.

Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
  • Loading branch information
csmarchbanks committed Apr 8, 2022
1 parent 041e0f5 commit ff2e7a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 13 additions & 3 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from urllib.request import (
build_opener, HTTPHandler, HTTPRedirectHandler, Request,
)
import warnings
from wsgiref.simple_server import make_server, WSGIRequestHandler, WSGIServer

from .openmetrics import exposition as openmetrics
Expand Down Expand Up @@ -97,10 +98,10 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
def _bake_output(registry, accept_header, accept_encoding_header, params, disable_compression):
"""Bake output for metrics output."""
# Choose the correct plain text format of the output.
formatter, content_type = choose_formatter(accept_header)
encoder, content_type = choose_encoder(accept_header)
if 'name[]' in params:
registry = registry.restricted_registry(params['name[]'])
output = formatter(registry)
output = encoder(registry)
headers = [('Content-Type', content_type)]
# If gzip encoding required, gzip the output.
if not disable_compression and gzip_accepted(accept_encoding_header):
Expand Down Expand Up @@ -237,7 +238,7 @@ def sample_line(line):
return ''.join(output).encode('utf-8')


def choose_formatter(accept_header: str) -> Tuple[Callable[[CollectorRegistry], bytes], str]:
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':
Expand All @@ -246,6 +247,15 @@ def choose_formatter(accept_header: str) -> Tuple[Callable[[CollectorRegistry],
return generate_latest, CONTENT_TYPE_LATEST


def choose_formatter(accept_header: str) -> Tuple[Callable[[CollectorRegistry], bytes], str]:
warnings.warn(
"choose_formatter is deprecated and will be removed in 0.15.0, please use choose_encoder instead",
DeprecationWarning,
stacklevel=2
)
return choose_encoder(accept_header)


def gzip_accepted(accept_encoding_header: str) -> bool:
accept_encoding_header = accept_encoding_header or ''
for accepted in accept_encoding_header.split(','):
Expand Down
20 changes: 18 additions & 2 deletions tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import threading
import time
import unittest
import warnings

import pytest

Expand All @@ -12,9 +13,10 @@
)
from prometheus_client.core import GaugeHistogramMetricFamily, Timestamp
from prometheus_client.exposition import (
basic_auth_handler, default_handler, MetricsHandler,
passthrough_redirect_handler,
basic_auth_handler, choose_encoder, choose_formatter, default_handler,
MetricsHandler, passthrough_redirect_handler,
)
import prometheus_client.openmetrics.exposition as openmetrics


class TestGenerateText(unittest.TestCase):
Expand Down Expand Up @@ -460,5 +462,19 @@ def test_histogram_metric_families(MetricFamily, registry, buckets, sum_value, e
_expect_metric_exception(registry, error)


def test_choose_encoder():
assert choose_encoder(None) == (generate_latest, CONTENT_TYPE_LATEST)
assert choose_encoder(CONTENT_TYPE_LATEST) == (generate_latest, CONTENT_TYPE_LATEST)
assert choose_encoder(openmetrics.CONTENT_TYPE_LATEST) == (openmetrics.generate_latest, openmetrics.CONTENT_TYPE_LATEST)


def test_choose_formatter():
with warnings.catch_warnings(record=True) as w:
assert choose_formatter('') == (generate_latest, CONTENT_TYPE_LATEST)
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
assert "choose_formatter is deprecated" in str(w[-1].message)


if __name__ == '__main__':
unittest.main()

0 comments on commit ff2e7a4

Please sign in to comment.