Skip to content

Commit

Permalink
Validating metric name before writing to cache
Browse files Browse the repository at this point in the history
Signed-off-by: Madalina Lazar <madalina.lazar@intel.com>
  • Loading branch information
madalazar committed Aug 9, 2023
1 parent 3144ce7 commit b4daf15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion telemetry-aware-scheduling/pkg/cache/autoupdating.go
Expand Up @@ -20,7 +20,10 @@ const (
l2 = 2
)

var errNull = errors.New("")
var (
errNull = errors.New("")
errInvalidMetricName = errors.New("invalid metric name")
)

// AutoUpdatingCache holds a map of metrics of interest with their associated NodeMetricsInfo object.
type AutoUpdatingCache struct {
Expand Down Expand Up @@ -119,6 +122,12 @@ func (n *AutoUpdatingCache) WritePolicy(namespace string, policyName string, pol
// It also increments a counter showing how many strategies are using the metric -
// protecting it from deletion until there are no more associated strategies.
func (n *AutoUpdatingCache) WriteMetric(metricName string, data metrics.NodeMetricsInfo) error {
if len(metricName) == 0 {
klog.V(l2).ErrorS(errInvalidMetricName, "Failed to write metric with metric name: "+metricName, "component", "controller")

return errInvalidMetricName
}

payload := nilPayloadCheck(data)
n.add(fmt.Sprintf(metricPath, metricName), payload)

Expand Down
1 change: 1 addition & 0 deletions telemetry-aware-scheduling/pkg/cache/autoupdating_test.go
Expand Up @@ -201,6 +201,7 @@ func TestNodeMetricsCache_WriteMetric(t *testing.T) {
{"false name queried", MockEmptySelfUpdatingCache(), "memory_free", args{"memoryFREE"}, true},
{"number queried", MockEmptySelfUpdatingCache(), "1", args{"memoryFREE"}, true},
{"add existing metric", MockEmptySelfUpdatingCache(), "dummyMetric1", args{"dummyMetric1"}, false},
{"empty metric name", MockEmptySelfUpdatingCache(), "", args{""}, true},
}
for _, tt := range tests {
tt := tt
Expand Down

0 comments on commit b4daf15

Please sign in to comment.