From 0cd750fa5eca591efc7f1d76934567695fedd716 Mon Sep 17 00:00:00 2001 From: Wataru Manji Date: Mon, 13 Jun 2022 15:59:03 +0900 Subject: [PATCH] Add remote-write headers (#2701) Co-authored-by: Wataru Manji --- app/vmalert/remotewrite/remotewrite.go | 5 +++++ app/vmalert/remotewrite/remotewrite_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/app/vmalert/remotewrite/remotewrite.go b/app/vmalert/remotewrite/remotewrite.go index 6d94c9ba0..08e60a5a5 100644 --- a/app/vmalert/remotewrite/remotewrite.go +++ b/app/vmalert/remotewrite/remotewrite.go @@ -237,7 +237,12 @@ func (c *Client) send(ctx context.Context, data []byte) error { return fmt.Errorf("failed to create new HTTP request: %w", err) } + // RFC standard compliant headers req.Header.Set("Content-Encoding", "snappy") + req.Header.Set("Content-Type", "application/x-protobuf") + + // Prometheus compliant headers + req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0") if c.authCfg != nil { if auth := c.authCfg.GetAuthHeader(); auth != "" { diff --git a/app/vmalert/remotewrite/remotewrite_test.go b/app/vmalert/remotewrite/remotewrite_test.go index da7c6db84..ed2320262 100644 --- a/app/vmalert/remotewrite/remotewrite_test.go +++ b/app/vmalert/remotewrite/remotewrite_test.go @@ -86,6 +86,16 @@ func (rw *rwServer) handler(w http.ResponseWriter, r *http.Request) { rw.err(w, fmt.Errorf("header read error: Content-Encoding is not snappy (%q)", h)) } + h = r.Header.Get("Content-Type") + if h != "application/x-protobuf" { + rw.err(w, fmt.Errorf("header read error: Content-Type is not x-protobuf (%q)", h)) + } + + h = r.Header.Get("X-Prometheus-Remote-Write-Version") + if h != "0.1.0" { + rw.err(w, fmt.Errorf("header read error: X-Prometheus-Remote-Write-Version is not 0.1.0 (%q)", h)) + } + data, err := ioutil.ReadAll(r.Body) if err != nil { rw.err(w, fmt.Errorf("body read err: %w", err))