|
| 1 | +// Copyright (c) The go-grpc-middleware Authors. |
| 2 | +// Licensed under the Apache License 2.0. |
| 3 | + |
| 4 | +package prometheus |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors" |
| 12 | + "github.com/prometheus/client_golang/prometheus" |
| 13 | + dto "github.com/prometheus/client_model/go" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "google.golang.org/grpc/codes" |
| 17 | +) |
| 18 | + |
| 19 | +func TestContextLabels(t *testing.T) { |
| 20 | + // Create server metrics with context labels |
| 21 | + serverMetrics := NewServerMetrics( |
| 22 | + WithContextLabels("user_id", "tenant_id"), |
| 23 | + ) |
| 24 | + |
| 25 | + // Create a custom registry to isolate this test |
| 26 | + reg := prometheus.NewRegistry() |
| 27 | + reg.MustRegister(serverMetrics) |
| 28 | + |
| 29 | + // Create a mock context with label values |
| 30 | + ctx := context.Background() |
| 31 | + |
| 32 | + // Create the labels extraction function |
| 33 | + labelsFromCtx := func(ctx context.Context) prometheus.Labels { |
| 34 | + return prometheus.Labels{ |
| 35 | + "user_id": "user123", |
| 36 | + "tenant_id": "tenant456", |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Create a reporter with the labels function |
| 41 | + rep := &reportable{ |
| 42 | + serverMetrics: serverMetrics, |
| 43 | + opts: []Option{WithLabelsFromContext(labelsFromCtx)}, |
| 44 | + } |
| 45 | + |
| 46 | + // Simulate a server call |
| 47 | + meta := interceptors.CallMeta{ |
| 48 | + Typ: interceptors.Unary, |
| 49 | + Service: "testpb.PingService", |
| 50 | + Method: "Ping", |
| 51 | + } |
| 52 | + |
| 53 | + reporter, _ := rep.ServerReporter(ctx, meta) |
| 54 | + |
| 55 | + // Simulate call completion |
| 56 | + reporter.PostCall(nil, time.Millisecond*100) |
| 57 | + |
| 58 | + // Collect metrics |
| 59 | + metricFamilies, err := reg.Gather() |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + // Find the handled counter metric |
| 63 | + var handledCounter *dto.MetricFamily |
| 64 | + for _, mf := range metricFamilies { |
| 65 | + if *mf.Name == "grpc_server_handled_total" { |
| 66 | + handledCounter = mf |
| 67 | + break |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + require.NotNil(t, handledCounter, "Should find grpc_server_handled_total metric") |
| 72 | + require.Len(t, handledCounter.Metric, 1, "Should have one metric sample") |
| 73 | + |
| 74 | + // Verify the metric has all expected labels |
| 75 | + metric := handledCounter.Metric[0] |
| 76 | + labelMap := make(map[string]string) |
| 77 | + for _, label := range metric.Label { |
| 78 | + labelMap[*label.Name] = *label.Value |
| 79 | + } |
| 80 | + |
| 81 | + // Check standard labels |
| 82 | + assert.Equal(t, "unary", labelMap["grpc_type"]) |
| 83 | + assert.Equal(t, "testpb.PingService", labelMap["grpc_service"]) |
| 84 | + assert.Equal(t, "Ping", labelMap["grpc_method"]) |
| 85 | + assert.Equal(t, codes.OK.String(), labelMap["grpc_code"]) |
| 86 | + |
| 87 | + // Check context labels |
| 88 | + assert.Equal(t, "user123", labelMap["user_id"]) |
| 89 | + assert.Equal(t, "tenant456", labelMap["tenant_id"]) |
| 90 | + |
| 91 | + // Verify metric value |
| 92 | + assert.Equal(t, float64(1), *metric.Counter.Value) |
| 93 | +} |
| 94 | + |
| 95 | +func TestContextLabelsWithMissingValues(t *testing.T) { |
| 96 | + // Create server metrics with context labels |
| 97 | + serverMetrics := NewServerMetrics( |
| 98 | + WithContextLabels("user_id", "missing_label"), |
| 99 | + ) |
| 100 | + |
| 101 | + // Create a custom registry to isolate this test |
| 102 | + reg := prometheus.NewRegistry() |
| 103 | + reg.MustRegister(serverMetrics) |
| 104 | + |
| 105 | + // Create a mock context with only partial label values |
| 106 | + ctx := context.Background() |
| 107 | + |
| 108 | + // Create the labels extraction function that only returns one label |
| 109 | + labelsFromCtx := func(ctx context.Context) prometheus.Labels { |
| 110 | + return prometheus.Labels{ |
| 111 | + "user_id": "user123", |
| 112 | + // missing_label is not provided |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Create a reporter with the labels function |
| 117 | + rep := &reportable{ |
| 118 | + serverMetrics: serverMetrics, |
| 119 | + opts: []Option{WithLabelsFromContext(labelsFromCtx)}, |
| 120 | + } |
| 121 | + |
| 122 | + // Simulate a server call |
| 123 | + meta := interceptors.CallMeta{ |
| 124 | + Typ: interceptors.Unary, |
| 125 | + Service: "testpb.PingService", |
| 126 | + Method: "Ping", |
| 127 | + } |
| 128 | + |
| 129 | + reporter, _ := rep.ServerReporter(ctx, meta) |
| 130 | + |
| 131 | + // Simulate call completion |
| 132 | + reporter.PostCall(nil, time.Millisecond*100) |
| 133 | + |
| 134 | + // Collect metrics |
| 135 | + metricFamilies, err := reg.Gather() |
| 136 | + require.NoError(t, err) |
| 137 | + |
| 138 | + // Find the handled counter metric |
| 139 | + var handledCounter *dto.MetricFamily |
| 140 | + for _, mf := range metricFamilies { |
| 141 | + if *mf.Name == "grpc_server_handled_total" { |
| 142 | + handledCounter = mf |
| 143 | + break |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + require.NotNil(t, handledCounter, "Should find grpc_server_handled_total metric") |
| 148 | + require.Len(t, handledCounter.Metric, 1, "Should have one metric sample") |
| 149 | + |
| 150 | + // Verify the metric has all expected labels |
| 151 | + metric := handledCounter.Metric[0] |
| 152 | + labelMap := make(map[string]string) |
| 153 | + for _, label := range metric.Label { |
| 154 | + labelMap[*label.Name] = *label.Value |
| 155 | + } |
| 156 | + |
| 157 | + // Check standard labels |
| 158 | + assert.Equal(t, "unary", labelMap["grpc_type"]) |
| 159 | + assert.Equal(t, "testpb.PingService", labelMap["grpc_service"]) |
| 160 | + assert.Equal(t, "Ping", labelMap["grpc_method"]) |
| 161 | + assert.Equal(t, codes.OK.String(), labelMap["grpc_code"]) |
| 162 | + |
| 163 | + // Check context labels - user_id should be present, missing_label should be empty |
| 164 | + assert.Equal(t, "user123", labelMap["user_id"]) |
| 165 | + assert.Equal(t, "", labelMap["missing_label"]) |
| 166 | + |
| 167 | + // Verify metric value |
| 168 | + assert.Equal(t, float64(1), *metric.Counter.Value) |
| 169 | +} |
| 170 | + |
| 171 | +func TestContextLabelsWithHistogram(t *testing.T) { |
| 172 | + // Create server metrics with context labels and histogram enabled |
| 173 | + serverMetrics := NewServerMetrics( |
| 174 | + WithContextLabels("user_id"), |
| 175 | + WithServerHandlingTimeHistogram(), |
| 176 | + ) |
| 177 | + |
| 178 | + // Create a custom registry to isolate this test |
| 179 | + reg := prometheus.NewRegistry() |
| 180 | + reg.MustRegister(serverMetrics) |
| 181 | + |
| 182 | + // Create a mock context with label values |
| 183 | + ctx := context.Background() |
| 184 | + |
| 185 | + // Create the labels extraction function |
| 186 | + labelsFromCtx := func(ctx context.Context) prometheus.Labels { |
| 187 | + return prometheus.Labels{ |
| 188 | + "user_id": "user123", |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // Create a reporter with the labels function |
| 193 | + rep := &reportable{ |
| 194 | + serverMetrics: serverMetrics, |
| 195 | + opts: []Option{WithLabelsFromContext(labelsFromCtx)}, |
| 196 | + } |
| 197 | + |
| 198 | + // Simulate a server call |
| 199 | + meta := interceptors.CallMeta{ |
| 200 | + Typ: interceptors.Unary, |
| 201 | + Service: "testpb.PingService", |
| 202 | + Method: "Ping", |
| 203 | + } |
| 204 | + |
| 205 | + reporter, _ := rep.ServerReporter(ctx, meta) |
| 206 | + |
| 207 | + // Simulate call completion |
| 208 | + reporter.PostCall(nil, time.Millisecond*100) |
| 209 | + |
| 210 | + // Collect metrics |
| 211 | + metricFamilies, err := reg.Gather() |
| 212 | + require.NoError(t, err) |
| 213 | + |
| 214 | + // Find the histogram metric |
| 215 | + var histogram *dto.MetricFamily |
| 216 | + for _, mf := range metricFamilies { |
| 217 | + if *mf.Name == "grpc_server_handling_seconds" { |
| 218 | + histogram = mf |
| 219 | + break |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + require.NotNil(t, histogram, "Should find grpc_server_handling_seconds metric") |
| 224 | + require.Len(t, histogram.Metric, 1, "Should have one metric sample") |
| 225 | + |
| 226 | + // Verify the histogram has all expected labels |
| 227 | + metric := histogram.Metric[0] |
| 228 | + labelMap := make(map[string]string) |
| 229 | + for _, label := range metric.Label { |
| 230 | + labelMap[*label.Name] = *label.Value |
| 231 | + } |
| 232 | + |
| 233 | + // Check standard labels |
| 234 | + assert.Equal(t, "unary", labelMap["grpc_type"]) |
| 235 | + assert.Equal(t, "testpb.PingService", labelMap["grpc_service"]) |
| 236 | + assert.Equal(t, "Ping", labelMap["grpc_method"]) |
| 237 | + |
| 238 | + // Check context labels |
| 239 | + assert.Equal(t, "user123", labelMap["user_id"]) |
| 240 | + |
| 241 | + // Verify histogram has recorded a sample |
| 242 | + assert.Equal(t, uint64(1), *metric.Histogram.SampleCount) |
| 243 | +} |
0 commit comments