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

Инкремент №14 #14

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[91 123 34 105 100 34 58 34 65 108 108 111 99 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 49 54 48 52 48 125 44 123 34 105 100 34 58 34 66 117 99 107 72 97 115 104 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 55 55 49 53 125 44 123 34 105 100 34 58 34 70 114 101 101 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 56 53 125 44 123 34 105 100 34 58 34 71 67 67 80 85 70 114 97 99 116 105 111 110 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 71 67 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 56 51 49 55 57 50 125 44 123 34 105 100 34 58 34 72 101 97 112 65 108 108 111 99 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 49 54 48 52 48 125 44 123 34 105 100 34 58 34 72 101 97 112 73 100 108 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 50 56 57 49 55 55 54 125 44 123 34 105 100 34 58 34 72 101 97 112 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 57 52 50 48 56 48 125 44 123 34 105 100 34 58 34 72 101 97 112 79 98 106 101 99 116 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 54 55 52 125 44 123 34 105 100 34 58 34 72 101 97 112 82 101 108 101 97 115 101 100 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 50 56 57 49 55 55 54 125 44 123 34 105 100 34 58 34 72 101 97 112 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 56 51 51 56 53 54 125 44 123 34 105 100 34 58 34 76 97 115 116 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 76 111 111 107 117 112 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 77 67 97 99 104 101 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 57 54 48 48 125 44 123 34 105 100 34 58 34 77 67 97 99 104 101 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 53 54 48 48 125 44 123 34 105 100 34 58 34 77 83 112 97 110 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 51 55 54 48 125 44 123 34 105 100 34 58 34 77 83 112 97 110 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 52 56 57 54 48 125 44 123 34 105 100 34 58 34 77 97 108 108 111 99 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 56 53 57 125 44 123 34 105 100 34 58 34 78 101 120 116 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 52 49 57 52 51 48 52 125 44 123 34 105 100 34 58 34 78 117 109 70 111 114 99 101 100 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 78 117 109 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 79 116 104 101 114 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 56 54 55 57 49 55 125 44 123 34 105 100 34 58 34 80 97 117 115 101 84 111 116 97 108 78 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 83 116 97 99 107 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 54 48 52 52 56 125 44 123 34 105 100 34 58 34 83 116 97 99 107 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 54 48 52 52 56 125 44 123 34 105 100 34 58 34 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 54 57 54 54 50 56 56 125 44 123 34 105 100 34 58 34 84 111 116 97 108 65 108 108 111 99 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 49 54 48 52 48 125 44 123 34 105 100 34 58 34 82 97 110 100 111 109 86 97 108 117 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 46 53 48 52 52 55 50 50 51 55 51 49 49 51 52 48 51 125 44 123 34 105 100 34 58 34 80 111 108 108 67 111 117 110 116 34 44 34 116 121 112 101 34 58 34 99 111 117 110 116 101 114 34 44 34 100 101 108 116 97 34 58 51 125 93]
1 change: 1 addition & 0 deletions 2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[91 123 34 105 100 34 58 34 65 108 108 111 99 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 49 54 48 52 48 125 44 123 34 105 100 34 58 34 66 117 99 107 72 97 115 104 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 55 55 49 53 125 44 123 34 105 100 34 58 34 70 114 101 101 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 56 53 125 44 123 34 105 100 34 58 34 71 67 67 80 85 70 114 97 99 116 105 111 110 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 71 67 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 56 51 49 55 57 50 125 44 123 34 105 100 34 58 34 72 101 97 112 65 108 108 111 99 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 49 54 48 52 48 125 44 123 34 105 100 34 58 34 72 101 97 112 73 100 108 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 50 56 57 49 55 55 54 125 44 123 34 105 100 34 58 34 72 101 97 112 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 57 52 50 48 56 48 125 44 123 34 105 100 34 58 34 72 101 97 112 79 98 106 101 99 116 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 54 55 52 125 44 123 34 105 100 34 58 34 72 101 97 112 82 101 108 101 97 115 101 100 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 50 56 57 49 55 55 54 125 44 123 34 105 100 34 58 34 72 101 97 112 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 56 51 51 56 53 54 125 44 123 34 105 100 34 58 34 76 97 115 116 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 76 111 111 107 117 112 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 77 67 97 99 104 101 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 57 54 48 48 125 44 123 34 105 100 34 58 34 77 67 97 99 104 101 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 53 54 48 48 125 44 123 34 105 100 34 58 34 77 83 112 97 110 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 51 55 54 48 125 44 123 34 105 100 34 58 34 77 83 112 97 110 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 52 56 57 54 48 125 44 123 34 105 100 34 58 34 77 97 108 108 111 99 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 49 56 53 57 125 44 123 34 105 100 34 58 34 78 101 120 116 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 52 49 57 52 51 48 52 125 44 123 34 105 100 34 58 34 78 117 109 70 111 114 99 101 100 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 78 117 109 71 67 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 79 116 104 101 114 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 56 54 55 57 49 55 125 44 123 34 105 100 34 58 34 80 97 117 115 101 84 111 116 97 108 78 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 125 44 123 34 105 100 34 58 34 83 116 97 99 107 73 110 117 115 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 54 48 52 52 56 125 44 123 34 105 100 34 58 34 83 116 97 99 107 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 54 48 52 52 56 125 44 123 34 105 100 34 58 34 83 121 115 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 54 57 54 54 50 56 56 125 44 123 34 105 100 34 58 34 84 111 116 97 108 65 108 108 111 99 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 51 49 54 48 52 48 125 44 123 34 105 100 34 58 34 82 97 110 100 111 109 86 97 108 117 101 34 44 34 116 121 112 101 34 58 34 103 97 117 103 101 34 44 34 118 97 108 117 101 34 58 48 46 53 48 52 52 55 50 50 51 55 51 49 49 51 52 48 51 125 44 123 34 105 100 34 58 34 80 111 108 108 67 111 117 110 116 34 44 34 116 121 112 101 34 58 34 99 111 117 110 116 101 114 34 44 34 100 101 108 116 97 34 58 51 125 93]
5 changes: 0 additions & 5 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@ func main() {
logging.LogFatal(err)
}

