python prometheus_client guage metrics are not saved in prometheus

86 views
Skip to first unread message

Tommy b

unread,
Sep 8, 2022, 1:01:59 AM9/8/22
to Prometheus Users
```
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 
Reply all
Reply to author
Forward
0 new messages