Fix table formatter
It's possible for Prometheus to return multiple metrics, where each of them has different labels. Before this the client would fail with error when using the default table formatter, because of for example: Row has incorrect number of values, (actual) 8!=9 (expected) It was also possible to mismatch the label keys and values for some of the rows. This patch makes sure the label values are matched to the correct keys. It also makes sure it uses all labels and it fills missing values with empty strings for metrics, which don't have that label defined. Example table output: +------------+----------+------------+----------------+-------+ | group | __name__ | job | instance | value | +------------+----------+------------+----------------+-------+ | | up | sg-core | localhost:3000 | 1 | | production | up | prometheus | localhost:9090 | 1 | +------------+----------+------------+----------------+-------+ Example json output: [ { "__name__": "up", "group": "", "job": "sg-core", "instance": "localhost:3000", "value": "1" }, { "__name__": "up", "group": "production", "job": "prometheus", "instance": "localhost:9090", "value": "1" } ] Change-Id: Id0dfabf52fe0a21194c498f4aefa1bff1d3eeea9
This commit is contained in:
parent
6047081ec1
commit
ed042e2b16
@ -98,18 +98,14 @@ def format_labels(d: dict) -> str:
|
||||
|
||||
|
||||
def metrics2cols(m):
|
||||
cols = []
|
||||
# get all label keys
|
||||
cols = list(set().union(*(d.labels.keys() for d in m)))
|
||||
cols.append("value")
|
||||
fields = []
|
||||
first = True
|
||||
for metric in m:
|
||||
row = []
|
||||
row = [""] * len(cols)
|
||||
for key, value in metric.labels.items():
|
||||
if first:
|
||||
cols.append(key)
|
||||
row.append(value)
|
||||
if first:
|
||||
cols.append("value")
|
||||
row.append(metric.value)
|
||||
row[cols.index(key)] = value
|
||||
row[cols.index("value")] = metric.value
|
||||
fields.append(row)
|
||||
first = False
|
||||
return cols, fields
|
||||
|
Loading…
x
Reference in New Issue
Block a user