Skip to content

Commit 2f9f140

Browse files
Sneha-atsunnylovestiramisu
authored andcommitted
emit metrics even for success scenarios
1 parent b78ebed commit 2f9f140

File tree

5 files changed

+14
-107
lines changed

5 files changed

+14
-107
lines changed

pkg/common/utils.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,20 +297,6 @@ func ConvertMiBStringToInt64(str string) (int64, error) {
297297
quantity := resource.MustParse(str)
298298
return volumehelpers.RoundUpToMiB(quantity)
299299
}
300-
<<<<<<< HEAD
301-
=======
302-
303-
// ParseMachineType returns an extracted machineType from a URL, or empty if not found.
304-
// machineTypeUrl: Full or partial URL of the machine type resource, in the format:
305-
//
306-
// zones/zone/machineTypes/machine-type
307-
func ParseMachineType(machineTypeUrl string) (string, error) {
308-
machineType := machineTypeRegex.FindStringSubmatch(machineTypeUrl)
309-
if machineType == nil {
310-
return "", fmt.Errorf("failed to parse machineTypeUrl. Expected suffix: zones/{zone}/machineTypes/{machine-type}. Got: %s", machineTypeUrl)
311-
}
312-
return machineType[1], nil
313-
}
314300

315301
// CodeForError returns a pointer to the grpc error code that maps to the http
316302
// error code for the passed in user googleapi error or context error. Returns
@@ -391,4 +377,3 @@ func LoggedError(msg string, err error) error {
391377
klog.Errorf(msg+"%v", err.Error())
392378
return status.Errorf(*CodeForError(err), msg+"%v", err.Error())
393379
}
394-
>>>>>>> Adding new metric pdcsi_operation_errors to fetch error count

pkg/common/utils_test.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -853,62 +853,6 @@ func TestConvertMiBStringToInt64(t *testing.T) {
853853
})
854854
}
855855
}
856-
<<<<<<< HEAD
857-
=======
858-
859-
func TestParseMachineType(t *testing.T) {
860-
tests := []struct {
861-
desc string
862-
inputMachineTypeUrl string
863-
expectedMachineType string
864-
expectError bool
865-
}{
866-
{
867-
desc: "full URL machine type",
868-
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4",
869-
expectedMachineType: "c3-highcpu-4",
870-
},
871-
{
872-
desc: "partial URL machine type",
873-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/n2-standard-4",
874-
expectedMachineType: "n2-standard-4",
875-
},
876-
{
877-
desc: "custom partial URL machine type",
878-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/e2-custom-2-4096",
879-
expectedMachineType: "e2-custom-2-4096",
880-
},
881-
{
882-
desc: "incorrect URL",
883-
inputMachineTypeUrl: "https://www.googleapis.com/compute/v1/projects/psch-gke-dev/zones/us-central1-c",
884-
expectError: true,
885-
},
886-
{
887-
desc: "incorrect partial URL",
888-
inputMachineTypeUrl: "zones/us-central1-c/machineTypes/",
889-
expectError: true,
890-
},
891-
{
892-
desc: "missing zone",
893-
inputMachineTypeUrl: "zones//machineTypes/n2-standard-4",
894-
expectError: true,
895-
},
896-
}
897-
for _, tc := range tests {
898-
t.Run(tc.desc, func(t *testing.T) {
899-
actualMachineFamily, err := ParseMachineType(tc.inputMachineTypeUrl)
900-
if err != nil && !tc.expectError {
901-
t.Errorf("Got error %v parsing machine type %s; expect no error", err, tc.inputMachineTypeUrl)
902-
}
903-
if err == nil && tc.expectError {
904-
t.Errorf("Got no error parsing machine type %s; expect an error", tc.inputMachineTypeUrl)
905-
}
906-
if err == nil && actualMachineFamily != tc.expectedMachineType {
907-
t.Errorf("Got %s parsing machine type; expect %s", actualMachineFamily, tc.expectedMachineType)
908-
}
909-
})
910-
}
911-
}
912856