err = agnt.ParseFlags()
if err != nil {
logging.LogFatal(err)
}

agnt.Run()
}
89 changes: 57 additions & 32 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package agent

import (
"fmt"
"net/http"
"strings"
"sync"
"time"

"github.com/caarlos0/env/v11"
"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/services"
"github.com/ex0rcist/metflix/internal/utils"
"github.com/spf13/pflag"
)
Expand All @@ -24,6 +27,7 @@ type Config struct {
Address entities.Address `env:"ADDRESS"`
PollInterval int `env:"POLL_INTERVAL"`
ReportInterval int `env:"REPORT_INTERVAL"`
Secret entities.Secret `env:"KEY"`
}

func New() (*Agent, error) {
Expand All @@ -33,41 +37,25 @@ func New() (*Agent, error) {
ReportInterval: 10,
}

stats := NewStats()
exporter := NewMetricsExporter(&config.Address, nil)
err := parseConfig(config)
if err != nil {
return nil, err
}

var signer services.Signer
if len(config.Secret) > 0 {
signer = services.NewSignerService(config.Secret)
}

exporter := NewMetricsExporter(&config.Address, http.DefaultTransport, signer)

return &Agent{
Config: config,
Stats: stats,
Stats: NewStats(),
Exporter: exporter,
}, nil
}

func (a *Agent) ParseFlags() error {
address := a.Config.Address

pflag.VarP(&address, "address", "a", "address:port for HTTP API requests")

pflag.IntVarP(&a.Config.PollInterval, "poll-interval", "p", a.Config.PollInterval, "interval (s) for polling stats")
pflag.IntVarP(&a.Config.ReportInterval, "report-interval", "r", a.Config.ReportInterval, "interval (s) for polling stats")

pflag.Parse()

// because VarP gets non-pointer value, set it manually
pflag.Visit(func(f *pflag.Flag) {
switch f.Name {
case "address":
a.Config.Address = address
}
})

if err := env.Parse(a.Config); err != nil {
return err
}

return nil
}

