@@ -29,6 +29,11 @@ import (
29
29
"k8s.io/component-base/metrics/testutil"
30
30
)
31
31
32
+ const (
33
+ SidecarOperationMetric = "csi_sidecar_operations_seconds"
34
+ ProcessStartTimeMetric = "process_start_time_seconds"
35
+ )
36
+
32
37
func TestRecordMetrics (t * testing.T ) {
33
38
testcases := map [string ]struct {
34
39
subsystem string
@@ -102,15 +107,17 @@ func testRecordMetrics(t *testing.T, subsystem string, stabilityLevel metrics.St
102
107
csi_sidecar_operations_seconds_sum{driver_name="fake.csi.driver.io",grpc_status_code="OK",method_name="/csi.v1.Controller/ControllerGetCapabilities"} 20
103
108
csi_sidecar_operations_seconds_count{driver_name="fake.csi.driver.io",grpc_status_code="OK",method_name="/csi.v1.Controller/ControllerGetCapabilities"} 1
104
109
`
110
+ metricName := SidecarOperationMetric
105
111
if subsystem != "" {
106
112
expectedMetrics = strings .Replace (expectedMetrics , "csi_sidecar" , subsystem , - 1 )
113
+ metricName = strings .Replace (metricName , "csi_sidecar" , subsystem , - 1 )
107
114
}
108
115
if stabilityLevel != "" {
109
116
expectedMetrics = strings .Replace (expectedMetrics , "ALPHA" , string (stabilityLevel ), - 1 )
110
117
}
111
118
112
119
if err := testutil .GatherAndCompare (
113
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
120
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), metricName ); err != nil {
114
121
t .Fatal (err )
115
122
}
116
123
}
@@ -150,8 +157,9 @@ func TestFixedLabels(t *testing.T) {
150
157
csi_sidecar_operations_seconds_count{a="111",b="222",driver_name="unknown-driver",grpc_status_code="OK",method_name="myOperation"} 1
151
158
`
152
159
160
+ time .Sleep (1000 )
153
161
if err := testutil .GatherAndCompare (
154
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
162
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
155
163
t .Fatal (err )
156
164
}
157
165
}
@@ -200,7 +208,7 @@ func TestVaryingLabels(t *testing.T) {
200
208
`
201
209
202
210
if err := testutil .GatherAndCompare (
203
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
211
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
204
212
t .Fatal (err )
205
213
}
206
214
}
@@ -273,7 +281,7 @@ func TestTwoVaryingLabels(t *testing.T) {
273
281
`
274
282
275
283
if err := testutil .GatherAndCompare (
276
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
284
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
277
285
t .Fatal (err )
278
286
}
279
287
}
@@ -318,7 +326,7 @@ func TestVaryingLabelsBackfill(t *testing.T) {
318
326
`
319
327
320
328
if err := testutil .GatherAndCompare (
321
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
329
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
322
330
t .Fatal (err )
323
331
}
324
332
}
@@ -392,7 +400,7 @@ func TestCombinedLabels(t *testing.T) {
392
400
`
393
401
394
402
if err := testutil .GatherAndCompare (
395
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
403
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
396
404
t .Fatal (err )
397
405
}
398
406
}
@@ -431,7 +439,7 @@ func TestRecordMetrics_NoDriverName(t *testing.T) {
431
439
`
432
440
433
441
if err := testutil .GatherAndCompare (
434
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
442
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
435
443
t .Fatal (err )
436
444
}
437
445
}
@@ -469,7 +477,7 @@ func TestRecordMetrics_Negative(t *testing.T) {
469
477
csi_sidecar_operations_seconds_count{driver_name="fake.csi.driver.io",grpc_status_code="InvalidArgument",method_name="myOperation"} 1
470
478
`
471
479
if err := testutil .GatherAndCompare (
472
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
480
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
473
481
t .Fatal (err )
474
482
}
475
483
}
@@ -505,6 +513,7 @@ func TestStartMetricsEndPoint_Noop(t *testing.T) {
505
513
if err != nil {
506
514
t .Fatalf ("Failed to parse metrics response. Response was: %+v Error: %v" , resp , err )
507
515
}
516
+ actualMetrics := string (contentBytes )
508
517
509
518
expectedMetrics := `# HELP csi_sidecar_operations_seconds [ALPHA] Container Storage Interface operation duration with gRPC error code status total
510
519
# TYPE csi_sidecar_operations_seconds histogram
@@ -524,10 +533,37 @@ func TestStartMetricsEndPoint_Noop(t *testing.T) {
524
533
csi_sidecar_operations_seconds_bucket{driver_name="fake.csi.driver.io",grpc_status_code="OK",method_name="/csi.v1.Controller/ControllerGetCapabilities",le="+Inf"} 1
525
534
csi_sidecar_operations_seconds_sum{driver_name="fake.csi.driver.io",grpc_status_code="OK",method_name="/csi.v1.Controller/ControllerGetCapabilities"} 20
526
535
csi_sidecar_operations_seconds_count{driver_name="fake.csi.driver.io",grpc_status_code="OK",method_name="/csi.v1.Controller/ControllerGetCapabilities"} 1
527
- `
536
+ `
528
537
529
- actualMetrics := string (contentBytes )
530
- if err := VerifyMetricsMatch (expectedMetrics , actualMetrics , "" ); err != nil {
538
+ if err := VerifyMetricsMatch (expectedMetrics , actualMetrics , ProcessStartTimeMetric ); err != nil {
531
539
t .Fatalf ("Metrics returned by end point do not match expectation: %v" , err )
532
540
}
533
541
}
542
+
543
+ func TestProcessStartTimeMetricExist (t * testing.T ) {
544
+ // Arrange
545
+ cmm := NewCSIMetricsManagerForSidecar (
546
+ "fake.csi.driver.io" /* driverName */ )
547
+ operationDuration , _ := time .ParseDuration ("20s" )
548
+
549
+ // Act
550
+ cmm .RecordMetrics (
551
+ "/csi.v1.Controller/ControllerGetCapabilities" , /* operationName */
552
+ nil , /* operationErr */
553
+ operationDuration /* operationDuration */ )
554
+
555
+ // Assert
556
+ metricsFamilies , err := cmm .GetRegistry ().Gather ()
557
+ if err != nil {
558
+ t .Fatalf ("Error fetching metrics: %v" , err )
559
+ }
560
+
561
+ // check process_start_time_seconds exist
562
+ for _ , metricsFamily := range metricsFamilies {
563
+ if metricsFamily .GetName () == ProcessStartTimeMetric {
564
+ return
565
+ }
566
+ }
567
+
568
+ t .Fatalf ("Metrics does not contain %v. Scraped content: %v" , ProcessStartTimeMetric , metricsFamilies )
569
+ }
0 commit comments