|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2024 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +// Package test implements an e2e test for the Metrics Recorder List component |
| 20 | +// of the Client Conn, and a TestMetricsRecorder utility. |
| 21 | +package test |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "log" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "google.golang.org/grpc" |
| 30 | + "google.golang.org/grpc/credentials/insecure" |
| 31 | + estats "google.golang.org/grpc/experimental/stats" |
| 32 | + "google.golang.org/grpc/internal" |
| 33 | + "google.golang.org/grpc/internal/grpctest" |
| 34 | + testgrpc "google.golang.org/grpc/interop/grpc_testing" |
| 35 | + testpb "google.golang.org/grpc/interop/grpc_testing" |
| 36 | + "google.golang.org/grpc/resolver" |
| 37 | + "google.golang.org/grpc/resolver/manual" |
| 38 | + "google.golang.org/grpc/serviceconfig" |
| 39 | +) |
| 40 | + |
| 41 | +var defaultTestTimeout = 5 * time.Second |
| 42 | + |
| 43 | +type s struct { |
| 44 | + grpctest.Tester |
| 45 | +} |
| 46 | + |
| 47 | +func Test(t *testing.T) { |
| 48 | + grpctest.RunSubTests(t, s{}) |
| 49 | +} |
| 50 | + |
| 51 | +// TestMetricsRecorderList tests the metrics recorder list functionality of the |
| 52 | +// ClientConn. It configures a global and local stats handler Dial Option. These |
| 53 | +// stats handlers implement the MetricsRecorder interface. It also configures a |
| 54 | +// balancer which registers metrics and records on metrics at build time. This |
| 55 | +// test then asserts that the recorded metrics show up on both configured stats |
| 56 | +// handlers, and that metrics calls with the incorrect number of labels do not |
| 57 | +// make their way to stats handlers. |
| 58 | +func (s) TestMetricsRecorderList(t *testing.T) { |
| 59 | + mr := manual.NewBuilderWithScheme("test-metrics-recorder-list") |
| 60 | + defer mr.Close() |
| 61 | + |
| 62 | + json := `{"loadBalancingConfig": [{"recording_load_balancer":{}}]}` |
| 63 | + sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(json) |
| 64 | + mr.InitialState(resolver.State{ |
| 65 | + ServiceConfig: sc, |
| 66 | + }) |
| 67 | + |
| 68 | + // Create two stats.Handlers which also implement MetricsRecorder, configure |
| 69 | + // one as a global dial option and one as a local dial option. |
| 70 | + mr1 := NewTestMetricsRecorder(t, []string{}) |
| 71 | + mr2 := NewTestMetricsRecorder(t, []string{}) |
| 72 | + |
| 73 | + defer internal.ClearGlobalDialOptions() |
| 74 | + internal.AddGlobalDialOptions.(func(opt ...grpc.DialOption))(grpc.WithStatsHandler(mr1)) |
| 75 | + |
| 76 | + cc, err := grpc.NewClient(mr.Scheme()+":///", grpc.WithResolvers(mr), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(mr2)) |
| 77 | + if err != nil { |
| 78 | + log.Fatalf("Failed to dial: %v", err) |
| 79 | + } |
| 80 | + defer cc.Close() |
| 81 | + |
| 82 | + tsc := testgrpc.NewTestServiceClient(cc) |
| 83 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 84 | + defer cancel() |
| 85 | + |
| 86 | + // Trigger the recording_load_balancer to build, which will trigger metrics |
| 87 | + // to record. |
| 88 | + tsc.UnaryCall(ctx, &testpb.SimpleRequest{}) |
| 89 | + |
| 90 | + mdWant := MetricsData{ |
| 91 | + Handle: (*estats.MetricDescriptor)(intCountHandle), |
| 92 | + IntIncr: 1, |
| 93 | + LabelKeys: []string{"int counter label", "int counter optional label"}, |
| 94 | + LabelVals: []string{"int counter label val", "int counter optional label val"}, |
| 95 | + } |
| 96 | + mr1.WaitForInt64Count(ctx, mdWant) |
| 97 | + mr2.WaitForInt64Count(ctx, mdWant) |
| 98 | + |
| 99 | + mdWant = MetricsData{ |
| 100 | + Handle: (*estats.MetricDescriptor)(floatCountHandle), |
| 101 | + FloatIncr: 2, |
| 102 | + LabelKeys: []string{"float counter label", "float counter optional label"}, |
| 103 | + LabelVals: []string{"float counter label val", "float counter optional label val"}, |
| 104 | + } |
| 105 | + mr1.WaitForFloat64Count(ctx, mdWant) |
| 106 | + mr2.WaitForFloat64Count(ctx, mdWant) |
| 107 | + |
| 108 | + mdWant = MetricsData{ |
| 109 | + Handle: (*estats.MetricDescriptor)(intHistoHandle), |
| 110 | + IntIncr: 3, |
| 111 | + LabelKeys: []string{"int histo label", "int histo optional label"}, |
| 112 | + LabelVals: []string{"int histo label val", "int histo optional label val"}, |
| 113 | + } |
| 114 | + mr1.WaitForInt64Histo(ctx, mdWant) |
| 115 | + mr2.WaitForInt64Histo(ctx, mdWant) |
| 116 | + |
| 117 | + mdWant = MetricsData{ |
| 118 | + Handle: (*estats.MetricDescriptor)(floatHistoHandle), |
| 119 | + FloatIncr: 4, |
| 120 | + LabelKeys: []string{"float histo label", "float histo optional label"}, |
| 121 | + LabelVals: []string{"float histo label val", "float histo optional label val"}, |
| 122 | + } |
| 123 | + mr1.WaitForFloat64Histo(ctx, mdWant) |
| 124 | + mr2.WaitForFloat64Histo(ctx, mdWant) |
| 125 | + mdWant = MetricsData{ |
| 126 | + Handle: (*estats.MetricDescriptor)(intGaugeHandle), |
| 127 | + IntIncr: 5, // Should ignore the 7 metrics recording point because emits wrong number of labels. |
| 128 | + LabelKeys: []string{"int gauge label", "int gauge optional label"}, |
| 129 | + LabelVals: []string{"int gauge label val", "int gauge optional label val"}, |
| 130 | + } |
| 131 | + mr1.WaitForInt64Gauge(ctx, mdWant) |
| 132 | + mr2.WaitForInt64Gauge(ctx, mdWant) |
| 133 | +} |
0 commit comments