简体   繁体   English

时间戳未过期的普罗米修斯客户端自定义指标

[英]prometheus client custom metrics with timestamp not expiring

I have written below custom collector which pulls the data from a rest api and adds page view metrics.我在下面写了自定义收集器,它从 rest api 中提取数据并添加页面查看指标。 Each payload has 5 metrics so I am adding timestamp to it.每个有效负载都有 5 个指标,因此我为其添加了时间戳。 It successfully publishes it to the http listener but the metrics never expires.它成功地将其发布到 http 侦听器,但指标永不过期。 How can I add the expiry to these metrics?如何将到期时间添加到这些指标?

#!/usr/bin/env python3

import argparse
import re
import sys
import time
import datetime
import urllib3
import requests
import aniso8601
import pytz
import json

from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY

class HttpCollector(object):
    def __init__(self):
        self.url = "my_endpoint"
        self.username = 'my_userid'
        self.password = 'my_pass'
        self.labels = ['app_name', 'url_host' ]
        self.page_views_metrics = GaugeMetricFamily('pageviews', 'Page Views', labels=self.labels)

    def collect(self):
        headers = {'Accept': '*/*', }
        auth = (self.username, self.password)
        urllib3.disable_warnings()
        result = requests.get(self.url, headers=headers, auth=auth, timeout=60, verify=False)
        if not result.ok:
            # Log error
            print("No results")
            return
        json_result = result.json()
        for record in json_result['records']:
            timestamp_epoch = covert_date_to_epoch(record["timestamp'])
            label_values = ["testapp", "testhost"]
            self.page_views_metrics.add_metric(label_values, record["page_views"], timestamp=timestamp_epoch)
        yield self.page_views_metrics

Making the self.page_views_metrics as local variable to collect method solved the problem.将 self.page_views_metrics 作为局部变量来收集方法解决了这个问题。

import re
import sys
import time
import datetime
import urllib3
import requests
import aniso8601
import pytz
import json

from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY

class HttpCollector(object):
    def __init__(self):
        self.url = "my_endpoint"
        self.username = 'my_userid'
        self.password = 'my_pass'
        self.labels = ['app_name', 'url_host' ]

    def collect(self):
        headers = {'Accept': '*/*', }
        auth = (self.username, self.password)
        urllib3.disable_warnings()
        result = requests.get(self.url, headers=headers, auth=auth, timeout=60, verify=False)
        if not result.ok:
            # Log error
            print("No results")
            return
        json_result = result.json()
        page_views_metrics = GaugeMetricFamily('pageviews', 'Page Views', labels=self.labels)
        for record in json_result['records']:
            timestamp_epoch = covert_date_to_epoch(record["timestamp'])
            label_values = ["testapp", "testhost"]
            page_views_metrics.add_metric(label_values, record["page_views"], timestamp=timestamp_epoch)
        yield page_views_metrics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM