-
Notifications
You must be signed in to change notification settings - Fork 90
/
remotewrite_server.go
41 lines (34 loc) · 1.24 KB
/
remotewrite_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package utils
import (
"math"
"net/http"
"net/http/httptest"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/util"
)
// RemoteWriteRequest wraps the received logs remote write request that is received.
type RemoteWriteRequest struct {
TenantID string
Request logproto.PushRequest
}
// NewRemoteWriteServer creates and starts a new httpserver.Server that can handle remote write request. When a request is handled,
// the received entries are written to receivedChan, and status is responded.
func NewRemoteWriteServer(receivedChan chan RemoteWriteRequest, status int) *httptest.Server {
server := httptest.NewServer(createServerHandler(receivedChan, status))
return server
}
func createServerHandler(receivedReqsChan chan RemoteWriteRequest, receivedOKStatus int) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
// Parse the request
var pushReq logproto.PushRequest
if err := util.ParseProtoReader(req.Context(), req.Body, int(req.ContentLength), math.MaxInt32, &pushReq, util.RawSnappy); err != nil {
rw.WriteHeader(500)
return
}
receivedReqsChan <- RemoteWriteRequest{
TenantID: req.Header.Get("X-Scope-OrgID"),
Request: pushReq,
}
rw.WriteHeader(receivedOKStatus)
}
}