diff --git a/config/types.go b/config/types.go index aa5d23c8..d60ea9b8 100644 --- a/config/types.go +++ b/config/types.go @@ -18,6 +18,7 @@ package config import ( "fmt" + "strings" "github.com/edgexfoundry/go-mod-core-contracts/v2/common" @@ -240,11 +241,19 @@ type TelemetryInfo struct { } // MetricEnabled returns whether the named metric is enabled -func (t *TelemetryInfo) MetricEnabled(name string) bool { - enabled, exists := t.Metrics[name] - if !exists { - return false +func (t *TelemetryInfo) MetricEnabled(metricName string) bool { + for configMetricName, enabled := range t.Metrics { + // Match on config metric name as prefix of passed in metric name (service's metric item name) + // This allows for a class of Metrics to be enabled with one configured metric name. + // App SDK uses this for PipelineMetrics by appending the pipeline ID to the name + // of the metric(s) it is collecting for multiple function pipelines. + if !strings.HasPrefix(metricName, configMetricName) { + continue + } + + return enabled } - return enabled + // Service's metric name did not match any config Metric name. + return false } diff --git a/config/types_test.go b/config/types_test.go new file mode 100644 index 00000000..cccb8930 --- /dev/null +++ b/config/types_test.go @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright 2022 Intel Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + *******************************************************************************/ + +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTelemetryInfo_MetricEnabled(t *testing.T) { + target := TelemetryInfo{} + + manyMetrics := map[string]bool{ + "OtherMetric": false, + "YourMetric": false, + "MyMetricSpecial": false, + "MyMetric": true, + } + + tests := []struct { + Name string + Metrics map[string]bool + ServiceMetricName string + Expected bool + }{ + {"Simple Match", manyMetrics, "MyMetric", true}, + {"Has Prefix Match", manyMetrics, "MyMetric-1234", true}, + {"No Match", manyMetrics, "1234-MyMetric", false}, + } + + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + target.Metrics = test.Metrics + actual := target.MetricEnabled(test.ServiceMetricName) + assert.Equal(t, test.Expected, actual) + }) + } +}