Search code examples
pythonclientprometheus

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. Each payload has 5 metrics so I am adding timestamp to it. It successfully publishes it to the http listener but the metrics never expires. 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

Solution

  • Making the self.page_views_metrics as local variable to collect method solved the problem.

    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