Skip to content
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
20 changes: 13 additions & 7 deletions stats/opentelemetry/csm/observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
unaryCompressedSize := float64(itestutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}}))
tests := []struct {
name string
// To test the different operations for Unary RPC's from the interceptor
Expand All @@ -147,7 +148,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: float64(57),
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
{
Expand All @@ -171,7 +172,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: float64(57),
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
{
Expand All @@ -185,7 +186,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: float64(57),
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
{
Expand All @@ -197,7 +198,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: float64(57),
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
}
Expand Down Expand Up @@ -299,6 +300,7 @@ func (s) TestCSMPluginOptionStreaming(t *testing.T) {
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
streamingCompressedSize := float64(itestutils.GzipCompressedMessageSize(t, &testpb.StreamingOutputCallRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}}))
tests := []struct {
name string
// To test the different operations for Streaming RPC's from the
Expand Down Expand Up @@ -361,7 +363,7 @@ func (s) TestCSMPluginOptionStreaming(t *testing.T) {
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
StreamingCompressedMessageSize: float64(57),
StreamingCompressedMessageSize: streamingCompressedSize,
},
},
}
Expand Down Expand Up @@ -510,9 +512,13 @@ func (s) TestXDSLabels(t *testing.T) {
customLabelAttr,
}

unaryCompressedBytesSentRecv := int64(57) // Fixed 10000 bytes with gzip assumption.
unaryCompressedBytesSentRecv := int64(itestutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{
Payload: &testpb.Payload{
Body: make([]byte, 10000),
},
})) // Fixed 10000 bytes with gzip.
unaryBucketCounts := []uint64{0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
unaryExtrema := metricdata.NewExtrema(int64(57))
unaryExtrema := metricdata.NewExtrema(unaryCompressedBytesSentRecv)
wantMetrics := []metricdata.Metrics{
{
Name: "grpc.client.attempt.started",
Expand Down
14 changes: 8 additions & 6 deletions stats/opentelemetry/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,10 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
}
}

compressedSize := testutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}})
wantMetrics := testutils.MetricData(testutils.MetricDataOptions{
Target: ss.Target,
UnaryCompressedMessageSize: float64(57),
UnaryCompressedMessageSize: float64(compressedSize),
})
gotMetrics = testutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics)
testutils.CompareMetrics(t, gotMetrics, wantMetrics)
Expand Down Expand Up @@ -846,9 +847,10 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
}
}

compressedSize := testutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}})
wantMetrics := testutils.MetricData(testutils.MetricDataOptions{
Target: ss.Target,
UnaryCompressedMessageSize: float64(57),
UnaryCompressedMessageSize: float64(compressedSize),
})
gotMetrics = testutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics)
testutils.CompareMetrics(t, gotMetrics, wantMetrics)
Expand All @@ -873,7 +875,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
Value: attribute.IntValue(compressedSize),
},
},
},
Expand All @@ -890,7 +892,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
Value: attribute.IntValue(compressedSize),
},
},
},
Expand Down Expand Up @@ -924,7 +926,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
Value: attribute.IntValue(compressedSize),
},
},
},
Expand All @@ -941,7 +943,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
Value: attribute.IntValue(compressedSize),
},
},
},
Expand Down
22 changes: 22 additions & 0 deletions stats/opentelemetry/internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package testutils

import (
"bytes"
"compress/gzip"
"context"
"fmt"
"slices"
Expand All @@ -28,6 +30,7 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"google.golang.org/protobuf/proto"
)

// Redefine default bounds here to avoid a cyclic dependency with top level
Expand Down Expand Up @@ -765,6 +768,25 @@ func MetricData(options MetricDataOptions) []metricdata.Metrics {
}
}

// GzipCompressedMessageSize returns the byte length of the proto message after
// gzip compression.
func GzipCompressedMessageSize(t *testing.T, m proto.Message) int {
t.Helper()
data, err := proto.Marshal(m)
if err != nil {
t.Fatalf("proto.Marshal failed: %v", err)
}
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
if _, err := zw.Write(data); err != nil {
t.Fatalf("gzip.Write failed: %v", err)
}
if err := zw.Close(); err != nil {
t.Fatalf("gzip.Close failed: %v", err)
}
return buf.Len()
}

// CompareMetrics asserts wantMetrics are what we expect. For duration metrics
// makes sure the data point is within possible testing time (five seconds from
// context timeout).
Expand Down
Loading