mirror of
https://codeberg.org/scip/io-exporter.git
synced 2025-12-17 12:31:03 +01:00
refactored for easier mainenance, add Alloc and Metrics classes
This commit is contained in:
75
cmd/metrics.go
Normal file
75
cmd/metrics.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// custom labels
|
||||
type Label struct {
|
||||
Name, Value string
|
||||
}
|
||||
|
||||
// simple prometheus wrapper
|
||||
type Metrics struct {
|
||||
run *prometheus.GaugeVec
|
||||
latency *prometheus.GaugeVec
|
||||
registry *prometheus.Registry
|
||||
values []string
|
||||
}
|
||||
|
||||
func NewMetrics(conf *Config) *Metrics {
|
||||
labels := []string{"file", "maxwait"}
|
||||
LabelLen := 2
|
||||
|
||||
for _, label := range conf.Labels {
|
||||
labels = append(labels, label.Name)
|
||||
}
|
||||
|
||||
metrics := &Metrics{
|
||||
run: prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "io_exporter_io_operation",
|
||||
Help: "whether io is working on the pvc, 1=ok, 0=fail",
|
||||
},
|
||||
labels,
|
||||
),
|
||||
latency: prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "io_exporter_io_latency",
|
||||
Help: "how long does the operation take in seconds",
|
||||
},
|
||||
labels,
|
||||
),
|
||||
|
||||
// use fixed size slice to avoid repeated allocs
|
||||
values: make([]string, LabelLen+len(conf.Labels)),
|
||||
|
||||
registry: prometheus.NewRegistry(),
|
||||
}
|
||||
|
||||
metrics.registry.MustRegister(metrics.run, metrics.latency)
|
||||
|
||||
// static labels
|
||||
metrics.values[0] = conf.File
|
||||
metrics.values[1] = fmt.Sprintf("%d", conf.Timeout)
|
||||
|
||||
// custom labels via -l label=value
|
||||
for idx, label := range conf.Labels {
|
||||
metrics.values[idx+LabelLen] = label.Value
|
||||
}
|
||||
|
||||
return metrics
|
||||
}
|
||||
|
||||
func (metrics *Metrics) Set(result bool, elapsed float64) {
|
||||
var res float64
|
||||
|
||||
if result {
|
||||
res = 1
|
||||
}
|
||||
|
||||
metrics.run.WithLabelValues(metrics.values...).Set(res)
|
||||
metrics.latency.WithLabelValues(metrics.values...).Set(elapsed)
|
||||
}
|
||||
Reference in New Issue
Block a user