From b5163351bfe88fed99ad0598a91d58f15fa7d939 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Wed, 5 Oct 2016 17:46:51 +0200 Subject: [PATCH] Simplify and fix remote write example After removing gRPC, this can be simplified again. Also, the configuration for the remote storage moved from flags to the config file. --- .../examples/remote_storage/README.md | 13 +++-- .../examples/remote_storage/server.go | 49 ++++++------------- 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/documentation/examples/remote_storage/README.md b/documentation/examples/remote_storage/README.md index 91dd09632..483bb22dc 100644 --- a/documentation/examples/remote_storage/README.md +++ b/documentation/examples/remote_storage/README.md @@ -7,11 +7,18 @@ To use it: ``` 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 ``` diff --git a/documentation/examples/remote_storage/server.go b/documentation/examples/remote_storage/server.go index ea1e1ea16..54a09321c 100644 --- a/documentation/examples/remote_storage/server.go +++ b/documentation/examples/remote_storage/server.go @@ -21,43 +21,15 @@ import ( "github.com/golang/protobuf/proto" "github.com/golang/snappy" "github.com/prometheus/common/model" - "golang.org/x/net/context" "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() { - http.Handle("/push", AppenderHandler(&server{})) - 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) { + http.HandleFunc("/receive", func(w http.ResponseWriter, r *http.Request) { reqBuf, err := ioutil.ReadAll(snappy.NewReader(r.Body)) if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -67,11 +39,18 @@ func AppenderHandler(s WriteServer) http.Handler { return } - if _, err := s.Write(context.Background(), &req); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + 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) - w.WriteHeader(http.StatusOK) + for _, s := range ts.Samples { + fmt.Printf(" %f %d\n", s.Value, s.TimestampMs) + } + } }) + + http.ListenAndServe(":1234", nil) }