Hello,

I currently intend to use prometheus python client library to push custom 
metrics which are being generated through ML model inference. for now, in 
the development environment, using following python code: (for now code 
just push random values generated).
```
import random
import time
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

class MetricsCollector:
def __init__(self, endpoint):
self.registry = CollectorRegistry()
self.gauges = {}
self.endpoint = endpoint
def add_metric(self, name):
gauge = Gauge(name, 'Description of the metric', registry=self.registry)
self.gauges[name] = gauge
def set_metric_value(self, name, value):
gauge = self.gauges.get(name)
if gauge is not None:
gauge.set(value)
def collect_metrics(self):
while True:
for name, gauge in self.gauges.items():
# Generate random value using random library
value = random.random()
self.set_metric_value(name, value) 
# Push metrics to the Prometheus endpoint
push_to_gateway(self.endpoint, job='my_job', registry=self.registry)
time.sleep(5) # Collect metrics every 5 seconds

# Example usage
if __name__ == '__main__':
collector = MetricsCollector('http://0.0.0.0:9999') # Replace with your 
Prometheus endpoint
# Add metrics
collector.add_metric('metric1')
collector.add_metric('metric2')
# Start collecting and pushing metrics
collector.collect_metrics()
```
And following default template for YML file:
```
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default 
is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is 
every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 
'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

```
With initialising command as as :
```
./prometheus --config.file="prometheus.yml" 
--web.listen-address="0.0.0.0:9999"
```

Then too it is throwing error:
```
File "/root/miniconda3/envs/mlops/lib/python3.7/urllib/request.py", line 
649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
```

I tried sending api request to check whether the url is accessible with 
following python code:
```
import requests
url = "http://0.0.0.0:9999";
payload = {}
headers = {}
r = requests.request("GET", url, headers=headers, data=payload)
print(r)
```

Which gives output: 
```
<Response [200]>
```
which means that URL atleast exist.

What can be possible problem associated with this though.

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/74758d83-fc33-48e5-a426-1c148cf4f3c2n%40googlegroups.com.

Reply via email to