Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs PushRequest data. #3178

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions pkg/distributor/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math"
"net/http"

"github.com/dustin/go-humanize"
"github.com/go-kit/kit/log/level"
"github.com/weaveworks/common/httpgrpc"

"github.com/cortexproject/cortex/pkg/util"
Expand All @@ -12,6 +14,7 @@ import (
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/logql/unmarshal"
unmarshal_legacy "github.com/grafana/loki/pkg/logql/unmarshal/legacy"
lokiutil "github.com/grafana/loki/pkg/util"
)

var contentType = http.CanonicalHeaderKey("Content-Type")
Expand All @@ -20,7 +23,6 @@ const applicationJSON = "application/json"

// PushHandler reads a snappy-compressed proto from the HTTP body.
func (d *Distributor) PushHandler(w http.ResponseWriter, r *http.Request) {

req, err := ParseRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -44,22 +46,53 @@ func (d *Distributor) PushHandler(w http.ResponseWriter, r *http.Request) {
func ParseRequest(r *http.Request) (*logproto.PushRequest, error) {
var req logproto.PushRequest

switch r.Header.Get(contentType) {
logger := util.WithContext(r.Context(), util.Logger)
body := lokiutil.NewSizeReader(r.Body)
contentType := r.Header.Get(contentType)

defer func() {
var (
entriesSize int64
streamLabelsSize int64
totalEntries int64
)

for _, s := range req.Streams {
streamLabelsSize += int64(len(s.Labels))
for _, e := range s.Entries {
totalEntries++
entriesSize += int64(len(e.Line))
}
}
level.Debug(logger).Log(
"msg", "push request parsed",
"path", r.URL.Path,
"content-type", contentType,
"body-size", humanize.Bytes(uint64(body.Size())),
"streams", len(req.Streams),
"entries", totalEntries,
"streamLabelsSize", humanize.Bytes(uint64(streamLabelsSize)),
cyriltovena marked this conversation as resolved.
Show resolved Hide resolved
"entriesSize", humanize.Bytes(uint64(entriesSize)),
"totalSize", humanize.Bytes(uint64(entriesSize+streamLabelsSize)),
)
}()

switch contentType {
case applicationJSON:
var err error

if loghttp.GetVersion(r.RequestURI) == loghttp.VersionV1 {
err = unmarshal.DecodePushRequest(r.Body, &req)
err = unmarshal.DecodePushRequest(body, &req)
} else {
err = unmarshal_legacy.DecodePushRequest(r.Body, &req)
err = unmarshal_legacy.DecodePushRequest(body, &req)
}

if err != nil {
return nil, err
}

default:
if err := util.ParseProtoReader(r.Context(), r.Body, int(r.ContentLength), math.MaxInt32, &req, util.RawSnappy); err != nil {
if err := util.ParseProtoReader(r.Context(), body, int(r.ContentLength), math.MaxInt32, &req, util.RawSnappy); err != nil {
return nil, err
}
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/util/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util

import (
"io"
)

type sizeReader struct {
size int64
r io.Reader
}

type SizeReader interface {
io.Reader
Size() int64
}

// NewSizeReader returns an io.Reader that will have the number of bytes
// read from r available.
func NewSizeReader(r io.Reader) SizeReader {
return &sizeReader{r: r}
}

func (v *sizeReader) Read(p []byte) (int, error) {
n, err := v.r.Read(p)
v.size += int64(n)
return n, err
}

func (v *sizeReader) Size() int64 {
return v.size
}