Skip to content

Commit ff65499

Browse files
stats/opentelemetry: dynamically calculate compressed message size in tests (#9346)
Tests in `stats/opentelemetry` and `stats/opentelemetry/csm` asserted a hardcoded compressed message size of `57` bytes for a 10,000-byte zero payload with gzip compression. In Go 1.27, standard library `compress/gzip` compression improvements changed the output size of this payload from 57 bytes to 54 bytes, causing metric and trace size assertions to fail when executed under newer Go toolchains. Added `GzipCompressedMessageSize` helper in to compute the expected compressed byte length dynamically via `compress/gzip`. Updated metric and trace assertions to use the same. RELEASE NOTES: None
1 parent 8a9f866 commit ff65499

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

stats/opentelemetry/csm/observability_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
131131
}
132132
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
133133
defer cancel()
134+
unaryCompressedSize := float64(itestutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}}))
134135
tests := []struct {
135136
name string
136137
// To test the different operations for Unary RPC's from the interceptor
@@ -147,7 +148,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
147148
},
148149
opts: itestutils.MetricDataOptions{
149150
CSMLabels: csmLabels,
150-
UnaryCompressedMessageSize: float64(57),
151+
UnaryCompressedMessageSize: unaryCompressedSize,
151152
},
152153
},
153154
{
@@ -171,7 +172,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
171172
},
172173
opts: itestutils.MetricDataOptions{
173174
CSMLabels: csmLabels,
174-
UnaryCompressedMessageSize: float64(57),
175+
UnaryCompressedMessageSize: unaryCompressedSize,
175176
},
176177
},
177178
{
@@ -185,7 +186,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
185186
},
186187
opts: itestutils.MetricDataOptions{
187188
CSMLabels: csmLabels,
188-
UnaryCompressedMessageSize: float64(57),
189+
UnaryCompressedMessageSize: unaryCompressedSize,
189190
},
190191
},
191192
{
@@ -197,7 +198,7 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
197198
},
198199
opts: itestutils.MetricDataOptions{
199200
CSMLabels: csmLabels,
200-
UnaryCompressedMessageSize: float64(57),
201+
UnaryCompressedMessageSize: unaryCompressedSize,
201202
},
202203
},
203204
}
@@ -299,6 +300,7 @@ func (s) TestCSMPluginOptionStreaming(t *testing.T) {
299300
}
300301
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
301302
defer cancel()
303+
streamingCompressedSize := float64(itestutils.GzipCompressedMessageSize(t, &testpb.StreamingOutputCallRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}}))
302304
tests := []struct {
303305
name string
304306
// To test the different operations for Streaming RPC's from the
@@ -361,7 +363,7 @@ func (s) TestCSMPluginOptionStreaming(t *testing.T) {
361363
},
362364
opts: itestutils.MetricDataOptions{
363365
CSMLabels: csmLabels,
364-
StreamingCompressedMessageSize: float64(57),
366+
StreamingCompressedMessageSize: streamingCompressedSize,
365367
},
366368
},
367369
}
@@ -510,9 +512,13 @@ func (s) TestXDSLabels(t *testing.T) {
510512
customLabelAttr,
511513
}
512514

513-
unaryCompressedBytesSentRecv := int64(57) // Fixed 10000 bytes with gzip assumption.
515+
unaryCompressedBytesSentRecv := int64(itestutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{
516+
Payload: &testpb.Payload{
517+
Body: make([]byte, 10000),
518+
},
519+
})) // Fixed 10000 bytes with gzip.
514520
unaryBucketCounts := []uint64{0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
515-
unaryExtrema := metricdata.NewExtrema(int64(57))
521+
unaryExtrema := metricdata.NewExtrema(unaryCompressedBytesSentRecv)
516522
wantMetrics := []metricdata.Metrics{
517523
{
518524
Name: "grpc.client.attempt.started",

stats/opentelemetry/e2e_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,10 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
443443
}
444444
}
445445

446+
compressedSize := testutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}})
446447
wantMetrics := testutils.MetricData(testutils.MetricDataOptions{
447448
Target: ss.Target,
448-
UnaryCompressedMessageSize: float64(57),
449+
UnaryCompressedMessageSize: float64(compressedSize),
449450
})
450451
gotMetrics = testutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics)
451452
testutils.CompareMetrics(t, gotMetrics, wantMetrics)
@@ -846,9 +847,10 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
846847
}
847848
}
848849

850+
compressedSize := testutils.GzipCompressedMessageSize(t, &testpb.SimpleRequest{Payload: &testpb.Payload{Body: make([]byte, 10000)}})
849851
wantMetrics := testutils.MetricData(testutils.MetricDataOptions{
850852
Target: ss.Target,
851-
UnaryCompressedMessageSize: float64(57),
853+
UnaryCompressedMessageSize: float64(compressedSize),
852854
})
853855
gotMetrics = testutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics)
854856
testutils.CompareMetrics(t, gotMetrics, wantMetrics)
@@ -873,7 +875,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
873875
},
874876
{
875877
Key: "message-size-compressed",
876-
Value: attribute.IntValue(57),
878+
Value: attribute.IntValue(compressedSize),
877879
},
878880
},
879881
},
@@ -890,7 +892,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
890892
},
891893
{
892894
Key: "message-size-compressed",
893-
Value: attribute.IntValue(57),
895+
Value: attribute.IntValue(compressedSize),
894896
},
895897
},
896898
},
@@ -924,7 +926,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
924926
},
925927
{
926928
Key: "message-size-compressed",
927-
Value: attribute.IntValue(57),
929+
Value: attribute.IntValue(compressedSize),
928930
},
929931
},
930932
},
@@ -941,7 +943,7 @@ func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) {
941943
},
942944
{
943945
Key: "message-size-compressed",
944-
Value: attribute.IntValue(57),
946+
Value: attribute.IntValue(compressedSize),
945947
},
946948
},
947949
},

stats/opentelemetry/internal/testutils/testutils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package testutils
1919

2020
import (
21+
"bytes"
22+
"compress/gzip"
2123
"context"
2224
"fmt"
2325
"slices"
@@ -28,6 +30,7 @@ import (
2830
"go.opentelemetry.io/otel/sdk/metric"
2931
"go.opentelemetry.io/otel/sdk/metric/metricdata"
3032
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
33+
"google.golang.org/protobuf/proto"
3134
)
3235

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

771+
// GzipCompressedMessageSize returns the byte length of the proto message after
772+
// gzip compression.
773+
func GzipCompressedMessageSize(t *testing.T, m proto.Message) int {
774+
t.Helper()
775+
data, err := proto.Marshal(m)
776+
if err != nil {
777+
t.Fatalf("proto.Marshal failed: %v", err)
778+
}
779+
var buf bytes.Buffer
780+
zw := gzip.NewWriter(&buf)
781+
if _, err := zw.Write(data); err != nil {
782+
t.Fatalf("gzip.Write failed: %v", err)
783+
}
784+
if err := zw.Close(); err != nil {
785+
t.Fatalf("gzip.Close failed: %v", err)
786+
}
787+
return buf.Len()
788+
}
789+
768790
// CompareMetrics asserts wantMetrics are what we expect. For duration metrics
769791
// makes sure the data point is within possible testing time (five seconds from
770792
// context timeout).

0 commit comments

Comments
 (0)