Skip to content

Commit

Permalink
Add group_label and instance_label to all metrics
Browse files Browse the repository at this point in the history
except for promhttp_* and go_*
  • Loading branch information
jramosrivas committed Jun 7, 2021
1 parent cc95f30 commit ad130dc
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 105 deletions.
47 changes: 23 additions & 24 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ const namespace = "node"

var constLabels = make(prometheus.Labels)

var (
scrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "scrape", "collector_duration_seconds"),
"node_exporter: Duration of a collector scrape.",
[]string{"collector"},
constLabels,
)
scrapeSuccessDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "scrape", "collector_success"),
"node_exporter: Whether a collector succeeded.",
[]string{"collector"},
constLabels,
)
)

const (
defaultEnabled = true
defaultDisabled = false
Expand Down Expand Up @@ -77,8 +62,10 @@ func registerCollector(collector string, isDefaultEnabled bool, factory func(log

// NodeCollector implements the prometheus.Collector interface.
type NodeCollector struct {
Collectors map[string]Collector
logger log.Logger
Collectors map[string]Collector
logger log.Logger
scrapeDurationDesc *prometheus.Desc
scrapeSuccessDesc *prometheus.Desc
}

// DisableDefaultCollectors sets the collector state to false for all collectors which
Expand Down Expand Up @@ -132,13 +119,25 @@ func NewNodeCollector(logger log.Logger, nameLabel, groupLabel string, filters .
}
}
}
return &NodeCollector{Collectors: collectors, logger: logger}, nil
scrapeDurationDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "scrape", "collector_duration_seconds"),
"node_exporter: Duration of a collector scrape.",
[]string{"collector"},
constLabels,
)
scrapeSuccessDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "scrape", "collector_success"),
"node_exporter: Whether a collector succeeded.",
[]string{"collector"},
constLabels,
)
return &NodeCollector{Collectors: collectors, logger: logger, scrapeDurationDesc: scrapeDurationDesc, scrapeSuccessDesc: scrapeSuccessDesc}, nil
}

// Describe implements the prometheus.Collector interface.
func (n NodeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- scrapeDurationDesc
ch <- scrapeSuccessDesc
ch <- n.scrapeDurationDesc
ch <- n.scrapeSuccessDesc
}

// Collect implements the prometheus.Collector interface.
Expand All @@ -147,14 +146,14 @@ func (n NodeCollector) Collect(ch chan<- prometheus.Metric) {
wg.Add(len(n.Collectors))
for name, c := range n.Collectors {
go func(name string, c Collector) {
execute(name, c, ch, n.logger)
execute(name, c, ch, n.logger, n)
wg.Done()
}(name, c)
}
wg.Wait()
}

func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.Logger) {
func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.Logger, n NodeCollector) {
begin := time.Now()
err := c.Update(ch)
duration := time.Since(begin)
Expand All @@ -171,8 +170,8 @@ func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.L
level.Debug(logger).Log("msg", "collector succeeded", "name", name, "duration_seconds", duration.Seconds())
success = 1
}
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, name)
ch <- prometheus.MustNewConstMetric(n.scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)
ch <- prometheus.MustNewConstMetric(n.scrapeSuccessDesc, prometheus.GaugeValue, success, name)
}

// Collector is the interface a collector has to implement.
Expand Down
12 changes: 0 additions & 12 deletions collector/cpu_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@

package collector

import (
"github.com/prometheus/client_golang/prometheus"
)

const (
cpuCollectorSubsystem = "cpu"
)

