cmd: expose an uptime metric for each command

This commit is contained in:
Vincent Bernat
2025-09-19 15:37:11 +02:00
parent b10914dcd5
commit 57185f67dd

View File

@@ -8,6 +8,7 @@ import (
"os" "os"
"runtime" "runtime"
"sync/atomic" "sync/atomic"
"time"
"akvorado/common/helpers" "akvorado/common/helpers"
"akvorado/common/reporter" "akvorado/common/reporter"
@@ -22,6 +23,7 @@ import (
var ( var (
debug bool debug bool
missedLogs atomic.Uint64 missedLogs atomic.Uint64
startTime time.Time
) )
// RootCmd is the root for all commands // RootCmd is the root for all commands
@@ -59,9 +61,16 @@ func moreMetrics(r *reporter.Reporter) {
Help: "Akvorado build information", Help: "Akvorado build information",
}, []string{"version", "compiler"}). }, []string{"version", "compiler"}).
WithLabelValues(helpers.AkvoradoVersion, runtime.Version()).Set(1) WithLabelValues(helpers.AkvoradoVersion, runtime.Version()).Set(1)
r.GaugeFunc(reporter.GaugeOpts{
Name: "uptime_seconds",
Help: "number of seconds the application is running",
}, func() float64 {
return time.Since(startTime).Seconds()
})
} }
func init() { func init() {
startTime = time.Now()
RootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, RootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false,
"Enable debug logs") "Enable debug logs")
} }