2

I have been trying to call remote_write() function of Prometheus via java/python/postman. I have configured a Prometheus server with remote receiver configuration and I have another Prometheus agent running from where I am trying to make an API call to do remote_write().

I have been getting the below error and I believe we need to do a snappy compression and call the APIs?

Any samples or help on how we can call this /api/v1/write API via java or python or maybe via postman by doing a snappy compression ?

Sample I am trying:

curl --location --request POST 'http://<prometheus-host>/api/v1/write' \
--header 'Content-Type: text/plain' \
--data-raw 'metrics_app_20221201091829_0000_driver_BlockManager_memory_diskSpaceUsed_MB_Number{type="gauges"} 0

Error:

snappy: corrupt input
Shane
  • 588
  • 6
  • 20
  • 1
    The docs for [``](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write) reference a Go sample [`remote_storage`](https://github.com/prometheus/prometheus/tree/release-2.40/documentation/examples/remote_storage) that defines a [`server.go`](https://github.com/prometheus/prometheus/blob/release-2.40/documentation/examples/remote_storage/example_write_adapter/server.go) – DazWilkin Dec 20 '22 at 01:29
  • 1
    The server uses [`DecodeWriteRequest`](https://github.com/prometheus/prometheus/blob/ab239ac5d43f6c1068f0d05283a0544576aaecf8/storage/remote/codec.go#L605) to read then snappy decode and then protobuf unmarshal incoming writes. Hopefully that provides an example for your implementation. – DazWilkin Dec 20 '22 at 01:29
  • I saw the server.go code and the samples being added and it seems like its to configure a Prometheus server to accept remote writes. Is there a sample or postman call to do a remote write to a Prometheus server with snappy as a compression codec? – Shane Dec 20 '22 at 04:20
  • 1
    Right, it's an implementation for what your code should produce. You need to invert the flow: marshal the protobuf and then snappy compress the result in your code. You could write that in Go using that as the basis. And you can use the same protos as the basis for Java|Python implementations and find a snappy implementation for those languages. You can then test your implementation with the Go sample. I think you won't be able to do this solely using Postman. – DazWilkin Dec 20 '22 at 04:30

1 Answers1

0

snappy is a kind of compression.
try this (python code):

import snappy
import requests

requests.post(
    "http://<prometheus-host>/api/v1/write",
    auth=(username, password),
    data=snappy.compress(payload),
    headers=headers,
)

* should probably pip install snappy

yonatan
  • 595
  • 1
  • 4
  • 18