Skip to content

Commit e41b1f7

Browse files
authored
Merge pull request #1297 from k8s-infra-cherrypick-robot/cherry-pick-1287-to-release-1.8
[release-1.8] emit metrics even for success scenarios
2 parents 99ea483 + 623859d commit e41b1f7

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

pkg/gce-pd-csi-driver/controller.go

+8-24
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
196196
var err error
197197
diskTypeForMetric := ""
198198
defer func() {
199-
if err != nil {
200-
gceCS.Metrics.RecordOperationErrorMetrics("CreateVolume", err, diskTypeForMetric)
201-
}
199+
gceCS.Metrics.RecordOperationErrorMetrics("CreateVolume", err, diskTypeForMetric)
202200
}()
203201
// Validate arguments
204202
volumeCapabilities := req.GetVolumeCapabilities()
@@ -430,9 +428,7 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
430428
var err error
431429
diskTypeForMetric := ""
432430
defer func() {
433-
if err != nil {
434-
gceCS.Metrics.RecordOperationErrorMetrics("DeleteVolume", err, diskTypeForMetric)
435-
}
431+
gceCS.Metrics.RecordOperationErrorMetrics("DeleteVolume", err, diskTypeForMetric)
436432
}()
437433
// Validate arguments
438434
volumeID := req.GetVolumeId()
@@ -476,9 +472,7 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
476472
var err error
477473
diskTypeForMetric := ""
478474
defer func() {
479-
if err != nil {
480-
gceCS.Metrics.RecordOperationErrorMetrics("ControllerPublishVolume", err, diskTypeForMetric)
481-
}
475+
gceCS.Metrics.RecordOperationErrorMetrics("ControllerPublishVolume", err, diskTypeForMetric)
482476
}()
483477
// Only valid requests will be accepted
484478
_, _, err = gceCS.validateControllerPublishVolumeRequest(ctx, req)
@@ -638,9 +632,7 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
638632
var err error
639633
diskTypeForMetric := ""
640634
defer func() {
641-
if err != nil {
642-
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
643-
}
635+
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
644636
}()
645637
_, _, err = gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
646638
if err != nil {
@@ -749,9 +741,7 @@ func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context
749741
var err error
750742
diskTypeForMetric := ""
751743
defer func() {
752-
if err != nil {
753-
gceCS.Metrics.RecordOperationErrorMetrics("ValidateVolumeCapabilities", err, diskTypeForMetric)
754-
}
744+
gceCS.Metrics.RecordOperationErrorMetrics("ValidateVolumeCapabilities", err, diskTypeForMetric)
755745
}()
756746
if req.GetVolumeCapabilities() == nil || len(req.GetVolumeCapabilities()) == 0 {
757747
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities must be provided")
@@ -908,9 +898,7 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
908898
var err error
909899
diskTypeForMetric := ""
910900
defer func() {
911-
if err != nil {
912-
gceCS.Metrics.RecordOperationErrorMetrics("CreateSnapshot", err, diskTypeForMetric)
913-
}
901+
gceCS.Metrics.RecordOperationErrorMetrics("CreateSnapshot", err, diskTypeForMetric)
914902
}()
915903
// Validate arguments
916904
volumeID := req.GetSourceVolumeId()
@@ -1145,9 +1133,7 @@ func (gceCS *GCEControllerServer) DeleteSnapshot(ctx context.Context, req *csi.D
11451133
var err error
11461134
diskTypeForMetric := ""
11471135
defer func() {
1148-
if err != nil {
1149-
gceCS.Metrics.RecordOperationErrorMetrics("DeleteSnapshot", err, diskTypeForMetric)
1150-
}
1136+
gceCS.Metrics.RecordOperationErrorMetrics("DeleteSnapshot", err, diskTypeForMetric)
11511137
}()
11521138
// Validate arguments
11531139
snapshotID := req.GetSnapshotId()
@@ -1236,9 +1222,7 @@ func (gceCS *GCEControllerServer) ControllerExpandVolume(ctx context.Context, re
12361222
var err error
12371223
diskTypeForMetric := ""
12381224
defer func() {
1239-
if err != nil {
1240-
gceCS.Metrics.RecordOperationErrorMetrics("ControllerExpandVolume", err, diskTypeForMetric)
1241-
}
1225+
gceCS.Metrics.RecordOperationErrorMetrics("ControllerExpandVolume", err, diskTypeForMetric)
12421226
}()
12431227
volumeID := req.GetVolumeId()
12441228
if len(volumeID) == 0 {

pkg/metrics/metrics.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/http"
2222
"os"
2323

24+
"google.golang.org/grpc/codes"
2425
"k8s.io/component-base/metrics"
2526
"k8s.io/klog/v2"
2627
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
@@ -90,7 +91,11 @@ func (mm *MetricsManager) RecordOperationErrorMetrics(
9091
operationName string,
9192
operationErr error,
9293
diskType string) {
93-
pdcsiOperationErrorsMetric.WithLabelValues(pdcsiDriverName, "/csi.v1.Controller/"+operationName, common.CodeForError(operationErr).String(), diskType).Inc()
94+
err := codes.OK.String()
95+
if operationErr != nil {
96+
err = common.CodeForError(operationErr).String()
97+
}
98+
pdcsiOperationErrorsMetric.WithLabelValues(pdcsiDriverName, "/csi.v1.Controller/"+operationName, err, diskType).Inc()
9499
}
95100

96101
func (mm *MetricsManager) EmitGKEComponentVersion() error {

0 commit comments

Comments
 (0)