add flag to report internal metrics on resource usage

This commit is contained in:
2025-10-21 10:16:08 +02:00
parent e9a4c46044
commit b47d696a4d
2 changed files with 27 additions and 7 deletions

View File

@@ -22,6 +22,7 @@ Options:
-t --timeout <int> When should the operation timeout in seconds -t --timeout <int> When should the operation timeout in seconds
-s --sleeptime <int> Time to sleep between checks (default: 5s) -s --sleeptime <int> Time to sleep between checks (default: 5s)
-l --label <label=value> Add label to exported metric -l --label <label=value> Add label to exported metric
-i --internals Also add labels about resource usage
-h --help Show help -h --help Show help
-v --version Show program version` -v --version Show program version`
) )
@@ -30,6 +31,7 @@ Options:
type Config struct { type Config struct {
Showversion bool `koanf:"version"` // -v Showversion bool `koanf:"version"` // -v
Showhelp bool `koanf:"help"` // -h Showhelp bool `koanf:"help"` // -h
Internals bool `koanf:"internals"` // -i
Label []string `koanf:"label"` // -v Label []string `koanf:"label"` // -v
Timeout int `koanf:"timeout"` // -t Timeout int `koanf:"timeout"` // -t
Port int `koanf:"port"` // -p Port int `koanf:"port"` // -p
@@ -54,6 +56,7 @@ func InitConfig(output io.Writer) (*Config, error) {
// parse commandline flags // parse commandline flags
flagset.BoolP("version", "v", false, "show program version") flagset.BoolP("version", "v", false, "show program version")
flagset.BoolP("help", "h", false, "show help") flagset.BoolP("help", "h", false, "show help")
flagset.BoolP("internals", "i", false, "add internal metrics")
flagset.StringArrayP("label", "l", nil, "additional labels") flagset.StringArrayP("label", "l", nil, "additional labels")
flagset.IntP("timeout", "t", 1, "timeout for file operation in seconds") flagset.IntP("timeout", "t", 1, "timeout for file operation in seconds")
flagset.IntP("port", "p", 9187, "prometheus metrics port to listen to") flagset.IntP("port", "p", 9187, "prometheus metrics port to listen to")

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
) )
// custom labels // custom labels
@@ -49,7 +50,23 @@ func NewMetrics(conf *Config) *Metrics {
registry: prometheus.NewRegistry(), registry: prometheus.NewRegistry(),
} }
if conf.Internals {
metrics.registry.MustRegister(
metrics.run,
metrics.latency,
// we might need to take care of the exporter in terms of
// resources, so also report those internals
collectors.NewGoCollector(
collectors.WithGoCollectorMemStatsMetricsDisabled(),
),
collectors.NewProcessCollector(
collectors.ProcessCollectorOpts{},
),
)
} else {
metrics.registry.MustRegister(metrics.run, metrics.latency) metrics.registry.MustRegister(metrics.run, metrics.latency)
}
// static labels // static labels
metrics.values[0] = conf.File metrics.values[0] = conf.File