913857
func TestCodeForError(t *testing.T) {
914858
internalErrorCode := codes.Internal
@@ -1015,4 +959,3 @@ func TestIsContextError(t *testing.T) {
1015959
}
1016960
}
1017961
}
1018-
>>>>>>> Adding new metric pdcsi_operation_errors to fetch error count

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
204204
var err error
205205
diskTypeForMetric := ""
206206
defer func() {
207-
if err != nil {
208-
gceCS.Metrics.RecordOperationErrorMetrics("CreateVolume", err, diskTypeForMetric)
209-
}
207+
gceCS.Metrics.RecordOperationErrorMetrics("CreateVolume", err, diskTypeForMetric)
210208
}()
211209
// Validate arguments
212210
volumeCapabilities := req.GetVolumeCapabilities()
@@ -439,9 +437,7 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
439437
var err error
440438
diskTypeForMetric := ""
441439
defer func() {
442-
if err != nil {
443-
gceCS.Metrics.RecordOperationErrorMetrics("DeleteVolume", err, diskTypeForMetric)
444-
}
440+
gceCS.Metrics.RecordOperationErrorMetrics("DeleteVolume", err, diskTypeForMetric)
445441
}()
446442
// Validate arguments
447443
volumeID := req.GetVolumeId()
@@ -485,9 +481,7 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
485481
var err error
486482
diskTypeForMetric := ""
487483
defer func() {
488-
if err != nil {
489-
gceCS.Metrics.RecordOperationErrorMetrics("ControllerPublishVolume", err, diskTypeForMetric)
490-
}
484+
gceCS.Metrics.RecordOperationErrorMetrics("ControllerPublishVolume", err, diskTypeForMetric)
491485
}()
492486
// Only valid requests will be accepted
493487
_, _, err = gceCS.validateControllerPublishVolumeRequest(ctx, req)
@@ -647,9 +641,7 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
647641
var err error
648642
diskTypeForMetric := ""
649643
defer func() {
650-
if err != nil {
651-
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
652-
}
644+
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
653645
}()
654646
_, _, err = gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
655647
if err != nil {
@@ -758,9 +750,7 @@ func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context
758750
var err error
759751
diskTypeForMetric := ""
760752
defer func() {
761-
if err != nil {
762-
gceCS.Metrics.RecordOperationErrorMetrics("ValidateVolumeCapabilities", err, diskTypeForMetric)
763-
}
753+
gceCS.Metrics.RecordOperationErrorMetrics("ValidateVolumeCapabilities", err, diskTypeForMetric)
764754
}()
765755
if req.GetVolumeCapabilities() == nil || len(req.GetVolumeCapabilities()) == 0 {
766756
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities must be provided")
@@ -917,9 +907,7 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
917907
var err error
918908
diskTypeForMetric := ""
919909
defer func() {
920-
if err != nil {
921-
gceCS.Metrics.RecordOperationErrorMetrics("CreateSnapshot", err, diskTypeForMetric)
922-
}
910+
gceCS.Metrics.RecordOperationErrorMetrics("CreateSnapshot", err, diskTypeForMetric)
923911
}()
924912
// Validate arguments
925913
volumeID := req.GetSourceVolumeId()
@@ -1154,9 +1142,7 @@ func (gceCS *GCEControllerServer) DeleteSnapshot(ctx context.Context, req *csi.D
11541142
var err error
11551143
diskTypeForMetric := ""
11561144
defer func() {
1157-
if err != nil {
1158-
gceCS.Metrics.RecordOperationErrorMetrics("DeleteSnapshot", err, diskTypeForMetric)
1159-
}
1145+
gceCS.Metrics.RecordOperationErrorMetrics("DeleteSnapshot", err, diskTypeForMetric)
11601146
}()
11611147
// Validate arguments
11621148
snapshotID := req.GetSnapshotId()
@@ -1245,9 +1231,7 @@ func (gceCS *GCEControllerServer) ControllerExpandVolume(ctx context.Context, re
12451231
var err error
12461232
diskTypeForMetric := ""
12471233
defer func() {
1248-
if err != nil {
1249-
gceCS.Metrics.RecordOperationErrorMetrics("ControllerExpandVolume", err, diskTypeForMetric)
1250-
}
1234+
gceCS.Metrics.RecordOperationErrorMetrics("ControllerExpandVolume", err, diskTypeForMetric)
12511235
}()
12521236
volumeID := req.GetVolumeId()
12531237
if len(volumeID) == 0 {

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@ limitations under the License.
1818
package gceGCEDriver
1919

2020
import (
21-
<<<<<<< HEAD
22-
"errors"
23-
"net/http"
2421
"testing"
2522

2623
csi "github.com/container-storage-interface/spec/lib/go/csi"
27-
"google.golang.org/api/googleapi"
28-
"google.golang.org/grpc/codes"
29-
=======
30-
"testing"
31-
32-
csi "github.com/container-storage-interface/spec/lib/go/csi"
33-
>>>>>>> Adding new metric pdcsi_operation_errors to fetch error count
3424
)
3525

3626
var (

pkg/metrics/metrics.go

Lines changed: 6 additions & 1 deletion
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)