Merge pull request #118086 from aramase/aramase/c/refactor_grpc_status_code_err
update err status code check in transformation metrics
This commit is contained in:
		| @@ -17,9 +17,11 @@ limitations under the License. | |||||||
| package value | package value | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"errors" | ||||||
| 	"sync" | 	"sync" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
|  | 	"google.golang.org/grpc/codes" | ||||||
| 	"google.golang.org/grpc/status" | 	"google.golang.org/grpc/status" | ||||||
|  |  | ||||||
| 	"k8s.io/component-base/metrics" | 	"k8s.io/component-base/metrics" | ||||||
| @@ -112,7 +114,7 @@ func RegisterMetrics() { | |||||||
| // RecordTransformation records latencies and count of TransformFromStorage and TransformToStorage operations. | // RecordTransformation records latencies and count of TransformFromStorage and TransformToStorage operations. | ||||||
| // Note that transformation_failures_total metric is deprecated, use transformation_operations_total instead. | // Note that transformation_failures_total metric is deprecated, use transformation_operations_total instead. | ||||||
| func RecordTransformation(transformationType, transformerPrefix string, elapsed time.Duration, err error) { | func RecordTransformation(transformationType, transformerPrefix string, elapsed time.Duration, err error) { | ||||||
| 	transformerOperationsTotal.WithLabelValues(transformationType, transformerPrefix, status.Code(err).String()).Inc() | 	transformerOperationsTotal.WithLabelValues(transformationType, transformerPrefix, getErrorCode(err)).Inc() | ||||||
|  |  | ||||||
| 	if err == nil { | 	if err == nil { | ||||||
| 		transformerLatencies.WithLabelValues(transformationType, transformerPrefix).Observe(elapsed.Seconds()) | 		transformerLatencies.WithLabelValues(transformationType, transformerPrefix).Observe(elapsed.Seconds()) | ||||||
| @@ -138,3 +140,23 @@ func RecordDataKeyGeneration(start time.Time, err error) { | |||||||
| func sinceInSeconds(start time.Time) float64 { | func sinceInSeconds(start time.Time) float64 { | ||||||
| 	return time.Since(start).Seconds() | 	return time.Since(start).Seconds() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type gRPCError interface { | ||||||
|  | 	GRPCStatus() *status.Status | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func getErrorCode(err error) string { | ||||||
|  | 	if err == nil { | ||||||
|  | 		return codes.OK.String() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// handle errors wrapped with fmt.Errorf and similar | ||||||
|  | 	var s gRPCError | ||||||
|  | 	if errors.As(err, &s) { | ||||||
|  | 		return s.GRPCStatus().Code().String() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// This is not gRPC error. The operation must have failed before gRPC | ||||||
|  | 	// method was called, otherwise we would get gRPC error. | ||||||
|  | 	return "unknown-non-grpc" | ||||||
|  | } | ||||||
|   | |||||||
| @@ -19,6 +19,7 @@ package value | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"fmt" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"time" | ||||||
| @@ -34,10 +35,13 @@ func TestTotals(t *testing.T) { | |||||||
| 	nonStatusErr := errors.New("test error") | 	nonStatusErr := errors.New("test error") | ||||||
| 	failedPreconditionErr := status.Error(codes.FailedPrecondition, "test error") | 	failedPreconditionErr := status.Error(codes.FailedPrecondition, "test error") | ||||||
| 	internalErr := status.Error(codes.Internal, "test error") | 	internalErr := status.Error(codes.Internal, "test error") | ||||||
|  | 	wrappedErr := fmt.Errorf("some low level thing failed: %w", status.Error(codes.NotFound, "some error")) | ||||||
|  |  | ||||||
| 	nonStatusErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: nonStatusErr}} | 	nonStatusErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: nonStatusErr}} | ||||||
| 	failedPreconditionErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: failedPreconditionErr}} | 	failedPreconditionErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: failedPreconditionErr}} | ||||||
| 	internalErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: internalErr}} | 	internalErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: internalErr}} | ||||||
| 	okTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{from: []byte("value")}} | 	okTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{from: []byte("value")}} | ||||||
|  | 	wrappedErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: wrappedErr}} | ||||||
|  |  | ||||||
| 	testCases := []struct { | 	testCases := []struct { | ||||||
| 		desc    string | 		desc    string | ||||||
| @@ -54,8 +58,8 @@ func TestTotals(t *testing.T) { | |||||||
| 			want: ` | 			want: ` | ||||||
| 				# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | 				# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | ||||||
| 				# TYPE apiserver_storage_transformation_operations_total counter | 				# TYPE apiserver_storage_transformation_operations_total counter | ||||||
| 				apiserver_storage_transformation_operations_total{status="Unknown",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | 				apiserver_storage_transformation_operations_total{status="unknown-non-grpc",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | ||||||
| 				apiserver_storage_transformation_operations_total{status="Unknown",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | 				apiserver_storage_transformation_operations_total{status="unknown-non-grpc",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | ||||||
| 				`, | 				`, | ||||||
| 		}, | 		}, | ||||||
| 		{ | 		{ | ||||||
| @@ -97,6 +101,19 @@ func TestTotals(t *testing.T) { | |||||||
| 				apiserver_storage_transformation_operations_total{status="Internal",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | 				apiserver_storage_transformation_operations_total{status="Internal",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | ||||||
| 				`, | 				`, | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			desc:   "wrapped not found error", | ||||||
|  | 			prefix: NewPrefixTransformers(nil, wrappedErrTransformer), | ||||||
|  | 			metrics: []string{ | ||||||
|  | 				"apiserver_storage_transformation_operations_total", | ||||||
|  | 			}, | ||||||
|  | 			want: ` | ||||||
|  | 			# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | ||||||
|  | 			# TYPE apiserver_storage_transformation_operations_total counter | ||||||
|  | 			apiserver_storage_transformation_operations_total{status="NotFound",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | ||||||
|  | 			apiserver_storage_transformation_operations_total{status="NotFound",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 | ||||||
|  | 			`, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	RegisterMetrics() | 	RegisterMetrics() | ||||||
|   | |||||||
| @@ -159,7 +159,7 @@ func TestPrefixFromMetrics(t *testing.T) { | |||||||
| 			want: ` | 			want: ` | ||||||
| 	# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | 	# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | ||||||
|   # TYPE apiserver_storage_transformation_operations_total counter |   # TYPE apiserver_storage_transformation_operations_total counter | ||||||
|   apiserver_storage_transformation_operations_total{status="Unknown",transformation_type="from_storage",transformer_prefix="other:"} 1 |   apiserver_storage_transformation_operations_total{status="unknown-non-grpc",transformation_type="from_storage",transformer_prefix="other:"} 1 | ||||||
|   `, |   `, | ||||||
| 			err: nil, | 			err: nil, | ||||||
| 		}, | 		}, | ||||||
| @@ -173,7 +173,7 @@ func TestPrefixFromMetrics(t *testing.T) { | |||||||
| 			want: ` | 			want: ` | ||||||
| 	# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | 	# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | ||||||
|   # TYPE apiserver_storage_transformation_operations_total counter |   # TYPE apiserver_storage_transformation_operations_total counter | ||||||
|   apiserver_storage_transformation_operations_total{status="Unknown",transformation_type="from_storage",transformer_prefix="unknown"} 1 |   apiserver_storage_transformation_operations_total{status="unknown-non-grpc",transformation_type="from_storage",transformer_prefix="unknown"} 1 | ||||||
|   `, |   `, | ||||||
| 			err: nil, | 			err: nil, | ||||||
| 		}, | 		}, | ||||||
| @@ -231,7 +231,7 @@ func TestPrefixToMetrics(t *testing.T) { | |||||||
| 			want: ` | 			want: ` | ||||||
| 	# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | 	# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. | ||||||
|   # TYPE apiserver_storage_transformation_operations_total counter |   # TYPE apiserver_storage_transformation_operations_total counter | ||||||
|   apiserver_storage_transformation_operations_total{status="Unknown",transformation_type="to_storage",transformer_prefix="other:"} 1 |   apiserver_storage_transformation_operations_total{status="unknown-non-grpc",transformation_type="to_storage",transformer_prefix="other:"} 1 | ||||||
|   `, |   `, | ||||||
| 			err: nil, | 			err: nil, | ||||||
| 		}, | 		}, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot