2
votes

With the help of Python request.get and json.loads functions, I was able to source the data from an URL. The data contain a list of instances where each one of them have metrics such as instance_id, status, etc.

My question is that is there any way I could upload those metrics to Prometheus? I looked into the pushgateway function but was not sure if that is the correct way of doing data pushing and storage.

My current effort to push data to Prometheus is the following:

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from prometheus_client import Summary

registry1 = CollectorRegistry()
registry2 = CollectorRegistry()

s = Summary('request_latency_seconds', 'Description of summary', registry=registry1)
s.observe(4.7)

g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry2)
g.set_to_current_time()

push_to_gateway('localhost:9091', job='batch_summary', registry=registry1)
push_to_gateway('localhost:9091', job='batch_gauge', registry=registry2)

However, I am not sure what metric type I should be pushing (Gauge, Summary or something else?)

Here is an example of the instance data I wish to push:

{'url': 'https://example.com', 
'created': '2017-09-17-time', 
'status_code': 200, 
'instance_start': '2018-09-17-time', }

The reason why I am pushing that data to Prometheus is that I wanted to use Grafana to get those data and visualize them.

An example Grafana graph would be: time as x-axis, number of instances with status-code: 200 as y-axis.

Any thoughts or help will be appreciated. Thank you!

1

1 Answers

0
votes

Since I did something similar, I can give you a few pointers. You can use pushgateway for "ephemeral and batch jobs to expose their metrics to Prometheus" as it says on there github page.

I think you need the summary metric, but to be sure please read the docs on the types of metrics on the prometheus docs page.

You did the first part, you exposed the metrics that you want through pushgateway, you can always check that it works through your web browser or with curl:

YOURIP:9091

If you can see some data on that port, you have to setup your configuration on the prometheus server.

You need to tell the prometheus server that is should scrape your nodes pushgateway's metrics. The file you're looking for is "prometheus.yml" its location depends on where you installed the prometheus server.

When you open it you should write something like this:

  - job_name: 'SomeJob-Name'
    static_configs:
      - targets: ['YourIPADDRESS:9091']
        labels:
          environment: 'prod'

After that restart your prometheus server and the data from your node should be visible on the path YourPrometheusIP:9090/targets.