Push: add deflate compression in post requests (#5249)

* deflate-compression-5247 - add deflate compression in post requests

* deflate-compression-5247 - add deflate tests by example gzip
pull/5139/head
3JIou_from_home 3 years ago committed by GitHub
parent 392fde1e27
commit 863408684c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      pkg/loghttp/push/push.go
  2. 56
      pkg/loghttp/push/push_test.go

@ -1,6 +1,7 @@
package push
import (
"compress/flate"
"compress/gzip"
"fmt"
"io"
@ -67,6 +68,10 @@ func ParseRequest(logger log.Logger, userID string, r *http.Request, tenantsRete
}
defer gzipReader.Close()
body = gzipReader
case "deflate":
flateReader := flate.NewReader(bodySize)
defer flateReader.Close()
body = flateReader
default:
return nil, fmt.Errorf("Content-Encoding %q not supported", contentEncoding)
}

@ -2,6 +2,7 @@ package push
import (
"bytes"
"compress/flate"
"compress/gzip"
"log"
"net/http/httptest"
@ -26,6 +27,19 @@ func gzipString(source string) string {
return buf.String()
}
// Deflate source string and return compressed string
func deflateString(source string) string {
var buf bytes.Buffer
zw, _ := flate.NewWriter(&buf, 6)
if _, err := zw.Write([]byte(source)); err != nil {
log.Fatal(err)
}
if err := zw.Close(); err != nil {
log.Fatal(err)
}
return buf.String()
}
func TestParseRequest(t *testing.T) {
tests := []struct {
path string
@ -66,6 +80,13 @@ func TestParseRequest(t *testing.T) {
contentEncoding: `gzip`,
valid: true,
},
{
path: `/loki/api/v1/push`,
body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/json`,
contentEncoding: `deflate`,
valid: true,
},
{
path: `/loki/api/v1/push`,
body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
@ -80,6 +101,13 @@ func TestParseRequest(t *testing.T) {
contentEncoding: `gzip`,
valid: true,
},
{
path: `/loki/api/v1/push`,
body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/json; charset=utf-8`,
contentEncoding: `deflate`,
valid: true,
},
{
path: `/loki/api/v1/push`,
body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
@ -87,6 +115,13 @@ func TestParseRequest(t *testing.T) {
contentEncoding: `gzip`,
valid: false,
},
{
path: `/loki/api/v1/push`,
body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/jsonn; charset=utf-8`,
contentEncoding: `deflate`,
valid: false,
},
{
path: `/loki/api/v1/push`,
body: gzipString(`{"streams": [{ "stream": { "foo4": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
@ -94,6 +129,27 @@ func TestParseRequest(t *testing.T) {
contentEncoding: `gzip`,
valid: false,
},
{
path: `/loki/api/v1/push`,
body: deflateString(`{"streams": [{ "stream": { "foo4": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/json; charsetutf-8`,
contentEncoding: `deflate`,
valid: false,
},
{
path: `/loki/api/v1/push`,
body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/jsonn; charset=utf-8`,
contentEncoding: `deflate`,
valid: false,
},
{
path: `/loki/api/v1/push`,
body: deflateString(`{"streams": [{ "stream": { "foo4": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/json; charsetutf-8`,
contentEncoding: `deflate`,
valid: false,
},
}
// Testing input array

Loading…
Cancel
Save