Skip to content

Commit

Permalink
Merge pull request prometheus#2053 from prometheus/fix-remote-write-e…
Browse files Browse the repository at this point in the history
…xample

Simplify and fix remote write example
  • Loading branch information
juliusv authored Oct 5, 2016
2 parents 1e2f03f + b516335 commit ee8e8d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
13 changes: 10 additions & 3 deletions documentation/examples/remote_storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
./prometheus -storage.remote.address=localhost:1234
Then start Prometheus:
```
./prometheus
```
49 changes: 14 additions & 35 deletions documentation/examples/remote_storage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}

0 comments on commit ee8e8d1

Please sign in to comment.