Skip to content

Commit

Permalink
NR-181096 fix: add log for size of compressed data in request (#1936)
Browse files Browse the repository at this point in the history
* NR-181096 fix: add log for size of compressed data in request

* remove parallel test execution
  • Loading branch information
rajrohanyadav authored Oct 18, 2024
1 parent 040624b commit b770e0e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/backend/telemetryapi/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ func createRequest(ctx context.Context, rawJSON json.RawMessage, compressed *byt
if nil != err {
return req, fmt.Errorf("error creating request: %v", err)
}

logger.WithTraceField("compressed_data_size", compressedLen).Debug("Request created")

reqHTTP.Header.Add("Content-Type", "application/json")
reqHTTP.Header.Add("Api-Key", apiKey)
reqHTTP.Header.Add("Content-Encoding", "gzip")
Expand Down
58 changes: 58 additions & 0 deletions pkg/backend/telemetryapi/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"encoding/json"
"fmt"
"math/rand"
"regexp"
"testing"
"time"

"github.com/newrelic/infrastructure-agent/pkg/backend/telemetryapi/internal"
"github.com/newrelic/infrastructure-agent/pkg/log"
logHelper "github.com/newrelic/infrastructure-agent/test/log"
"github.com/sirupsen/logrus"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -763,3 +767,57 @@ func Test_buildRequestsMultipleMetricsBatch(t *testing.T) {
})
}
}

//nolint:paralleltest
func TestCreateRequest(t *testing.T) {
tests := map[string]struct {
rawJSON json.RawMessage
compressedPayload *bytes.Buffer
compressedLen int
apiKey, url, userAgent string
}{
"successful request creation": {
rawJSON: json.RawMessage(`{"key":"value"}`),
compressedPayload: bytes.NewBufferString("compressed data"),
compressedLen: 14,
apiKey: "test-api-key",
url: "http://test-url",
userAgent: "test-user-agent",
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
// Mock compressFunc
origCompressFunc := compressFunc
defer func() { compressFunc = origCompressFunc }()
compressFunc = func(_ []byte) (*bytes.Buffer, error) {
return test.compressedPayload, nil
}
// Mock log
hook := logHelper.NewInMemoryEntriesHook([]logrus.Level{logrus.DebugLevel})
log.AddHook(hook)
log.SetLevel(logrus.TraceLevel)
// Test
req, err := createRequest(ctx, test.rawJSON, test.compressedPayload, test.compressedLen, test.apiKey, test.url, test.userAgent)
// Assert
require.NoError(t, err)
assert.Equal(t, "application/json", req.Request.Header.Get("Content-Type"))
assert.Equal(t, test.apiKey, req.Request.Header.Get("Api-Key"))
assert.Equal(t, "gzip", req.Request.Header.Get("Content-Encoding"))
assert.Equal(t, test.userAgent, req.Request.Header.Get("User-Agent"))
assert.Equal(t, test.url, req.Request.URL.String())
assert.Equal(t, ctx, req.Request.Context())
assert.Equal(t, test.rawJSON, req.UncompressedBody)
assert.Equal(t, test.compressedPayload.Bytes(), req.compressedBody)
assert.Equal(t, test.compressedLen, req.compressedBodyLength)
// Assert log
assert.True(t, hook.EntryWithMessageExists(regexp.MustCompile(`Request created`)))
logEntries := hook.GetEntries()
assert.Len(t, logEntries, 1)
assert.Equal(t, logrus.DebugLevel, logEntries[0].Level)
assert.Equal(t, test.compressedLen, logEntries[0].Data["compressed_data_size"])
})
}
}

0 comments on commit b770e0e

Please sign in to comment.