var (
nodeCPUSecondsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
)
5 changes: 5 additions & 0 deletions collector/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func init() {

// NewCPUCollector returns a new Collector exposing CPU stats.
func NewCPUCollector(logger log.Logger) (Collector, error) {
nodeCPUSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
return &statCollector{
cpu: nodeCPUSecondsDesc,
logger: logger,
Expand Down
5 changes: 5 additions & 0 deletions collector/cpu_dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func init() {

// NewStatCollector returns a new Collector exposing CPU stats.
func NewStatCollector(logger log.Logger) (Collector, error) {
nodeCPUSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
return &statCollector{
cpu: nodeCPUSecondsDesc,
logger: logger,
Expand Down
5 changes: 5 additions & 0 deletions collector/cpu_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func init() {

// NewStatCollector returns a new Collector exposing CPU stats.
func NewStatCollector(logger log.Logger) (Collector, error) {
nodeCPUSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
return &statCollector{
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
temp: typedDesc{prometheus.NewDesc(
Expand Down
5 changes: 5 additions & 0 deletions collector/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func NewCPUCollector(logger log.Logger) (Collector, error) {
if err != nil {
return nil, fmt.Errorf("failed to open procfs: %w", err)
}
nodeCPUSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
c := &cpuCollector{
fs: fs,
cpu: nodeCPUSecondsDesc,
Expand Down
5 changes: 5 additions & 0 deletions collector/cpu_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func init() {
}

func NewCPUCollector(logger log.Logger) (Collector, error) {
nodeCPUSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
return &cpuCollector{
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
logger: logger,
Expand Down
5 changes: 5 additions & 0 deletions collector/cpu_openbsd_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func init() {
}

func NewCPUCollector(logger log.Logger) (Collector, error) {
nodeCPUSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
return &cpuCollector{
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
logger: logger,
Expand Down
5 changes: 5 additions & 0 deletions collector/cpu_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func init() {
}

func NewCpuCollector(logger log.Logger) (Collector, error) {
nodeCPUSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "seconds_total"),
"Seconds the CPUs spent in each mode.",
[]string{"cpu", "mode"}, constLabels,
)
return &cpuCollector{
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
logger: logger,
Expand Down
48 changes: 0 additions & 48 deletions collector/diskstats_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,10 @@

package collector

import (
"github.com/prometheus/client_golang/prometheus"
)

const (
diskSubsystem = "disk"
)

var (
diskLabelNames = []string{"device"}

readsCompletedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"),
"The total number of reads completed successfully.",
diskLabelNames, constLabels,
)

readBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "read_bytes_total"),
"The total number of bytes read successfully.",
diskLabelNames, constLabels,
)

writesCompletedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "writes_completed_total"),
"The total number of writes completed successfully.",
diskLabelNames, constLabels,
)

writtenBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "written_bytes_total"),
"The total number of bytes written successfully.",
diskLabelNames, constLabels,
)

ioTimeSecondsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "io_time_seconds_total"),
"Total seconds spent doing I/Os.",
diskLabelNames, constLabels,
)

readTimeSecondsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "read_time_seconds_total"),
"The total number of seconds spent by all reads.",
diskLabelNames,
constLabels,
)

writeTimeSecondsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "write_time_seconds_total"),
"This is the total number of seconds spent by all writes.",
diskLabelNames,
constLabels,
)
)
41 changes: 40 additions & 1 deletion collector/diskstats_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,47 @@ func init() {

// NewDiskstatsCollector returns a new Collector exposing disk device stats.
func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
var diskLabelNames = []string{"device"}
readsCompletedDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"),
"The total number of reads completed successfully.",
diskLabelNames, constLabels,
)

readBytesDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "read_bytes_total"),
"The total number of bytes read successfully.",
diskLabelNames, constLabels,
)

writesCompletedDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "writes_completed_total"),
"The total number of writes completed successfully.",
diskLabelNames, constLabels,
)

writtenBytesDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "written_bytes_total"),
"The total number of bytes written successfully.",
diskLabelNames, constLabels,
)

ioTimeSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "io_time_seconds_total"),
"Total seconds spent doing I/Os.",
diskLabelNames, constLabels,
)

readTimeSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "read_time_seconds_total"),
"The total number of seconds spent by all reads.",
diskLabelNames, constLabels,
)

writeTimeSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "write_time_seconds_total"),
"This is the total number of seconds spent by all writes.",
diskLabelNames, constLabels,
)
return &diskstatsCollector{
descs: []typedDescFunc{
{
Expand Down
42 changes: 41 additions & 1 deletion collector/diskstats_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,50 @@ func init() {
// NewDiskstatsCollector returns a new Collector exposing disk device stats.
// Docs from https://www.kernel.org/doc/Documentation/iostats.txt
func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
var diskLabelNames = []string{"device"}
readsCompletedDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"),
"The total number of reads completed successfully.",
diskLabelNames, constLabels,
)

readBytesDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "read_bytes_total"),
"The total number of bytes read successfully.",
diskLabelNames, constLabels,
)

writesCompletedDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "writes_completed_total"),
"The total number of writes completed successfully.",
diskLabelNames, constLabels,
)

writtenBytesDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "written_bytes_total"),
"The total number of bytes written successfully.",
diskLabelNames, constLabels,
)

ioTimeSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "io_time_seconds_total"),
"Total seconds spent doing I/Os.",
diskLabelNames, constLabels,
)

readTimeSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "read_time_seconds_total"),
"The total number of seconds spent by all reads.",
diskLabelNames, constLabels,
)

writeTimeSecondsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "write_time_seconds_total"),
"This is the total number of seconds spent by all writes.",
diskLabelNames, constLabels,
)
return &diskstatsCollector{
ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices),

descs: []typedFactorDesc{
{
desc: readsCompletedDesc, valueType: prometheus.CounterValue,
Expand Down
30 changes: 30 additions & 0 deletions collector/diskstats_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ func init() {

// NewDiskstatsCollector returns a new Collector exposing disk device stats.
func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
readsCompletedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"),
"The total number of reads completed successfully.",
diskLabelNames, constLabels,
)

readBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "read_bytes_total"),
"The total number of bytes read successfully.",
diskLabelNames, constLabels,
)

writesCompletedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "writes_completed_total"),
"The total number of writes completed successfully.",
diskLabelNames, constLabels,
)

writtenBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "written_bytes_total"),
"The total number of bytes written successfully.",
diskLabelNames, constLabels,
)

ioTimeSecondsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "io_time_seconds_total"),
"Total seconds spent doing I/Os.",
diskLabelNames, constLabels,
)

return &diskstatsCollector{
rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue},
rbytes: typedDesc{readBytesDesc, prometheus.CounterValue},
Expand Down
Loading

0 comments on commit ad130dc

Please sign in to comment.