Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitize rapl zone name #2301

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions collector/rapl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,29 @@ func (c *raplCollector) Update(ch chan<- prometheus.Metric) error {
}
return err
}
index := strconv.Itoa(rz.Index)

descriptor := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "rapl", rz.Name+"_joules_total"),
"Current RAPL "+rz.Name+" value in joules",
[]string{"index", "path"}, nil,
)

ch <- prometheus.MustNewConstMetric(
descriptor,
prometheus.CounterValue,
float64(newMicrojoules)/1000000.0,
index,
rz.Path,
)
index := strconv.Itoa(rz.Index)
ch <- getRzMetric(rz.Name, rz.Path, index, newMicrojoules)
}
return nil
}

// getRzMetric sanitize the raplZoneName and returns new metrics
func getRzMetric(raplZoneName, raplZonePath, index string, newMicrojoules uint64) prometheus.Metric {
// sanitizing rapl zone name for invalid metric name
raplZoneName = SanitizeMetricName(raplZoneName)

descriptor := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "rapl", raplZoneName+"_joules_total"),
"Current RAPL "+raplZoneName+" value in joules",
[]string{"index", "path"}, nil,
)

return prometheus.MustNewConstMetric(
descriptor,
prometheus.CounterValue,
float64(newMicrojoules)/1000000.0,
index,
raplZonePath,
)
}
49 changes: 49 additions & 0 deletions collector/rapl_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2015 The Prometheus Authors
// 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 collector

import (
"testing"
)

func TestInvalidRaplName(t *testing.T) {
testCases := []struct {
desc string
name string
}{
{
desc: "invalid metrics name with numeric",
name: "package9",
},
{
desc: "invalid metrics name with dash",
name: "package-",
},
{
desc: "invalid metrics name",
name: "package-0-die-0",
},
}

// these are irrelevant in this test so accepting default value
var raplZonePath, index string
var newMicrojoules uint64

for i := range testCases {
t.Run(testCases[i].desc, func(t *testing.T) {
// getRzMetric panics if any error occurs
getRzMetric(testCases[i].name, raplZonePath, index, newMicrojoules)
})
}
}