Skip to content

Commit 875b071

Browse files
authored
Merge pull request #136 from codex-team/feat/brotli-sentry
2 parents a0155ab + a5e0749 commit 875b071

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/codex-team/hawk.collector
22

33
require (
44
github.com/alicebob/miniredis/v2 v2.34.0
5+
github.com/andybalholm/brotli v1.0.2
56
github.com/caarlos0/env/v6 v6.6.0
67
github.com/cenkalti/backoff/v4 v4.1.0
78
github.com/codex-team/hawk.go v1.0.5
@@ -17,7 +18,6 @@ require (
1718
github.com/streadway/amqp v1.0.0
1819
github.com/stretchr/testify v1.7.0
1920
github.com/tidwall/gjson v1.8.0
20-
github.com/tidwall/sjson v1.1.6
2121
github.com/valyala/fasthttp v1.25.0
2222
go.mongodb.org/mongo-driver v1.7.1
2323
)

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,13 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
368368
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
369369
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
370370
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
371-
github.com/tidwall/gjson v1.7.4/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
372371
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
373372
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
374373
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
375374
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
376375
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
377376
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
378377
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
379-
github.com/tidwall/sjson v1.1.6 h1:8fDdlahON04OZBlTQCIatW8FstSFJz8oxidj5h0rmSQ=
380-
github.com/tidwall/sjson v1.1.6/go.mod h1:KN3FZ7odvXIHPbJdhNorK/M9lWweVUbXsXXhrJ/kGOA=
381378
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
382379
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
383380
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=

pkg/server/errorshandler/handler_sentry.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ func (handler *Handler) HandleSentry(ctx *fasthttp.RequestCtx) {
7272
sendAnswerHTTP(ctx, ResponseMessage{Code: 400, Error: true, Message: "Failed to decompress gzip body"})
7373
return
7474
}
75-
log.Debugf("Decompressed body: %s", sentryEnvelopeBody)
75+
log.Debugf("Decompressed gzip body: %s", sentryEnvelopeBody)
76+
} else if contentEncoding == "br" {
77+
sentryEnvelopeBody, err = decompressBrotliString(sentryEnvelopeBody)
78+
if err != nil {
79+
log.Warnf("Failed to decompress brotli body: %s", err)
80+
sendAnswerHTTP(ctx, ResponseMessage{Code: 400, Error: true, Message: "Failed to decompress brotli body"})
81+
return
82+
}
83+
log.Debugf("Decompressed brotli body: %s", sentryEnvelopeBody)
7684
} else {
7785
log.Debugf("Body: %s", sentryEnvelopeBody)
7886
}

pkg/server/errorshandler/sentry_utils.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"fmt"
88
"io"
99
"strings"
10-
log "github.com/sirupsen/logrus"
10+
11+
"github.com/andybalholm/brotli"
12+
log "github.com/sirupsen/logrus"
1113
)
1214

1315
func decompressGzipString(gzipString []byte) ([]byte, error) {
@@ -26,15 +28,27 @@ func decompressGzipString(gzipString []byte) ([]byte, error) {
2628
return result.Bytes(), nil
2729
}
2830

31+
func decompressBrotliString(brotliString []byte) ([]byte, error) {
32+
reader := brotli.NewReader(bytes.NewReader(brotliString))
33+
34+
var result bytes.Buffer
35+
_, err := io.Copy(&result, reader)
36+
if err != nil {
37+
return []byte(""), fmt.Errorf("failed to decompress brotli data: %w", err)
38+
}
39+
40+
return result.Bytes(), nil
41+
}
42+
2943
func getSentryKeyFromAuth(auth string) (string, error) {
3044
auth = strings.TrimPrefix(auth, "Sentry ")
3145
pairs := strings.Split(auth, ",")
3246
for _, pair := range pairs {
33-
pair = strings.TrimSpace(pair)
34-
kv := strings.SplitN(pair, "=", 2)
35-
if len(kv) == 2 && strings.TrimSpace(kv[0]) == "sentry_key" {
36-
return strings.TrimSpace(kv[1]), nil
37-
}
47+
pair = strings.TrimSpace(pair)
48+
kv := strings.SplitN(pair, "=", 2)
49+
if len(kv) == 2 && strings.TrimSpace(kv[0]) == "sentry_key" {
50+
return strings.TrimSpace(kv[1]), nil
51+
}
3852
}
3953

4054
log.Infof("Sentry key not found in auth header: %s", auth)

0 commit comments

Comments
 (0)