From 6df8e95da02d253cac4042d121f05d622f36b1e8 Mon Sep 17 00:00:00 2001 From: divolgin Date: Fri, 6 Oct 2023 10:41:09 -0700 Subject: [PATCH] Don't panic on nil values --- pkg/handlers/custom_metrics.go | 4 ++++ pkg/handlers/custom_metrics_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/pkg/handlers/custom_metrics.go b/pkg/handlers/custom_metrics.go index 0d7cee12..20a58789 100644 --- a/pkg/handlers/custom_metrics.go +++ b/pkg/handlers/custom_metrics.go @@ -56,6 +56,10 @@ func validateCustomMetricsData(data ApplicationMetricsData) error { for key, val := range data { valType := reflect.TypeOf(val) + if valType == nil { + return errors.Errorf("%s value is nil, only scalar values are allowed", key) + } + switch valType.Kind() { case reflect.Slice: return errors.Errorf("%s value is an array, only scalar values are allowed", key) diff --git a/pkg/handlers/custom_metrics_test.go b/pkg/handlers/custom_metrics_test.go index 56e99834..6928a857 100644 --- a/pkg/handlers/custom_metrics_test.go +++ b/pkg/handlers/custom_metrics_test.go @@ -43,6 +43,14 @@ func Test_validateCustomMetricsData(t *testing.T) { }, wantErr: true, }, + { + name: "nil value", + data: ApplicationMetricsData{ + "key1": nil, + "key2": 4, + }, + wantErr: true, + }, } for _, test := range tests {