Merge pull request #2053 from prometheus/fix-remote-write-example

Simplify and fix remote write example
This commit is contained in:
Julius Volz 2016-10-05 17:53:45 +02:00 committed by GitHub
commit ee8e8d1d31
2 changed files with 24 additions and 38 deletions

View file

@ -7,11 +7,18 @@ To use it:
``` ```
go build go build
remote_storage ./remote_storage
``` ```
...and then run Prometheus as: ...and then add the following to your `prometheus.yml`:
```yaml
remote_write:
url: "http://localhost:1234/receive"
```
Then start Prometheus:
``` ```
./prometheus -storage.remote.address=localhost:1234 ./prometheus
``` ```

View file

@ -21,43 +21,15 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"golang.org/x/net/context"
"github.com/prometheus/prometheus/storage/remote" "github.com/prometheus/prometheus/storage/remote"
) )
type server struct{}
func (server *server) Write(ctx context.Context, req *remote.WriteRequest) (*remote.WriteResponse, error) {
for _, ts := range req.Timeseries {
m := make(model.Metric, len(ts.Labels))
for _, l := range ts.Labels {
m[model.LabelName(l.Name)] = model.LabelValue(l.Value)
}
fmt.Println(m)
for _, s := range ts.Samples {
fmt.Printf(" %f %d\n", s.Value, s.TimestampMs)
}
}
return &remote.WriteResponse{}, nil
}
func main() { func main() {
http.Handle("/push", AppenderHandler(&server{})) http.HandleFunc("/receive", func(w http.ResponseWriter, r *http.Request) {
http.ListenAndServe(":1234", nil)
}
type WriteServer interface {
Write(context.Context, *remote.WriteRequest) (*remote.WriteResponse, error)
}
// AppenderHandler returns a http.Handler that accepts proto encoded samples.
func AppenderHandler(s WriteServer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqBuf, err := ioutil.ReadAll(snappy.NewReader(r.Body)) reqBuf, err := ioutil.ReadAll(snappy.NewReader(r.Body))
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -67,11 +39,18 @@ func AppenderHandler(s WriteServer) http.Handler {
return return
} }
if _, err := s.Write(context.Background(), &req); err != nil { for _, ts := range req.Timeseries {
http.Error(w, err.Error(), http.StatusInternalServerError) m := make(model.Metric, len(ts.Labels))
return for _, l := range ts.Labels {
} m[model.LabelName(l.Name)] = model.LabelValue(l.Value)
}
fmt.Println(m)
w.WriteHeader(http.StatusOK) for _, s := range ts.Samples {
fmt.Printf(" %f %d\n", s.Value, s.TimestampMs)
}
}
}) })
http.ListenAndServe(":1234", nil)
} }