Merge pull request #4465 from ryanpetrello/json-metrics

add support for Accept:application/json to /api/v2/metrics

Reviewed-by: https://github.com/softwarefactory-project-zuul[bot]
This commit is contained in:
softwarefactory-project-zuul[bot]
2019-08-12 20:26:16 +00:00
committed by GitHub
5 changed files with 20 additions and 14 deletions

View File

@@ -1,5 +1,3 @@
from prometheus_client.parser import text_string_to_metric_families
from awxkit.api.resources import resources
from . import base
from . import page
@@ -8,14 +6,8 @@ from . import page
class Metrics(base.Base):
def get(self, **query_parameters):
request = self.connection.get(self.endpoint, query_parameters)
self.page_identity(request, ignore_json_errors=True)
parsed_metrics = text_string_to_metric_families(request.text)
data = {}
for family in parsed_metrics:
for sample in family.samples:
data[sample[0]] = {"labels": sample[1], "value": sample[2]}
request.json = lambda: data
request = self.connection.get(self.endpoint, query_parameters,
headers={'Accept': 'application/json'})
return self.page_identity(request)

View File

@@ -172,7 +172,7 @@ class Page(object):
resp.status_code = 200
return cls(r=resp)
def page_identity(self, response, request_json=None, ignore_json_errors=False):
def page_identity(self, response, request_json=None):
"""Takes a `requests.Response` and
returns a new __item_class__ instance if the request method is not a get, or returns
a __class__ instance if the request path is different than the caller's `endpoint`.
@@ -191,7 +191,7 @@ class Page(object):
data = response.json()
except ValueError as e: # If there was no json to parse
data = dict()
if (response.text and not ignore_json_errors) or response.status_code not in (200, 202, 204):
if response.text or response.status_code not in (200, 202, 204):
text = response.text
if len(text) > 1024:
text = text[:1024] + '... <<< Truncated >>> ...'

View File

@@ -1,7 +1,6 @@
PyYAML>=5.1
cryptography
flake8
prometheus-client
python-dateutil
requests
termcolor