But sometime there is situation where you need to store your own custom metrics on prometheus. In such case you have to write your own exporters which will exporters the data into Prometheus.
There is two way to exporting the data on prometheus:
1. Exporting to a Pushgateway
Here we metrics are getting the push to prometheus server. It not exposed over any URL or port. Internally it directly calls to Prometheus host and pushes the metrics to that.
2. Exposing over HTTP (on specific port)
In this option, Exporter will expose all data over HTTP in prometheus data format. Like below:
It exposes the data on a specific port e.g
Let’s take a very basic example, Suppose there is one script (Daemon service) is running with the name
myprocess.go. What do we do in real life to check if some script is running or not? We just hit
ps command like below:
ps -ef | grep 'myprocess'
Now we are going to do the same in our exporter. It will hit the same command and if script is running we will set value
We are going to use the Second option (Exposing over HTTP).
We going to use python_client. Just install it with below
pip install prometheus_client
We need webserver gateway, For which we are going to use WSGI.
Whenever we are writing exporters, We need to take care of metric type storage. Prometheus offered four types of metrics: Counter, Gauge, Summary, and Histogram. You can explore more here. Decide your metric type before pushing data.
We are going to run a webserver on
8000 port and exposing the result on
/metrics path. So to access metrics just hit
Complet sample code from my gist:
Add config in
- job_name: service_up_exporter scrape_interval: 2m scrape_timeout: 2m metrics_path: "/metrics" static_configs: - targets: ['exporterurl.com:8000/metrics']
Restart prometheus service:
sudo service prometheus restart
This is just a very basic example. It is a simple script to understand the way of writing exporters. You can write your custom exporters according to your use case.
Here we have successfully write exporter which will expose the metrics. To know more about pushgateway or HTTP expoorter visit here
In part - 4, I have explained how you can integrate Grafana with Prometheus.
Share you comments below :)