Prometheus. Pushing metrics.
Prometheus by default prefers
a pull based metrics collection.
That means, Prometheus will collect
metrics at the certain interval
from your systems and store it in its data base.
But, once in awhile, here comes a situation
where, for example, a batch job may not
exist long enough to be scraped.
This job can, instead, push
that metrics to the push gateway.
As you can see above, the push gateway
is also a component from the Prometheus project
and can be installed separately.
Below, you see a diagram of an app
which pushes metrics to the push gateway
which then get scraped, like Prometheus,
and pulls the metric out of the push gateway.
Sometimes metrics cannot be scraped.
Think of batch jobs, service that
are not reachable due to the NAT, firewall.
Pushgateway is used as an intermediary service
which allow you to push the metrics.
There are some pitfalls, however.
Most of the times this is a single instance,
so this results in a single point of failure.
Prometheus's automatic instance
health monitoring is not possible.
So make sure, you have the right
to use case for the Pushgateway.
The Pushgateway never forgets the metrics,
unless they are deleted via the api.
Here you can see the simple example,
so this way you can "curl -X delete",
and then you select the metric you want to delete.
There is actually one valid use case of the Pushgateway.
This use case is the service-level batch jobs
and not related to the specific machine.
If a NAT gateway, or NAT, or both
to firewall is blocking you from using the pull mechanism,
move the Prometheus server on the same network.
Here, you can see simple Python example,
so we import to Prometheus clients,
import collection registry, gauge, and push to gateway.
Below, in the "push_the_gateway",
you have to specify where the gateway exists,
so in this case local host, what's the job name,
and the registry you wanna push to.
The Pushgateway functions take
also a grouping keys, so you can do "push_to_gateway",
this replaces metrics with the same grouping key.
You can "pushadd_to_gateway", this only
replaces metrics with the same name and grouping key,
but you can also do a "delete_from_gateway",
and this deletes metrics
with the given job and grouping key.
Pushing metrics with go.
So, here you another example, like
I discussed in the previous slides
with the Python example, so, as you can see,
import "prometheus/push" and then
you define the "gatewayUrl".
So, this a little Go example will
push the "troughput in Mbps" to the Pushgateway.
I hope this gives you a better understanding
of the push mechanism in Prometheus.
Make sure to check your use case before using it.