-
from remote_pb2 import WriteRequest
from types_pb2 import TimeSeries, Label, Labels, Sample
import httpx
import snappy
import time
#http://web.archive.org/web/20220524151753/https://www.dreamincode.net/forums/blog/324/entry-5188-prometheus-remote-write-with-non-go-languages/
#https://gist.github.com/robskillington/fb82ee5c737b79a3bc891df3dce7a9aa
#https://gist.github.com/nourspace/04264118a5c4c0bffe5a8724b486ab4d#file-write-py
#curl -vvX POST http://localhost:9090/api/v1/write
write_request = WriteRequest()
time_series = write_request.timeseries.add()
label = time_series.labels.add()
label.name = "__name__"
label.value = "metric_name"
label = time_series.labels.add()
label.name = "some_thing"
label.value = "some_label"
sample = time_series.samples.add()
sample.value = 1
sample.timestamp = int(time.time())
data = snappy.compress(write_request.SerializeToString())
res = httpx.post("http://localhost:9090/api/v1/write",content=data)
pass It really can't be this difficult to throw some JSON or other data at Prometheus |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think your issue is Note that Prometheus acting as a remote write receiver is still a relatively new feature. Depending how delayed data you wish to insert, you might be interested in the experimental option |
Beta Was this translation helpful? Give feedback.
I think your issue is
sample.timestamp = int(time.time())
-- you're using a Unix epoch timestamp in seconds, whereas Prometheus expects a value in milliseconds (see the spec at https://prometheus.io/docs/concepts/remote_write_spec/), so Prometheus sees this as a very old sample and hence gives the out-of-order error.Note that Prometheus acting as a remote write receiver is still a relatively new feature. Depending how delayed data you wish to insert, you might be interested in the experimental option
out_of_order_time_window
(see https://prometheus.io/docs/prometheus/latest/configuration/configuration/#tsdb).