mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 13:44:05 -08:00
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.
This commit is contained in:
parent
1e2f03f668
commit
b5163351bf
|
@ -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
|
||||
```
|
||||
|
|
|
@ -21,14 +21,24 @@ 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 main() {
|
||||
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
|
||||
}
|
||||
|
||||
var req remote.WriteRequest
|
||||
if err := proto.Unmarshal(reqBuf, &req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -40,38 +50,7 @@ func (server *server) Write(ctx context.Context, req *remote.WriteRequest) (*rem
|
|||
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) {
|
||||
reqBuf, err := ioutil.ReadAll(snappy.NewReader(r.Body))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var req remote.WriteRequest
|
||||
if err := proto.Unmarshal(reqBuf, &req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := s.Write(context.Background(), &req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue