diff --git a/contrib/aws/aws-sdk-go-v2/aws/eventbridge/eventbridge_test.go b/contrib/aws/aws-sdk-go-v2/aws/eventbridge/eventbridge_test.go index 288391d351..c4ba41f00b 100644 --- a/contrib/aws/aws-sdk-go-v2/aws/eventbridge/eventbridge_test.go +++ b/contrib/aws/aws-sdk-go-v2/aws/eventbridge/eventbridge_test.go @@ -9,6 +9,7 @@ import ( "context" "encoding/json" "strconv" + "strings" "testing" "github.com/aws/aws-sdk-go-v2/aws" @@ -159,3 +160,53 @@ func TestInjectTraceContext(t *testing.T) { }) } } + +func TestInjectTraceContextSizeLimit(t *testing.T) { + mt := mocktracer.Start() + defer mt.Stop() + + ctx := context.Background() + _, ctx = tracer.StartSpanFromContext(ctx, "test-span") + + tests := []struct { + name string + entry types.PutEventsRequestEntry + expected func(*testing.T, *types.PutEventsRequestEntry) + }{ + { + name: "Do not inject when payload is too large", + entry: types.PutEventsRequestEntry{ + Detail: aws.String(`{"large": "` + strings.Repeat("a", maxSizeBytes-15) + `"}`), + EventBusName: aws.String("test-bus"), + }, + expected: func(t *testing.T, entry *types.PutEventsRequestEntry) { + assert.GreaterOrEqual(t, len(*entry.Detail), maxSizeBytes-15) + assert.NotContains(t, *entry.Detail, datadogKey) + assert.True(t, strings.HasPrefix(*entry.Detail, `{"large": "`)) + assert.True(t, strings.HasSuffix(*entry.Detail, `"}`)) + }, + }, + { + name: "Inject when payload is just under the limit", + entry: types.PutEventsRequestEntry{ + Detail: aws.String(`{"large": "` + strings.Repeat("a", maxSizeBytes-1000) + `"}`), + EventBusName: aws.String("test-bus"), + }, + expected: func(t *testing.T, entry *types.PutEventsRequestEntry) { + assert.Less(t, len(*entry.Detail), maxSizeBytes) + var detail map[string]interface{} + err := json.Unmarshal([]byte(*entry.Detail), &detail) + require.NoError(t, err) + assert.Contains(t, detail, datadogKey) + assert.Contains(t, detail, "large") + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + injectTraceContext(ctx, &tt.entry) + tt.expected(t, &tt.entry) + }) + } +}