python prometheus_client guage metrics are not saved in prometheus

Viewed 71
import prometheus_client as prom, Guage
import random
import time


pool={'pool_name': 'testing-prom-tool','member_name': 'promtest', 'mem_port': '443', 'mem_address': 'xx.xx.xx.xx', 'mem_state': 'down'}
# Create a metric to track time spent and requests made.
REQUEST_TIME = prom.Summary('request_processing_seconds', 'Time spent processing request')


# Decorate function with metric.
@REQUEST_TIME.time()
def process_request():
    time.sleep(1)


if __name__ == '__main__':
    #                          name documentation label names ###  I was able to populate all the keys from the dictionary using the pool. keys())
    f5_prom_test = prom.Guage('f5_test','f5_node_status',('pool_name','member_name','mem_port','mem_address','mem_state'))
    prom.start_http_server(1234)
While True:
   process_request()
        f5_prom_test.labels(pool.get('pool_name'),pool.get('member_name'),pool.get('mem_port'),pool.get('mem_address'),pool.get('mem_state'))
#f5_prom.labels(**pool), this works as well


I can see metrics are registered when I curl -K http://localhost:1234. Somehow the metrics are not saved in the Prometheus. I can't view the data whenever I stop the python script in grafana, and there is no historical data held in the Prometheus tsdb to view on the Prometheus web URL

curl -K http://localhost:1234

f5_test_created{mem_address="xx.xx.xx.xxx",mem_name="test-server",pool_name"=testpool",mem_port="5443",mem_state="down"} 1.658982617711136e+09

Here is my prometheus.yml for custom python Prometheus collector for pool data

  - job_name: 'python-exporter
    scrape_interval: 5s
    static_configs:
      - targets: ['hostname:1234']

I can view the data only when I use the python script; later, that data is not saved in Prometheus. I'm not using any custom registry , How to save registered data in /metrics to Prometheus using prometheus_client? I already changed my retention period of Prometheus tsdb

1 Answers

The data was saved to Prometheus; following link has more info

https://github.com/prometheus/client_python/discussions/837

I looked at the last 30 days of data on the Prometheus UI browser by using pool_details{name=~"testing-pool-purpose"}[30d], and I do see the data is saved to Prometheus; the reason for information is not shown in grafana is due to I used rate queries and instant on grafana table option which only shows the latest timestamp. When I turned off instant, I could see the data in grafana; I need to structure my queries better to get the data in grafana

Related