diff --git a/CHANGELOG.md b/CHANGELOG.md index f5671cd2..5d819fc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ * [ENHANCEMENT] Add node_softirqs_total metric #2221 * [ENHANCEMENT] Add device filter flags to arp collector #2254 +* [ENHANCEMENT] Add rapl zone name label option #2401 +* [BUGFIX] Sanitize rapl zone names #2299 ## 1.3.1 / 2021-12-01 diff --git a/collector/rapl_linux.go b/collector/rapl_linux.go index 4fc566cc..f6f7dfc7 100644 --- a/collector/rapl_linux.go +++ b/collector/rapl_linux.go @@ -26,17 +26,26 @@ import ( "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs/sysfs" + "gopkg.in/alecthomas/kingpin.v2" ) +const raplCollectorSubsystem = "rapl" + type raplCollector struct { fs sysfs.FS logger log.Logger + + joulesMetricDesc *prometheus.Desc } func init() { - registerCollector("rapl", defaultEnabled, NewRaplCollector) + registerCollector(raplCollectorSubsystem, defaultEnabled, NewRaplCollector) } +var ( + raplZoneLabel = kingpin.Flag("collector.rapl.enable-zone-label", "Enables service unit metric unit_start_time_seconds").Bool() +) + // NewRaplCollector returns a new Collector exposing RAPL metrics. func NewRaplCollector(logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) @@ -45,9 +54,16 @@ func NewRaplCollector(logger log.Logger) (Collector, error) { return nil, err } + joulesMetricDesc := prometheus.NewDesc( + prometheus.BuildFQName(namespace, raplCollectorSubsystem, "joules_total"), + "Current RAPL value in joules", + []string{"index", "path", "rapl_zone"}, nil, + ) + collector := raplCollector{ - fs: fs, - logger: logger, + fs: fs, + logger: logger, + joulesMetricDesc: joulesMetricDesc, } return &collector, nil } @@ -69,7 +85,7 @@ func (c *raplCollector) Update(ch chan<- prometheus.Metric) error { } for _, rz := range zones { - newMicrojoules, err := rz.GetEnergyMicrojoules() + microJoules, err := rz.GetEnergyMicrojoules() if err != nil { if errors.Is(err, os.ErrPermission) { level.Debug(c.logger).Log("msg", "Can't access energy_uj file", "zone", rz, "err", err) @@ -77,21 +93,48 @@ func (c *raplCollector) Update(ch chan<- prometheus.Metric) error { } return err } - index := strconv.Itoa(rz.Index) - - descriptor := prometheus.NewDesc( - prometheus.BuildFQName(namespace, "rapl", SanitizeMetricName(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, - ) + + joules := float64(microJoules) / 1000000.0 + + if *raplZoneLabel { + ch <- c.joulesMetricWithZoneLabel(rz, joules) + } else { + ch <- c.joulesMetric(rz, joules) + } } return nil } + +func (c *raplCollector) joulesMetric(z sysfs.RaplZone, v float64) prometheus.Metric { + index := strconv.Itoa(z.Index) + descriptor := prometheus.NewDesc( + prometheus.BuildFQName( + namespace, + raplCollectorSubsystem, + fmt.Sprintf("%s_joules_total", SanitizeMetricName(z.Name)), + ), + fmt.Sprintf("Current RAPL %s value in joules", z.Name), + []string{"index", "path"}, nil, + ) + + return prometheus.MustNewConstMetric( + descriptor, + prometheus.CounterValue, + v, + index, + z.Path, + ) +} + +func (c *raplCollector) joulesMetricWithZoneLabel(z sysfs.RaplZone, v float64) prometheus.Metric { + index := strconv.Itoa(z.Index) + + return prometheus.MustNewConstMetric( + c.joulesMetricDesc, + prometheus.CounterValue, + v, + index, + z.Path, + z.Name, + ) +}