@@ -22,6 +22,7 @@ import (
2222 "testing"
2323 "time"
2424
25+ "vitess.io/vitess/go/stats"
2526 "vitess.io/vitess/go/vt/log"
2627 querypb "vitess.io/vitess/go/vt/proto/query"
2728 topodatapb "vitess.io/vitess/go/vt/proto/topodata"
@@ -157,16 +158,18 @@ func TestQueryThrottler_Shutdown(t *testing.T) {
157158 require .NotNil (t , strategy )
158159}
159160
160- // TestIncomingQueryThrottler_DryRunMode tests that dry-run mode logs decisions but doesn't throttle queries.
161- func TestIncomingQueryThrottler_DryRunMode (t * testing.T ) {
161+ // TestQueryThrottler_DryRunMode tests that dry-run mode logs decisions but doesn't throttle queries.
162+ func TestQueryThrottler_DryRunMode (t * testing.T ) {
162163 tests := []struct {
163- name string
164- enabled bool
165- dryRun bool
166- throttleDecision registry.ThrottleDecision
167- expectError bool
168- expectDryRunLog bool
169- expectedLogMsg string
164+ name string
165+ enabled bool
166+ dryRun bool
167+ throttleDecision registry.ThrottleDecision
168+ expectError bool
169+ expectDryRunLog bool
170+ expectedLogMsg string
171+ expectedTotalRequests int64
172+ expectedThrottledRequests int64
170173 }{
171174 {
172175 name : "Disabled throttler - no checks performed" ,
@@ -198,8 +201,9 @@ func TestIncomingQueryThrottler_DryRunMode(t *testing.T) {
198201 Throttle : false ,
199202 Message : "Query allowed" ,
200203 },
201- expectError : false ,
202- expectDryRunLog : false ,
204+ expectError : false ,
205+ expectDryRunLog : false ,
206+ expectedTotalRequests : 1 ,
203207 },
204208 {
205209 name : "Normal mode - query throttled" ,
@@ -213,8 +217,10 @@ func TestIncomingQueryThrottler_DryRunMode(t *testing.T) {
213217 Threshold : 80.0 ,
214218 ThrottlePercentage : 1.0 ,
215219 },
216- expectError : true ,
217- expectDryRunLog : false ,
220+ expectError : true ,
221+ expectDryRunLog : false ,
222+ expectedTotalRequests : 1 ,
223+ expectedThrottledRequests : 1 ,
218224 },
219225 {
220226 name : "Dry-run mode - query would be throttled but allowed" ,
@@ -228,9 +234,11 @@ func TestIncomingQueryThrottler_DryRunMode(t *testing.T) {
228234 Threshold : 80.0 ,
229235 ThrottlePercentage : 1.0 ,
230236 },
231- expectError : false ,
232- expectDryRunLog : true ,
233- expectedLogMsg : "[DRY-RUN] Query throttled: metric=cpu value=95.0 threshold=80.0" ,
237+ expectError : false ,
238+ expectDryRunLog : true ,
239+ expectedLogMsg : "[DRY-RUN] Query throttled: metric=cpu value=95.0 threshold=80.0, metric name: cpu, metric value: 95.000000" ,
240+ expectedTotalRequests : 1 ,
241+ expectedThrottledRequests : 1 ,
234242 },
235243 {
236244 name : "Dry-run mode - query allowed normally" ,
@@ -240,8 +248,9 @@ func TestIncomingQueryThrottler_DryRunMode(t *testing.T) {
240248 Throttle : false ,
241249 Message : "Query allowed" ,
242250 },
243- expectError : false ,
244- expectDryRunLog : false ,
251+ expectError : false ,
252+ expectDryRunLog : false ,
253+ expectedTotalRequests : 1 ,
245254 },
246255 }
247256
@@ -252,6 +261,8 @@ func TestIncomingQueryThrottler_DryRunMode(t *testing.T) {
252261 decision : tt .throttleDecision ,
253262 }
254263
264+ env := tabletenv .NewEnv (vtenv .NewTestEnv (), & tabletenv.TabletConfig {}, "TestThrottler" )
265+
255266 // Create throttler with controlled config
256267 iqt := & QueryThrottler {
257268 ctx : context .Background (),
@@ -260,8 +271,18 @@ func TestIncomingQueryThrottler_DryRunMode(t *testing.T) {
260271 DryRun : tt .dryRun ,
261272 },
262273 strategy : mockStrategy ,
274+ env : env ,
275+ stats : Stats {
276+ requestsTotal : env .Exporter ().NewCountersWithMultiLabels (queryThrottlerAppName + "Requests" , "TestThrottler requests" , []string {"Strategy" , "Workload" , "Priority" }),
277+ requestsThrottled : env .Exporter ().NewCountersWithMultiLabels (queryThrottlerAppName + "Throttled" , "TestThrottler throttled" , []string {"Strategy" , "Workload" , "Priority" , "MetricName" , "MetricValue" , "DryRun" }),
278+ totalLatency : env .Exporter ().NewMultiTimings (queryThrottlerAppName + "TotalLatencyMs" , "Total latency of QueryThrottler.Throttle in milliseconds" , []string {"Strategy" , "Workload" , "Priority" }),
279+ evaluateLatency : env .Exporter ().NewMultiTimings (queryThrottlerAppName + "EvaluateLatencyMs" , "Latency from Throttle entry to completion of Evaluate in milliseconds" , []string {"Strategy" , "Workload" , "Priority" }),
280+ },
263281 }
264282
283+ iqt .stats .requestsTotal .ResetAll ()
284+ iqt .stats .requestsThrottled .ResetAll ()
285+
265286 // Capture log output
266287 logCapture := & testLogCapture {}
267288 originalLogWarningf := log .Warningf
@@ -299,6 +320,12 @@ func TestIncomingQueryThrottler_DryRunMode(t *testing.T) {
299320 } else {
300321 require .Empty (t , logCapture .logs , "Expected no log messages" )
301322 }
323+
324+ // Verify stats expectation
325+ totalRequests := stats .CounterForDimension (iqt .stats .requestsTotal , "Strategy" )
326+ throttledRequests := stats .CounterForDimension (iqt .stats .requestsThrottled , "Strategy" )
327+ require .Equal (t , tt .expectedTotalRequests , totalRequests .Counts ()["MockStrategy" ], "Total requests should match expected" )
328+ require .Equal (t , tt .expectedThrottledRequests , throttledRequests .Counts ()["MockStrategy" ], "Throttled requests should match expected" )
302329 })
303330 }
304331}
0 commit comments