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

Add GZIP compression to collector endpoint #95

Merged
merged 2 commits into from
Jan 26, 2024
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
14 changes: 13 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collector

import (
"compress/gzip"
"context"
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -139,7 +140,18 @@ func (c *Collector) GetRawNetworkMetricsHandler(w http.ResponseWriter, req *http
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(batchBytes); err != nil {

writer, err := gzip.NewWriterLevel(w, gzip.BestCompression)
if err != nil {
c.log.Errorf("cannot create gzip writer: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer writer.Close()

w.Header().Add("Content-Encoding", "gzip")
Ivaka marked this conversation as resolved.
Show resolved Hide resolved

if _, err := writer.Write(batchBytes); err != nil {
c.log.Errorf("write batch: %v", err)
return
}
Expand Down
19 changes: 17 additions & 2 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package collector

import (
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"sort"
Expand Down Expand Up @@ -424,7 +426,13 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) {
r.Equal(200, w.Code)

batch := &pb.RawNetworkMetricBatch{}
err := proto.Unmarshal(w.Body.Bytes(), batch)
gzreader, err := gzip.NewReader(w.Body)
r.NoError(err)

body, err := io.ReadAll(gzreader)
r.NoError(err)

err = proto.Unmarshal(body, batch)
r.NoError(err)
r.Len(batch.Items, 2)
sort.Slice(batch.Items, func(i, j int) bool {
Expand Down Expand Up @@ -511,7 +519,14 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) {
r.Equal(200, w.Code)

batch := &pb.RawNetworkMetricBatch{}
err := proto.Unmarshal(w.Body.Bytes(), batch)

gzreader, err := gzip.NewReader(w.Body)
r.NoError(err)

body, err := io.ReadAll(gzreader)
r.NoError(err)

err = proto.Unmarshal(body, batch)
r.NoError(err)
r.Len(batch.Items, 2)
sort.Slice(batch.Items, func(i, j int) bool {
Expand Down
Loading