Distributor: Loki API can receive gzipped JSON (#3291)

* Push API recognize HTTP header "Content-Encoding: gzip" and upload gzipped JSON

* Fix documentation mistake
pull/3297/head
Dmitry Ukolov 5 years ago committed by GitHub
parent 59a34f9867
commit 2a51fb42bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/sources/api/_index.md
  2. 23
      pkg/distributor/http.go

@ -581,6 +581,8 @@ JSON post body can be sent in the following format:
} }
``` ```
You can set `Content-Encoding: gzip` request header and post gzipped JSON.
> **NOTE**: logs sent to Loki for every stream must be in timestamp-ascending > **NOTE**: logs sent to Loki for every stream must be in timestamp-ascending
> order; logs with identical timestamps are only allowed if their content > order; logs with identical timestamps are only allowed if their content
> differs. If a log line is received with a timestamp older than the most > differs. If a log line is received with a timestamp older than the most

@ -1,6 +1,8 @@
package distributor package distributor
import ( import (
"compress/gzip"
"fmt"
"math" "math"
"net/http" "net/http"
"time" "time"
@ -23,6 +25,7 @@ import (
var ( var (
contentType = http.CanonicalHeaderKey("Content-Type") contentType = http.CanonicalHeaderKey("Content-Type")
contentEnc = http.CanonicalHeaderKey("Content-Encoding")
bytesIngested = promauto.NewCounterVec(prometheus.CounterOpts{ bytesIngested = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "loki", Namespace: "loki",
@ -63,7 +66,24 @@ func (d *Distributor) PushHandler(w http.ResponseWriter, r *http.Request) {
func ParseRequest(r *http.Request) (*logproto.PushRequest, error) { func ParseRequest(r *http.Request) (*logproto.PushRequest, error) {
userID, _ := user.ExtractOrgID(r.Context()) userID, _ := user.ExtractOrgID(r.Context())
logger := util_log.WithContext(r.Context(), util_log.Logger) logger := util_log.WithContext(r.Context(), util_log.Logger)
body := lokiutil.NewSizeReader(r.Body)
var body lokiutil.SizeReader
contentEncoding := r.Header.Get(contentEnc)
switch contentEncoding {
case "":
body = lokiutil.NewSizeReader(r.Body)
case "gzip":
gzipReader, err := gzip.NewReader(r.Body)
if err != nil {
return nil, err
}
defer gzipReader.Close()
body = lokiutil.NewSizeReader(gzipReader)
default:
return nil, fmt.Errorf("Content-Encoding %q not supported", contentEncoding)
}
contentType := r.Header.Get(contentType) contentType := r.Header.Get(contentType)
var req logproto.PushRequest var req logproto.PushRequest
@ -97,6 +117,7 @@ func ParseRequest(r *http.Request) (*logproto.PushRequest, error) {
"msg", "push request parsed", "msg", "push request parsed",
"path", r.URL.Path, "path", r.URL.Path,
"contentType", contentType, "contentType", contentType,
"contentEncoding", contentEncoding,
"bodySize", humanize.Bytes(uint64(body.Size())), "bodySize", humanize.Bytes(uint64(body.Size())),
"streams", len(req.Streams), "streams", len(req.Streams),
"entries", totalEntries, "entries", totalEntries,

Loading…
Cancel
Save