@@ -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
}
@@ -151,7 +158,7 @@ func TestFixedLabels(t *testing.T) {
151
158
`
152
159
153
160
if err := testutil .GatherAndCompare (
154
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
161
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
155
162
t .Fatal (err )
156
163
}
157
164
}
@@ -200,7 +207,7 @@ func TestVaryingLabels(t *testing.T) {
200
207
`
201
208
202
209
if err := testutil .GatherAndCompare (
203
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
210
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
204
211
t .Fatal (err )
205
212
}
206
213
}
@@ -273,7 +280,7 @@ func TestTwoVaryingLabels(t *testing.T) {
273
280
`
274
281
275
282
if err := testutil .GatherAndCompare (
276
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
283
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
277
284
t .Fatal (err )
278
285
}
279
286
}
@@ -318,7 +325,7 @@ func TestVaryingLabelsBackfill(t *testing.T) {
318
325
`
319
326
320
327
if err := testutil .GatherAndCompare (
321
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
328
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
322
329
t .Fatal (err )
323
330
}
324
331
}
@@ -392,7 +399,7 @@ func TestCombinedLabels(t *testing.T) {
392
399
`
393
400
394
401
if err := testutil .GatherAndCompare (
395
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
402
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
396
403
t .Fatal (err )
397
404
}
398
405
}
@@ -431,7 +438,7 @@ func TestRecordMetrics_NoDriverName(t *testing.T) {
431
438
`
432
439
433
440
if err := testutil .GatherAndCompare (
434
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
441
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
435
442
t .Fatal (err )
436
443
}
437
444
}
@@ -469,7 +476,7 @@ func TestRecordMetrics_Negative(t *testing.T) {
469
476
csi_sidecar_operations_seconds_count{driver_name="fake.csi.driver.io",grpc_status_code="InvalidArgument",method_name="myOperation"} 1
470
477
`
471
478
if err := testutil .GatherAndCompare (
472
- cmm .GetRegistry (), strings .NewReader (expectedMetrics )); err != nil {
479
+ cmm .GetRegistry (), strings .NewReader (expectedMetrics ), SidecarOperationMetric ); err != nil {
473
480
t .Fatal (err )
474
481
}
475
482
}
@@ -505,6 +512,7 @@ func TestStartMetricsEndPoint_Noop(t *testing.T) {
505
512
if err != nil {
506
513
t .Fatalf ("Failed to parse metrics response. Response was: %+v Error: %v" , resp , err )
507
514
}
515
+ actualMetrics := string (contentBytes )
508
516
509
517
expectedMetrics := `# HELP csi_sidecar_operations_seconds [ALPHA] Container Storage Interface operation duration with gRPC error code status total
510
518
# TYPE csi_sidecar_operations_seconds histogram
@@ -524,10 +532,37 @@ func TestStartMetricsEndPoint_Noop(t *testing.T) {
524
532
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
533
csi_sidecar_operations_seconds_sum{driver_name="fake.csi.driver.io",grpc_status_code="OK",method_name="/csi.v1.Controller/ControllerGetCapabilities"} 20
526
534
csi_sidecar_operations_seconds_count{driver_name="fake.csi.driver.io",grpc_status_code="OK",method_name="/csi.v1.Controller/ControllerGetCapabilities"} 1
527
- `
535
+ `
528
536
529
- actualMetrics := string (contentBytes )
530
- if err := VerifyMetricsMatch (expectedMetrics , actualMetrics , "" ); err != nil {
537
+ if err := VerifyMetricsMatch (expectedMetrics , actualMetrics , ProcessStartTimeMetric ); err != nil {
531
538
t .Fatalf ("Metrics returned by end point do not match expectation: %v" , err )
532
539
}
533
540
}
541
+
542
+ func TestProcessStartTimeMetricExist (t * testing.T ) {
543
+ // Arrange
544
+ cmm := NewCSIMetricsManagerForSidecar (
545
+ "fake.csi.driver.io" /* driverName */ )
546
+ operationDuration , _ := time .ParseDuration ("20s" )
547
+
548
+ // Act
549
+ cmm .RecordMetrics (
550
+ "/csi.v1.Controller/ControllerGetCapabilities" , /* operationName */
551
+ nil , /* operationErr */
552
+ operationDuration /* operationDuration */ )
553
+
554
+ // Assert
555
+ metricsFamilies , err := cmm .GetRegistry ().Gather ()
556
+ if err != nil {
557
+ t .Fatalf ("Error fetching metrics: %v" , err )
558
+ }
559
+
560
+ // check process_start_time_seconds exist
561
+ for _ , metricsFamily := range metricsFamilies {
562
+ if metricsFamily .GetName () == ProcessStartTimeMetric {
563
+ return
564
+ }
565
+ }
566
+
567
+ t .Fatalf ("Metrics does not contain %v. Scraped content: %v" , ProcessStartTimeMetric , metricsFamilies )
568
+ }
0 commit comments