How set timestamp with pushGateway using Python

909 views
Skip to first unread message

Lukas Müller de Oliveira

unread,
Nov 4, 2021, 11:41:02 AM11/4/21
to Prometheus Users
Hi,

I'm using pushGateway to scrape some parts of my application that can't be scraped by Prometheus.
It is kind of a process that in the end I have a time series that I send to pushGateway.
From what I read in the documentation, despite sending the value of a metric in the push, I can't set the timestamp that I want it to be shown in Grafana(the timestamp of my time series), it is used the timestamp of when the push was sent.
How can i set a timestamp for each value?

#part of my code
registry = CollectorRegistry()
date = datetime.strptime('2021-11-04 10:45:32.259398', '%Y-%m-%d %H:%M:%S.%f')
g = Gauge('test_job_1', 'test_1', ['test','instance'], registry=registry)
g.labels('code_a','test').set(20)
g.set_time(date )#something like this, to set the date i want
push_to_gateway('localhost:9091', job='pushgateway', registry=registry)

Is there any way I can do this?

Brian Candler

unread,
Nov 4, 2021, 2:45:24 PM11/4/21
to Prometheus Users
You can't - and this is an anti-pattern for pushgateway.

pushgateway is for when a service is transient (e.g. a scheduled task which terminates), but it needs to leave its result lying around to be scraped at some point in the future.

You could consider using the remote_write protocol to write your data directly to prometheus, but this isn't well documented.  The simplest way would be to run a small prometheus instance on your remote node A, and get *that* to use the remote_write protocol to central node B.  Then you don't need to get involved with implementing the remote_write protocol yourself.
Reply all
Reply to author
Forward
0 new messages