From b47d696a4df2cba40c7daa4e0e8410f8d8c76f93 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 21 Oct 2025 10:16:08 +0200 Subject: [PATCH] add flag to report internal metrics on resource usage --- cmd/config.go | 15 +++++++++------ cmd/metrics.go | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 10ea9e4..46f2bac 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -22,18 +22,20 @@ Options: -t --timeout When should the operation timeout in seconds -s --sleeptime Time to sleep between checks (default: 5s) -l --label Add label to exported metric +-i --internals Also add labels about resource usage -h --help Show help -v --version Show program version` ) // config via commandline flags type Config struct { - Showversion bool `koanf:"version"` // -v - Showhelp bool `koanf:"help"` // -h - Label []string `koanf:"label"` // -v - Timeout int `koanf:"timeout"` // -t - Port int `koanf:"port"` // -p - Sleeptime int `koanf:"sleep"` // -s + Showversion bool `koanf:"version"` // -v + Showhelp bool `koanf:"help"` // -h + Internals bool `koanf:"internals"` // -i + Label []string `koanf:"label"` // -v + Timeout int `koanf:"timeout"` // -t + Port int `koanf:"port"` // -p + Sleeptime int `koanf:"sleep"` // -s File string Labels []Label @@ -54,6 +56,7 @@ func InitConfig(output io.Writer) (*Config, error) { // parse commandline flags flagset.BoolP("version", "v", false, "show program version") flagset.BoolP("help", "h", false, "show help") + flagset.BoolP("internals", "i", false, "add internal metrics") flagset.StringArrayP("label", "l", nil, "additional labels") flagset.IntP("timeout", "t", 1, "timeout for file operation in seconds") flagset.IntP("port", "p", 9187, "prometheus metrics port to listen to") diff --git a/cmd/metrics.go b/cmd/metrics.go index 48e15d3..565c115 100644 --- a/cmd/metrics.go +++ b/cmd/metrics.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" ) // custom labels @@ -49,7 +50,23 @@ func NewMetrics(conf *Config) *Metrics { registry: prometheus.NewRegistry(), } - metrics.registry.MustRegister(metrics.run, metrics.latency) + 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) + } // static labels metrics.values[0] = conf.File