|
6 | 6 | package rpc |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "context" |
| 10 | + "fmt" |
9 | 11 | "reflect" |
10 | 12 | "testing" |
| 13 | + "time" |
11 | 14 |
|
12 | 15 | "github.com/cockroachdb/cockroach/pkg/roachpb" |
13 | 16 | "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
14 | 18 | io_prometheus_client "github.com/prometheus/client_model/go" |
15 | 19 | "github.com/stretchr/testify/require" |
| 20 | + "google.golang.org/grpc" |
| 21 | + "google.golang.org/grpc/codes" |
| 22 | + "google.golang.org/grpc/status" |
16 | 23 | ) |
17 | 24 |
|
18 | 25 | // TestMetricsRelease verifies that peerMetrics.release() removes tracking for |
@@ -82,3 +89,84 @@ func TestMetricsRelease(t *testing.T) { |
82 | 89 | // We added one extra peer and one extra locality, verify counts. |
83 | 90 | require.Equal(t, expectedCount, verifyAllFields(m, 2)) |
84 | 91 | } |
| 92 | + |
| 93 | +func TestServerRequestInstrumentInterceptor(t *testing.T) { |
| 94 | + defer leaktest.AfterTest(t)() |
| 95 | + defer log.Scope(t).Close(t) |
| 96 | + |
| 97 | + requestMetrics := NewRequestMetrics() |
| 98 | + |
| 99 | + ctx := context.Background() |
| 100 | + req := struct{}{} |
| 101 | + |
| 102 | + testcase := []struct { |
| 103 | + methodName string |
| 104 | + statusCode codes.Code |
| 105 | + shouldRecord bool |
| 106 | + }{ |
| 107 | + {"rpc/test/method", codes.OK, true}, |
| 108 | + {"rpc/test/method", codes.Internal, true}, |
| 109 | + {"rpc/test/method", codes.Aborted, true}, |
| 110 | + {"rpc/test/notRecorded", codes.OK, false}, |
| 111 | + } |
| 112 | + |
| 113 | + for _, tc := range testcase { |
| 114 | + t.Run(fmt.Sprintf("%s %s", tc.methodName, tc.statusCode), func(t *testing.T) { |
| 115 | + info := &grpc.UnaryServerInfo{FullMethod: tc.methodName} |
| 116 | + handler := func(ctx context.Context, req interface{}) (interface{}, error) { |
| 117 | + if tc.statusCode == codes.OK { |
| 118 | + time.Sleep(time.Millisecond) |
| 119 | + return struct{}{}, nil |
| 120 | + } |
| 121 | + return nil, status.Error(tc.statusCode, tc.statusCode.String()) |
| 122 | + } |
| 123 | + interceptor := NewRequestMetricsInterceptor(requestMetrics, func(fullMethodName string) bool { |
| 124 | + return tc.shouldRecord |
| 125 | + }) |
| 126 | + _, err := interceptor(ctx, req, info, handler) |
| 127 | + if err != nil { |
| 128 | + require.Equal(t, tc.statusCode, status.Code(err)) |
| 129 | + } |
| 130 | + var expectedCount uint64 |
| 131 | + if tc.shouldRecord { |
| 132 | + expectedCount = 1 |
| 133 | + } |
| 134 | + assertGrpcMetrics(t, requestMetrics.Duration.ToPrometheusMetrics(), map[string]uint64{ |
| 135 | + fmt.Sprintf("%s %s", tc.methodName, tc.statusCode): expectedCount, |
| 136 | + }) |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func assertGrpcMetrics( |
| 142 | + t *testing.T, metrics []*io_prometheus_client.Metric, expected map[string]uint64, |
| 143 | +) { |
| 144 | + t.Helper() |
| 145 | + actual := map[string]*io_prometheus_client.Histogram{} |
| 146 | + for _, m := range metrics { |
| 147 | + var method, statusCode string |
| 148 | + for _, l := range m.Label { |
| 149 | + switch *l.Name { |
| 150 | + case RpcMethodLabel: |
| 151 | + method = *l.Value |
| 152 | + case RpcStatusCodeLabel: |
| 153 | + statusCode = *l.Value |
| 154 | + } |
| 155 | + } |
| 156 | + histogram := m.Histogram |
| 157 | + require.NotNil(t, histogram, "expected histogram") |
| 158 | + key := fmt.Sprintf("%s %s", method, statusCode) |
| 159 | + actual[key] = histogram |
| 160 | + } |
| 161 | + |
| 162 | + for key, val := range expected { |
| 163 | + histogram, ok := actual[key] |
| 164 | + if val == 0 { |
| 165 | + require.False(t, ok, "expected `%s` to not exist", key) |
| 166 | + } else { |
| 167 | + require.True(t, ok) |
| 168 | + require.Greater(t, *histogram.SampleSum, float64(0), "expected `%s` to have a SampleSum > 0", key) |
| 169 | + require.Equal(t, val, *histogram.SampleCount, "expected `%s` to have SampleCount of %d", key, val) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments