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

chore: migrate sample event column to text for reporting #5503

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion enterprise/reporting/error_reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (edr *ErrorDetailReporter) Report(ctx context.Context, metrics []*types.PUR
metric.StatusDetail.ErrorDetails.Code,
metric.StatusDetail.ErrorDetails.Message,
sampleResponse,
string(sampleEvent),
sampleEvent,
metric.StatusDetail.EventName,
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion enterprise/reporting/label_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func createMetricObject(eventName, errorMessage string) types.PUReportedMetric {
Count: 3,
StatusCode: 0,
SampleResponse: `{"some-sample-response-key": "some-sample-response-value"}`,
SampleEvent: []byte(`{"some-sample-event-key": "some-sample-event-value"}`),
SampleEvent: `{"some-sample-event-key": "some-sample-event-value"}`,
EventName: eventName,
EventType: "some-event-type",
},
Expand Down
8 changes: 4 additions & 4 deletions enterprise/reporting/reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ func (r *DefaultReporter) getReports(currentMs, aggregationIntervalMin int64, sy
SELECT
%s, MAX(reported_at),
COALESCE(
(ARRAY_AGG(sample_response ORDER BY id DESC) FILTER (WHERE (sample_event != '{}'::jsonb AND sample_event IS NOT NULL) OR (sample_response IS NOT NULL AND sample_response != '')))[1],
(ARRAY_AGG(sample_response ORDER BY id DESC) FILTER (WHERE (sample_event != '{}' AND sample_event IS NOT NULL) OR (sample_response IS NOT NULL AND sample_response != '')))[1],
''
) AS sample_response,
COALESCE(
(ARRAY_AGG(sample_event ORDER BY id DESC) FILTER (WHERE (sample_event != '{}'::jsonb AND sample_event IS NOT NULL) OR (sample_response IS NOT NULL AND sample_response != '')))[1],
'{}'::jsonb
(ARRAY_AGG(sample_event ORDER BY id DESC) FILTER (WHERE (sample_event != '{}' AND sample_event IS NOT NULL) OR (sample_response IS NOT NULL AND sample_response != '')))[1],
'{}'
) AS sample_event,
SUM(count),
SUM(violation_count)
Expand Down Expand Up @@ -715,7 +715,7 @@ func (r *DefaultReporter) Report(ctx context.Context, metrics []*types.PUReporte
metric.StatusDetail.Count, metric.StatusDetail.ViolationCount,
metric.PUDetails.TerminalPU, metric.PUDetails.InitialPU,
metric.StatusDetail.StatusCode,
sampleResponse, string(sampleEvent),
sampleResponse, sampleEvent,
metric.StatusDetail.EventName, metric.StatusDetail.EventType,
metric.StatusDetail.ErrorType,
)
Expand Down
182 changes: 16 additions & 166 deletions enterprise/reporting/reporting_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package reporting

import (
"bytes"
"context"
"encoding/json"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -13,7 +11,6 @@ import (
"github.com/rudderlabs/rudder-go-kit/config"
"github.com/rudderlabs/rudder-go-kit/logger"
"github.com/rudderlabs/rudder-go-kit/stats"
"github.com/rudderlabs/rudder-server/utils/misc"
"github.com/rudderlabs/rudder-server/utils/types"
)

Expand All @@ -39,7 +36,7 @@ var _ = Describe("Reporting", func() {
Count: 3,
StatusCode: 0,
SampleResponse: `{"some-sample-response-key": "some-sample-response-value"}`,
SampleEvent: []byte(`{"some-sample-event-key": "some-sample-event-value"}`),
SampleEvent: `{"some-sample-event-key": "some-sample-event-value"}`,
EventName: "some-event-name",
EventType: "some-event-type",
},
Expand All @@ -64,7 +61,7 @@ var _ = Describe("Reporting", func() {
Count: 3,
StatusCode: 0,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
EventName: "",
EventType: "",
},
Expand Down Expand Up @@ -121,7 +118,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 5,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "",
},
},
Expand All @@ -148,7 +145,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand All @@ -175,7 +172,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand Down Expand Up @@ -215,7 +212,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 5,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "",
},
},
Expand Down Expand Up @@ -245,7 +242,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand Down Expand Up @@ -275,7 +272,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand Down Expand Up @@ -316,7 +313,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 5,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "",
},
{
Expand All @@ -325,7 +322,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand Down Expand Up @@ -355,7 +352,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand Down Expand Up @@ -393,7 +390,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "another-error-type",
},
}
Expand Down Expand Up @@ -424,7 +421,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 5,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "",
},
{
Expand All @@ -433,7 +430,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand Down Expand Up @@ -463,7 +460,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "some-error-type",
},
},
Expand Down Expand Up @@ -493,7 +490,7 @@ func TestGetAggregatedReports(t *testing.T) {
ViolationCount: 10,
StatusCode: 200,
SampleResponse: "",
SampleEvent: []byte(`{}`),
SampleEvent: `{}`,
ErrorType: "another-error-type",
},
},
Expand All @@ -504,150 +501,3 @@ func TestGetAggregatedReports(t *testing.T) {
assert.Equal(t, expectedResponse, aggregatedMetrics)
})
}

