forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobservability_test.go
More file actions
649 lines (603 loc) · 24 KB
/
Copy pathobservability_test.go
File metadata and controls
649 lines (603 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package csm
import (
"context"
"errors"
"fmt"
"io"
"os"
"testing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
"google.golang.org/grpc/encoding/gzip"
estats "google.golang.org/grpc/experimental/stats"
istats "google.golang.org/grpc/internal/stats"
"google.golang.org/grpc/internal/stubserver"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/stats/opentelemetry"
itestutils "google.golang.org/grpc/stats/opentelemetry/internal/testutils"
)
// setupEnv configures the environment for CSM Observability Testing. It sets
// the bootstrap env var to a bootstrap file with a nodeID provided. It sets CSM
// Env Vars as well, and mocks the resource detector's returned attribute set to
// simulate the environment. It registers a cleanup function on the provided t
// to restore the environment to its original state.
func setupEnv(t *testing.T, resourceDetectorEmissions map[string]string, meshID, csmCanonicalServiceName, csmWorkloadName string) {
oldCSMMeshID, csmMeshIDPresent := os.LookupEnv("CSM_MESH_ID")
oldCSMCanonicalServiceName, csmCanonicalServiceNamePresent := os.LookupEnv("CSM_CANONICAL_SERVICE_NAME")
oldCSMWorkloadName, csmWorkloadNamePresent := os.LookupEnv("CSM_WORKLOAD_NAME")
os.Setenv("CSM_MESH_ID", meshID)
os.Setenv("CSM_CANONICAL_SERVICE_NAME", csmCanonicalServiceName)
os.Setenv("CSM_WORKLOAD_NAME", csmWorkloadName)
var attributes []attribute.KeyValue
for k, v := range resourceDetectorEmissions {
attributes = append(attributes, attribute.String(k, v))
}
// Return the attributes configured as part of the test in place
// of reading from resource.
attrSet := attribute.NewSet(attributes...)
origGetAttrSet := getAttrSetFromResourceDetector
getAttrSetFromResourceDetector = func(context.Context) *attribute.Set {
return &attrSet
}
t.Cleanup(func() {
if csmMeshIDPresent {
os.Setenv("CSM_MESH_ID", oldCSMMeshID)
} else {
os.Unsetenv("CSM_MESH_ID")
}
if csmCanonicalServiceNamePresent {
os.Setenv("CSM_CANONICAL_SERVICE_NAME", oldCSMCanonicalServiceName)
} else {
os.Unsetenv("CSM_CANONICAL_SERVICE_NAME")
}
if csmWorkloadNamePresent {
os.Setenv("CSM_WORKLOAD_NAME", oldCSMWorkloadName)
} else {
os.Unsetenv("CSM_WORKLOAD_NAME")
}
getAttrSetFromResourceDetector = origGetAttrSet
})
}
// TestCSMPluginOptionUnary tests the CSM Plugin Option and labels. It
// configures the environment for the CSM Plugin Option to read from. It then
// configures a system with a gRPC Client and gRPC server with the OpenTelemetry
// Dial and Server Option configured with a CSM Plugin Option with a certain
// unary handler set to induce different ways of setting metadata exchange
// labels, and makes a Unary RPC. This RPC should cause certain recording for
// each registered metric observed through a Manual Metrics Reader on the
// provided OpenTelemetry SDK's Meter Provider. The CSM Labels emitted from the
// plugin option should be attached to the relevant metrics.
func (s) TestCSMPluginOptionUnary(t *testing.T) {
resourceDetectorEmissions := map[string]string{
"cloud.platform": "gcp_kubernetes_engine",
"cloud.region": "cloud_region_val", // availability_zone isn't present, so this should become location
"cloud.account.id": "cloud_account_id_val",
"k8s.namespace.name": "k8s_namespace_name_val",
"k8s.cluster.name": "k8s_cluster_name_val",
}
const meshID = "mesh_id"
const csmCanonicalServiceName = "csm_canonical_service_name"
const csmWorkloadName = "csm_workload_name"
setupEnv(t, resourceDetectorEmissions, meshID, csmCanonicalServiceName, csmWorkloadName)
attributesWant := map[string]string{
"csm.workload_canonical_service": csmCanonicalServiceName, // from env
"csm.mesh_id": "mesh_id", // from bootstrap env var
// No xDS Labels - this happens in a test below.
"csm.remote_workload_type": "gcp_kubernetes_engine",
"csm.remote_workload_canonical_service": csmCanonicalServiceName,
"csm.remote_workload_project_id": "cloud_account_id_val",
"csm.remote_workload_cluster_name": "k8s_cluster_name_val",
"csm.remote_workload_namespace_name": "k8s_namespace_name_val",
"csm.remote_workload_location": "cloud_region_val",
"csm.remote_workload_name": csmWorkloadName,
}
var csmLabels []attribute.KeyValue
for k, v := range attributesWant {
csmLabels = append(csmLabels, attribute.String(k, v))
}
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
// level that can plumb metadata exchange header in.
unaryCallFunc func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error)
opts itestutils.MetricDataOptions
}{
{
name: "normal-flow",
unaryCallFunc: func(_ context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{Payload: &testpb.Payload{
Body: make([]byte, len(in.GetPayload().GetBody())),
}}, nil
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
{
name: "trailers-only",
unaryCallFunc: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return nil, errors.New("some error") // return an error and no message - this triggers trailers only - no messages or headers sent
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCallFailed: true,
},
},
{
name: "set-header",
unaryCallFunc: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
grpc.SetHeader(ctx, metadata.New(map[string]string{"some-metadata": "some-metadata-val"}))
return &testpb.SimpleResponse{Payload: &testpb.Payload{
Body: make([]byte, len(in.GetPayload().GetBody())),
}}, nil
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
{
name: "send-header",
unaryCallFunc: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
grpc.SendHeader(ctx, metadata.New(map[string]string{"some-metadata": "some-metadata-val"}))
return &testpb.SimpleResponse{Payload: &testpb.Payload{
Body: make([]byte, len(in.GetPayload().GetBody())),
}}, nil
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
{
name: "send-msg",
unaryCallFunc: func(_ context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{Payload: &testpb.Payload{
Body: make([]byte, len(in.GetPayload().GetBody())),
}}, nil
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
UnaryCompressedMessageSize: unaryCompressedSize,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
ss := &stubserver.StubServer{UnaryCallF: test.unaryCallFunc}
po := newPluginOption(ctx)
sopts := []grpc.ServerOption{
serverOptionWithCSMPluginOption(opentelemetry.Options{
MetricsOptions: opentelemetry.MetricsOptions{
MeterProvider: provider,
Metrics: opentelemetry.DefaultMetrics(),
}}, po),
}
dopts := []grpc.DialOption{dialOptionWithCSMPluginOption(opentelemetry.Options{
MetricsOptions: opentelemetry.MetricsOptions{
MeterProvider: provider,
Metrics: opentelemetry.DefaultMetrics(),
OptionalLabels: []string{"csm.service_name", "csm.service_namespace_name"},
},
}, po)}
if err := ss.Start(sopts, dopts...); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
var request *testpb.SimpleRequest
if test.opts.UnaryCompressedMessageSize != 0 {
request = &testpb.SimpleRequest{Payload: &testpb.Payload{
Body: make([]byte, 10000),
}}
}
// Make a Unary RPC. These should cause certain metrics to be
// emitted, which should be able to be observed through the Metric
// Reader.
ss.Client.UnaryCall(ctx, request, grpc.UseCompressor(gzip.Name))
rm := &metricdata.ResourceMetrics{}
reader.Collect(ctx, rm)
gotMetrics := map[string]metricdata.Metrics{}
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
gotMetrics[m.Name] = m
}
}
opts := test.opts
opts.Target = ss.Target
wantMetrics := itestutils.MetricDataUnary(opts)
gotMetrics = itestutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics)
itestutils.CompareMetrics(t, gotMetrics, wantMetrics)
})
}
}
// TestCSMPluginOptionStreaming tests the CSM Plugin Option and labels. It
// configures the environment for the CSM Plugin Option to read from. It then
// configures a system with a gRPC Client and gRPC server with the OpenTelemetry
// Dial and Server Option configured with a CSM Plugin Option with a certain
// streaming handler set to induce different ways of setting metadata exchange
// labels, and makes a Streaming RPC. This RPC should cause certain recording
// for each registered metric observed through a Manual Metrics Reader on the
// provided OpenTelemetry SDK's Meter Provider. The CSM Labels emitted from the
// plugin option should be attached to the relevant metrics.
func (s) TestCSMPluginOptionStreaming(t *testing.T) {
resourceDetectorEmissions := map[string]string{
"cloud.platform": "gcp_kubernetes_engine",
"cloud.region": "cloud_region_val", // availability_zone isn't present, so this should become location
"cloud.account.id": "cloud_account_id_val",
"k8s.namespace.name": "k8s_namespace_name_val",
"k8s.cluster.name": "k8s_cluster_name_val",
}
const meshID = "mesh_id"
const csmCanonicalServiceName = "csm_canonical_service_name"
const csmWorkloadName = "csm_workload_name"
setupEnv(t, resourceDetectorEmissions, meshID, csmCanonicalServiceName, csmWorkloadName)
attributesWant := map[string]string{
"csm.workload_canonical_service": csmCanonicalServiceName, // from env
"csm.mesh_id": "mesh_id", // from bootstrap env var
// No xDS Labels - this happens in a test below.
"csm.remote_workload_type": "gcp_kubernetes_engine",
"csm.remote_workload_canonical_service": csmCanonicalServiceName,
"csm.remote_workload_project_id": "cloud_account_id_val",
"csm.remote_workload_cluster_name": "k8s_cluster_name_val",
"csm.remote_workload_namespace_name": "k8s_namespace_name_val",
"csm.remote_workload_location": "cloud_region_val",
"csm.remote_workload_name": csmWorkloadName,
}
var csmLabels []attribute.KeyValue
for k, v := range attributesWant {
csmLabels = append(csmLabels, attribute.String(k, v))
}
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
// interceptor level that can plumb metadata exchange header in.
streamingCallFunc func(stream testgrpc.TestService_FullDuplexCallServer) error
opts itestutils.MetricDataOptions
}{
{
name: "trailers-only",
streamingCallFunc: func(stream testgrpc.TestService_FullDuplexCallServer) error {
for {
if _, err := stream.Recv(); err == io.EOF {
return nil
}
}
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
},
},
{
name: "set-header",
streamingCallFunc: func(stream testgrpc.TestService_FullDuplexCallServer) error {
stream.SetHeader(metadata.New(map[string]string{"some-metadata": "some-metadata-val"}))
for {
if _, err := stream.Recv(); err == io.EOF {
return nil
}
}
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
},
},
{
name: "send-header",
streamingCallFunc: func(stream testgrpc.TestService_FullDuplexCallServer) error {
stream.SendHeader(metadata.New(map[string]string{"some-metadata": "some-metadata-val"}))
for {
if _, err := stream.Recv(); err == io.EOF {
return nil
}
}
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
},
},
{
name: "send-msg",
streamingCallFunc: func(stream testgrpc.TestService_FullDuplexCallServer) error {
stream.Send(&testpb.StreamingOutputCallResponse{Payload: &testpb.Payload{
Body: make([]byte, 10000),
}})
for {
if _, err := stream.Recv(); err == io.EOF {
return nil
}
}
},
opts: itestutils.MetricDataOptions{
CSMLabels: csmLabels,
StreamingCompressedMessageSize: streamingCompressedSize,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
ss := &stubserver.StubServer{FullDuplexCallF: test.streamingCallFunc}
po := newPluginOption(ctx)
sopts := []grpc.ServerOption{
serverOptionWithCSMPluginOption(opentelemetry.Options{
MetricsOptions: opentelemetry.MetricsOptions{
MeterProvider: provider,
Metrics: opentelemetry.DefaultMetrics(),
}}, po),
}
dopts := []grpc.DialOption{dialOptionWithCSMPluginOption(opentelemetry.Options{
MetricsOptions: opentelemetry.MetricsOptions{
MeterProvider: provider,
Metrics: opentelemetry.DefaultMetrics(),
OptionalLabels: []string{"csm.service_name", "csm.service_namespace_name"},
},
}, po)}
if err := ss.Start(sopts, dopts...); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
stream, err := ss.Client.FullDuplexCall(ctx, grpc.UseCompressor(gzip.Name))
if err != nil {
t.Fatalf("ss.Client.FullDuplexCall failed: %f", err)
}
if test.opts.StreamingCompressedMessageSize != 0 {
if err := stream.Send(&testpb.StreamingOutputCallRequest{Payload: &testpb.Payload{
Body: make([]byte, 10000),
}}); err != nil {
t.Fatalf("stream.Send failed")
}
if _, err := stream.Recv(); err != nil {
t.Fatalf("stream.Recv failed with error: %v", err)
}
}
stream.CloseSend()
if _, err = stream.Recv(); err != io.EOF {
t.Fatalf("stream.Recv received an unexpected error: %v, expected an EOF error", err)
}
rm := &metricdata.ResourceMetrics{}
reader.Collect(ctx, rm)
gotMetrics := map[string]metricdata.Metrics{}
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
gotMetrics[m.Name] = m
}
}
opts := test.opts
opts.Target = ss.Target
wantMetrics := itestutils.MetricDataStreaming(opts)
gotMetrics = itestutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics)
itestutils.CompareMetrics(t, gotMetrics, wantMetrics)
})
}
}
// TestXDSLabels tests that xDS Labels get emitted from OpenTelemetry metrics.
// This test configures OpenTelemetry with the CSM Plugin Option, and xDS
// Optional Labels turned on. It then configures a test balancer that updates
// labels, simulating the cluster_impl picker. It then makes a unary RPC, and
// expects xDS Labels labels to be attached to emitted relevant metrics. Full
// xDS System alongside OpenTelemetry will be tested with interop. (there is a
// test for xDS -> Stats handler and this tests -> OTel -> emission). It also
// tests the optional per call locality label in the same manner.
func (s) TestXDSLabels(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
balancerName := "o11y_balancer_xds_labels"
balancer.Register(base.NewBalancerBuilder(balancerName, &o11yPickerBuilder{}, base.Config{}))
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
ss := &stubserver.StubServer{
UnaryCallF: func(_ context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{Payload: &testpb.Payload{
Body: make([]byte, len(in.GetPayload().GetBody())),
}}, nil
},
}
po := newPluginOption(ctx)
sc := fmt.Sprintf(`{"loadBalancingConfig": [{"%s": {}}]}`, balancerName)
dopts := []grpc.DialOption{dialOptionSetCSM(opentelemetry.Options{
MetricsOptions: opentelemetry.MetricsOptions{
MeterProvider: provider,
Metrics: opentelemetry.DefaultMetrics(),
OptionalLabels: []string{"csm.service_name", "csm.service_namespace_name", "grpc.lb.locality", "grpc.lb.backend_service", "grpc.client.call.custom"},
},
}, po), grpc.WithDefaultServiceConfig(sc)}
if err := ss.Start(nil, dopts...); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
ss.Client.UnaryCall(estats.NewContextWithCustomLabel(ctx, "my-custom-label"), &testpb.SimpleRequest{Payload: &testpb.Payload{
Body: make([]byte, 10000),
}}, grpc.UseCompressor(gzip.Name))
rm := &metricdata.ResourceMetrics{}
reader.Collect(ctx, rm)
gotMetrics := map[string]metricdata.Metrics{}
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
gotMetrics[m.Name] = m
}
}
unaryMethodAttr := attribute.String("grpc.method", "grpc.testing.TestService/UnaryCall")
targetAttr := attribute.String("grpc.target", ss.Target)
unaryStatusAttr := attribute.String("grpc.status", "OK")
serviceNameAttr := attribute.String("csm.service_name", "service_name_val")
serviceNamespaceAttr := attribute.String("csm.service_namespace_name", "service_namespace_val")
localityAttr := attribute.String("grpc.lb.locality", "grpc.lb.locality_val")
backendServiceAttr := attribute.String("grpc.lb.backend_service", "grpc.lb.backend_service_val")
meshIDAttr := attribute.String("csm.mesh_id", "unknown")
workloadCanonicalServiceAttr := attribute.String("csm.workload_canonical_service", "unknown")
remoteWorkloadTypeAttr := attribute.String("csm.remote_workload_type", "unknown")
remoteWorkloadCanonicalServiceAttr := attribute.String("csm.remote_workload_canonical_service", "unknown")
customLabelAttr := attribute.String("grpc.client.call.custom", "my-custom-label")
unaryMethodClientSideEnd := []attribute.KeyValue{
unaryMethodAttr,
targetAttr,
unaryStatusAttr,
serviceNameAttr,
serviceNamespaceAttr,
localityAttr,
backendServiceAttr,
meshIDAttr,
workloadCanonicalServiceAttr,
remoteWorkloadTypeAttr,
remoteWorkloadCanonicalServiceAttr,
customLabelAttr,
}
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(unaryCompressedBytesSentRecv)
wantMetrics := []metricdata.Metrics{
{
Name: "grpc.client.attempt.started",
Description: "Number of client call attempts started.",
Unit: "{attempt}",
Data: metricdata.Sum[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Attributes: attribute.NewSet(unaryMethodAttr, targetAttr, customLabelAttr),
Value: 1,
},
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
},
}, // Doesn't have xDS Labels, CSM Labels start from header or trailer from server, whichever comes first, so this doesn't need it
{
Name: "grpc.client.attempt.duration",
Description: "End-to-end time taken to complete a client call attempt.",
Unit: "s",
Data: metricdata.Histogram[float64]{
DataPoints: []metricdata.HistogramDataPoint[float64]{
{
Attributes: attribute.NewSet(unaryMethodClientSideEnd...),
Count: 1,
Bounds: itestutils.DefaultLatencyBounds,
},
},
Temporality: metricdata.CumulativeTemporality,
},
},
{
Name: "grpc.client.attempt.sent_total_compressed_message_size",
Description: "Compressed message bytes sent per client call attempt.",
Unit: "By",
Data: metricdata.Histogram[int64]{
DataPoints: []metricdata.HistogramDataPoint[int64]{
{
Attributes: attribute.NewSet(unaryMethodClientSideEnd...),
Count: 1,
Bounds: itestutils.DefaultSizeBounds,
BucketCounts: unaryBucketCounts,
Min: unaryExtrema,
Max: unaryExtrema,
Sum: unaryCompressedBytesSentRecv,
},
},
Temporality: metricdata.CumulativeTemporality,
},
},
{
Name: "grpc.client.attempt.rcvd_total_compressed_message_size",
Description: "Compressed message bytes received per call attempt.",
Unit: "By",
Data: metricdata.Histogram[int64]{
DataPoints: []metricdata.HistogramDataPoint[int64]{
{
Attributes: attribute.NewSet(unaryMethodClientSideEnd...),
Count: 1,
Bounds: itestutils.DefaultSizeBounds,
BucketCounts: unaryBucketCounts,
Min: unaryExtrema,
Max: unaryExtrema,
Sum: unaryCompressedBytesSentRecv,
},
},
Temporality: metricdata.CumulativeTemporality,
},
},
{
Name: "grpc.client.call.duration",
Description: "Time taken by gRPC to complete an RPC from application's perspective.",
Unit: "s",
Data: metricdata.Histogram[float64]{
DataPoints: []metricdata.HistogramDataPoint[float64]{
{
Attributes: attribute.NewSet(unaryMethodAttr, targetAttr, unaryStatusAttr, customLabelAttr),
Count: 1,
Bounds: itestutils.DefaultLatencyBounds,
},
},
Temporality: metricdata.CumulativeTemporality,
},
},
}
gotMetrics = itestutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics)
itestutils.CompareMetrics(t, gotMetrics, wantMetrics)
}
// TestObservability tests that Observability global function compiles and runs
// without error. The actual functionality of this function will be verified in
// interop tests.
func (s) TestObservability(*testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
cleanup := EnableObservability(ctx, opentelemetry.Options{})
cleanup()
}
type o11yPickerBuilder struct{}
func (*o11yPickerBuilder) Build(info base.PickerBuildInfo) balancer.Picker {
return &o11yPicker{info: info}
}
type o11yPicker struct {
info base.PickerBuildInfo
}
func (p *o11yPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
// This simulates the behavior of the cluster_impl picker.
// It runs after TagRPC has already registered the default telemetry
// callback.
istats.UpdateLabels(info.Ctx, map[string]string{
"csm.service_name": "service_name_val",
"csm.service_namespace_name": "service_namespace_val",
"grpc.lb.locality": "grpc.lb.locality_val",
"grpc.lb.backend_service": "grpc.lb.backend_service_val",
})
for sc := range p.info.ReadySCs {
return balancer.PickResult{SubConn: sc}, nil
}
return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
}