Skip to content

Commit

Permalink
Get prometheus remote write version from header (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
boernd committed Jun 13, 2018
1 parent 092675f commit 885b2bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 0 additions & 5 deletions README.md
Expand Up @@ -8,11 +8,6 @@ Example Prometheusbeat configuration:
prometheusbeat:
listen: ":8080"
context: "/prometheus"
# The storage request format had a breaking change starting with Prometheus 1.7.
# Set the version accordingly.
# 1: Prometheus < 1.7
# 2: Prometheus >= 1.7
version: 2
[...]
```
Expand Down
2 changes: 0 additions & 2 deletions config/config.go
Expand Up @@ -6,11 +6,9 @@ package config
type Config struct {
Listen string `config:"listen"`
Context string `config:"context"`
Version int `config:"version`
}

var DefaultConfig = Config{
Listen: ":8080",
Context: "/prometheus",
Version: 2,
}
26 changes: 19 additions & 7 deletions prometheus/prometheus.go
Expand Up @@ -7,14 +7,14 @@ import (
"net/http"
"strings"

"github.com/elastic/beats/libbeat/common"

"github.com/golang/protobuf/proto"
"github.com/golang/snappy"

"github.com/prometheus/prometheus/prompb"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
version "github.com/hashicorp/go-version"
"github.com/infonova/prometheusbeat/config"
"github.com/prometheus/prometheus/prompb"
)

type PrometheusServer struct {
Expand All @@ -39,10 +39,22 @@ func (promSrv *PrometheusServer) Start(events chan common.MapStr) {

func (promSrv *PrometheusServer) handlePrometheus(w http.ResponseWriter, r *http.Request) {

v := r.Header.Get("X-Prometheus-Remote-Write-Version")
//No header indicates old prometheus version
if len(v) == 0 {
v = "0.0.0"
}

baseVer, _ := version.NewVersion("0.1.0")
reqVer, err := version.NewVersion(v)
if err != nil {
logp.Err(strings.Join([]string{"wrong prometheus remote write version:", v}, " "))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

var reqBuf []byte
var err error
// Handle breaking change between Prometheus versions
if promSrv.config.Version == 1 {
if reqVer.LessThan(baseVer) {
reqBuf, err = ioutil.ReadAll(snappy.NewReader(r.Body))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down

0 comments on commit 885b2bb

Please sign in to comment.