Skip to content

Commit

Permalink
Add rapl zone label option (prometheus#2401)
Browse files Browse the repository at this point in the history
Add an optional flag to set the RAPL zone as a label, instead of as part
of the metric name.

Fixes: prometheus#2299

Signed-off-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
SuperQ authored and oblitorum committed Apr 9, 2024
1 parent d3381f9 commit 35ff9a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
81 changes: 62 additions & 19 deletions collector/rapl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -69,29 +85,56 @@ 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)
return ErrNoData
}
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,
)
}

0 comments on commit 35ff9a1

Please sign in to comment.