func TestSanitizeJSONForReports(t *testing.T) {
tests := []struct {
name string
input json.RawMessage
want json.RawMessage
wantErr bool
}{
{
name: "empty input",
input: json.RawMessage(``),
want: json.RawMessage(`{}`),
wantErr: false,
},
{
name: "valid json",
input: json.RawMessage(`{"key":"value"}`),
want: json.RawMessage(`{"key":"value"}`),
wantErr: false,
},
{
name: "json with null characters",
input: json.RawMessage(`{"key":"\u0000value\u0000"}`),
want: json.RawMessage(`{"key":"value"}`),
wantErr: false,
},
{
name: "json with html entities",
input: json.RawMessage(`{"key":"\u0026value\u003ctest\u003e"}`),
want: json.RawMessage(`{"key":"\u0026value\u003ctest\u003e"}`),
wantErr: false,
},
{
name: "invalid json",
input: json.RawMessage(`{"key":"value`),
want: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := misc.SanitizeJSON(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("sanitizeJSONForReports() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !bytes.Equal(got, tt.want) {
t.Errorf("sanitizeJSONForReports() = %s, want %s", got, tt.want)
}
})
}
}

func TestSanitizeStringForReports(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "empty string",
input: "",
want: "",
},
{
name: "string with null characters",
input: "test\u0000string\u0000",
want: "teststring",
},
{
name: "string without null characters",
input: "test string",
want: "test string",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sanitizeStringForReports(tt.input)
if got != tt.want {
t.Errorf("sanitizeStringForReports() = %v, want %v", got, tt.want)
}
})
}
}

func TestGetSampleWithEventSampling_WithSanitization(t *testing.T) {
tests := []struct {
name string
sampleEvent json.RawMessage
sampleResponse string
wantEvent json.RawMessage
wantResponse string
wantErr bool
}{
{
name: "sample event with null characters",
sampleEvent: json.RawMessage(`{"key":"\u0000value\u0000"}`),
sampleResponse: "test\u0000response",
wantEvent: json.RawMessage(`{"key":"value"}`),
wantResponse: "testresponse",
wantErr: false,
},
{
name: "sample event with <, >",
sampleEvent: json.RawMessage(`{"key":"\u0026value\u003ctest\u003e"}`),
sampleResponse: "test&response",
wantEvent: json.RawMessage(`{"key":"\u0026value\u003ctest\u003e"}`),
wantResponse: "test&response",
wantErr: false,
},
{
name: "invalid json in sample event",
sampleEvent: json.RawMessage(`{"key":"value`),
sampleResponse: "test\u0000response",
wantEvent: nil,
wantResponse: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metric := types.PUReportedMetric{
StatusDetail: &types.StatusDetail{
SampleEvent: tt.sampleEvent,
SampleResponse: tt.sampleResponse,
},
}

gotEvent, gotResponse, err := getSampleWithEventSampling(metric, 0, nil, false, 0)
if (err != nil) != tt.wantErr {
t.Errorf("getSampleWithEventSampling() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if !bytes.Equal(gotEvent, tt.wantEvent) {
t.Errorf("getSampleWithEventSampling() gotEvent = %s, want %s", gotEvent, tt.wantEvent)
}
if gotResponse != tt.wantResponse {
t.Errorf("getSampleWithEventSampling() gotResponse = %v, want %v", gotResponse, tt.wantResponse)
}
}
})
}
}
Loading
Loading