创建从一个Python列表纱布指标

问题描述 投票:0回答:2

工作Python列表上。下面是样本

result=[{'time': '00:00'}, {'app': 'dgn'}, {'avg': '7717'}, {'time': '00:00'}, {'app': 'pds'}, {'avg': '75.40223463687151'}] 

我创建从上面的数据的量规度量。尝试了以下,得到从上述数据度量:

class EventMetricCollector(object):
def avg_response_time_metric(self):


    metric = GaugeMetricFamily(
        'avg_response_time_ms',
        'average response time',
        labels=["time","app","avg"])

    for time, app, avg in result:
        metric.add_metric([time],[app],[avg])

    return metric

def collect(self):
    yield self_avg_response_time_metric()

然而,运行时,我得到这个错误

for time, app, avg in result:

ValueError: not enough values to unpack (expected 3, got 1) 

我预期的输出:

avg_response_time_metric{time="0",app=:"dgn",avg="7717"}
python metrics prometheus
2个回答
0
投票

Python是期待三个值从列表中解压,但只有一个被取出。

您需要通过列表zip()。尝试更换此for循环:

for time, app, avg in zip(result[::3], result[1::3], result[2::3]):

zips通过同时列表的相邻的三个要素。


0
投票

结合Alex的建议与您的代码片段:

class EventMetricCollector(object):
   def avg_response_time_metric(self):

       metric = GaugeMetricFamily(
           'avg_response_time_ms',
           'average response time',
           labels=["time","app","avg"])

       for time, app, avg in zip(result[::3], result[1::3], result[2::3]):
           metric.add_metric([time],[app],[avg])

       return metric

    def collect(self):
      yield self_avg_response_time_metric()
© www.soinside.com 2019 - 2024. All rights reserved.