Skip to content

Commit

Permalink
feat: Enhance metric name matching to match on prefix (#327)
Browse files Browse the repository at this point in the history
* feat: Enhance metric name matching to match on prefix

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.

Signed-off-by: Leonard Goodell <[email protected]>
  • Loading branch information
Lenny Goodell authored Apr 25, 2022
1 parent 575cc54 commit a90d46a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
19 changes: 14 additions & 5 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"fmt"
"strings"

"github.com/edgexfoundry/go-mod-core-contracts/v2/common"

Expand Down Expand Up @@ -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
}
51 changes: 51 additions & 0 deletions config/types_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit a90d46a

Please sign in to comment.