func (a *Agent) Run() {
logging.LogInfo(a.Config.String())
logging.LogInfo("agent ready")
Expand Down Expand Up @@ -156,8 +144,45 @@ func (a *Agent) reportStats() {
}

func (c Config) String() string {
return fmt.Sprintf(
"agent config: address=%v; poll-interval=%v; report-interval=%v",
c.Address, c.PollInterval, c.ReportInterval,
)

str := []string{
fmt.Sprintf("address=%s", c.Address),
fmt.Sprintf("poll-interval=%v", c.PollInterval),
fmt.Sprintf("report-interval=%v", c.ReportInterval),
}

if len(c.Secret) > 0 {
str = append(str, fmt.Sprintf("secret=%v", c.Secret))
}

return "agent config: " + strings.Join(str, "; ")
}

func parseConfig(config *Config) error {
address := config.Address
pflag.VarP(&address, "address", "a", "address:port for HTTP API requests")

secret := config.Secret
pflag.VarP(&secret, "secret", "k", "a key to sign outgoing data")

pflag.IntVarP(&config.PollInterval, "poll-interval", "p", config.PollInterval, "interval (s) for polling stats")
pflag.IntVarP(&config.ReportInterval, "report-interval", "r", config.ReportInterval, "interval (s) for polling stats")

pflag.Parse()

// because VarP gets non-pointer value, set it manually
pflag.Visit(func(f *pflag.Flag) {
switch f.Name {
case "address":
config.Address = address
case "secret":
config.Secret = secret
}
})

if err := env.Parse(config); err != nil {
return err
}

return nil
}
23 changes: 17 additions & 6 deletions internal/agent/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/ex0rcist/metflix/internal/compression"
"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/metrics"
"github.com/ex0rcist/metflix/internal/services"
"github.com/ex0rcist/metflix/internal/utils"
"github.com/rs/zerolog/log"
)
Expand All @@ -26,16 +28,13 @@ type Exporter interface {
type MetricsExporter struct {
baseURL *entities.Address
client *http.Client
signer services.Signer

buffer []metrics.MetricExchange
err error
}

func NewMetricsExporter(baseURL *entities.Address, httpTransport http.RoundTripper) *MetricsExporter {
if httpTransport == nil {
httpTransport = http.DefaultTransport
}

func NewMetricsExporter(baseURL *entities.Address, httpTransport http.RoundTripper, signer services.Signer) *MetricsExporter {
client := &http.Client{
Timeout: 2 * time.Second,
Transport: httpTransport,
Expand All @@ -44,6 +43,7 @@ func NewMetricsExporter(baseURL *entities.Address, httpTransport http.RoundTripp
return &MetricsExporter{
baseURL: baseURL,
client: client,
signer: signer,
}
}

Expand Down Expand Up @@ -128,6 +128,16 @@ func (e *MetricsExporter) doSend() error {
req.Header.Set("Content-Encoding", "gzip")
req.Header.Set("X-Request-Id", requestID)

if e.signer != nil {
signature, err := e.signer.CalculateSignature(payload.Bytes())
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", err.Error())
return err
}

req.Header.Set("HashSHA256", signature)
}

logRequest(ctx, url, req.Header, body)

resp, err := e.client.Do(req)
Expand All @@ -146,7 +156,8 @@ func (e *MetricsExporter) doSend() error {
logResponse(ctx, resp, respBody)

if resp.StatusCode != http.StatusOK {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error reporting stats", resp.Status, string(respBody))
formatedBody := strings.ReplaceAll(string(respBody), "\n", "")
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error reporting stats", resp.Status, formatedBody)
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/agent/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestNewMetricsExporter(t *testing.T) {

require.NotPanics(func() {
address := entities.Address("localhost")
NewMetricsExporter(&address, nil)
NewMetricsExporter(&address, nil, nil)
})
}

Expand Down Expand Up @@ -58,7 +58,7 @@ func TestApiClientReport(t *testing.T) {

address := entities.Address("localhost:8080")

api := NewMetricsExporter(&address, RoundTripFunc(rtf))
api := NewMetricsExporter(&address, RoundTripFunc(rtf), nil) // TODO: add signer mock
api.Add("test", metrics.Counter(42))
err := api.Send()
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions internal/compression/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewCompressor(w http.ResponseWriter, ctx context.Context) *Compressor {

func (c *Compressor) Write(resp []byte) (int, error) {
contentType := c.Header().Get("Content-Type")

if _, ok := c.supportedContent[contentType]; !ok {
logging.LogDebugCtx(c.context, "compression not supported for "+contentType)
return c.ResponseWriter.Write(resp)
Expand Down
2 changes: 2 additions & 0 deletions internal/entities/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
ErrEncodingInternal = errors.New("internal encoding error")
ErrEncodingUnsupported = errors.New("requsted encoding is not supported")

ErrNoSignature = errors.New("no signature provided")

ErrUnexpected = errors.New("unexpected error")
)

Expand Down
32 changes: 32 additions & 0 deletions internal/entities/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package entities

import (
"strings"

"github.com/rs/zerolog/log"
)

type Secret string

func (s *Secret) Set(src string) error {
if len([]byte(src)) < 32 {
log.Warn().Msg("secret is too short")
}

*s = Secret(src)

return nil
}

func (s Secret) Type() string {
return "string"
}

func (s Secret) String() string {
if len(s) <= 2 {
return string(s)
}

masked := strings.Repeat("*", len(s)-2)
return string(s[0]) + masked + string(s[len(s)-1])
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,12 @@ import (
"errors"
"net/http"
"strings"
"time"

"github.com/ex0rcist/metflix/internal/compression"
"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/utils"
"github.com/go-chi/chi/middleware"
"github.com/rs/zerolog/log"
)

func RequestsLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
requestID := findOrCreateRequestID(r)

// setup child logger for middleware
logger := log.Logger.With().
Str("rid", requestID).
Logger()

// log started
logger.Info().
Str("method", r.Method).
Str("url", r.URL.String()).
Str("remote-addr", r.RemoteAddr). // middleware.RealIP
Msg("Started")

logger.Debug().
Msgf("request: %s", utils.HeadersToStr(r.Header))

// execute
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
ctx := logger.WithContext(r.Context())
next.ServeHTTP(ww, r.WithContext(ctx))

logger.Debug().
Msgf("response: %s", utils.HeadersToStr(ww.Header()))

// log completed
logger.Info().
Float64("elapsed", time.Since(start).Seconds()).
Int("status", ww.Status()).
Int("size", ww.BytesWritten()).
Msg("Completed")
})
}

func DecompressRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down Expand Up @@ -98,16 +57,6 @@ func CompressResponse(next http.Handler) http.Handler {
})
}

func findOrCreateRequestID(r *http.Request) string {
requestID := r.Header.Get("X-Request-Id")

if requestID == "" {
requestID = utils.GenerateRequestID()
}

return requestID
}

func needGzipEncoding(r *http.Request) bool {
if len(r.Header.Get("Accept-Encoding")) == 0 {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"net/http"
"net/http/httptest"

"testing"
)

Expand Down Expand Up @@ -165,27 +164,6 @@ func TestCompressResponse_NoCompressionRequested(t *testing.T) {
}
}

// findOrCreateRequestID tests
func TestFindOrCreateRequestID_ExistingID(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("X-Request-Id", "existing-id")

requestID := findOrCreateRequestID(req)
if requestID != "existing-id" {
t.Fatalf("expected 'existing-id', got %s", requestID)
}
}

func TestFindOrCreateRequestID_NewID(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)

requestID := findOrCreateRequestID(req)
if requestID == "" {
t.Fatalf("expected non-empty request ID")
}
}

// needGzipEncoding tests
func TestNeedGzipEncoding_Supported(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Encoding", "gzip")
Expand Down
Loading
Loading