mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Improve metrics pkg and fix linter warnings
This commit is contained in:
parent
b304e88e79
commit
50212d7596
6 changed files with 43 additions and 31 deletions
|
@ -2,11 +2,12 @@ package metrics
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
|
||||
"github.com/safing/portbase/config"
|
||||
)
|
||||
|
||||
// Configuration Keys
|
||||
// Configuration Keys.
|
||||
var (
|
||||
CfgOptionInstanceKey = "core/metrics/instance"
|
||||
instanceOption config.StringOption
|
||||
|
@ -16,13 +17,19 @@ var (
|
|||
pushOption config.StringOption
|
||||
cfgOptionPushOrder = 0
|
||||
|
||||
pushFlag string
|
||||
instanceFlag string
|
||||
pushFlag string
|
||||
instanceFlag string
|
||||
defaultInstance string
|
||||
)
|
||||
|
||||
func init() {
|
||||
hostname, err := os.Hostname()
|
||||
if err == nil {
|
||||
defaultInstance = hostname
|
||||
}
|
||||
|
||||
flag.StringVar(&pushFlag, "push-metrics", "", "set default URL to push prometheus metrics to")
|
||||
flag.StringVar(&instanceFlag, "metrics-instance", "", "set the default global instance label")
|
||||
flag.StringVar(&instanceFlag, "metrics-instance", defaultInstance, "set the default global instance label")
|
||||
}
|
||||
|
||||
func prepConfig() error {
|
||||
|
|
|
@ -7,16 +7,18 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
vm "github.com/VictoriaMetrics/metrics"
|
||||
|
||||
"github.com/safing/portbase/api"
|
||||
"github.com/safing/portbase/config"
|
||||
|
||||
vm "github.com/VictoriaMetrics/metrics"
|
||||
)
|
||||
|
||||
// PrometheusFormatRequirement is required format defined by prometheus for
|
||||
// metric and label names.
|
||||
const prometheusBaseFormt = "[a-zA-Z_][a-zA-Z0-9_]*"
|
||||
const PrometheusFormatRequirement = "^" + prometheusBaseFormt + "$"
|
||||
const (
|
||||
prometheusBaseFormt = "[a-zA-Z_][a-zA-Z0-9_]*"
|
||||
PrometheusFormatRequirement = "^" + prometheusBaseFormt + "$"
|
||||
)
|
||||
|
||||
var prometheusFormat = regexp.MustCompile(PrometheusFormatRequirement)
|
||||
|
||||
|
|
|
@ -5,64 +5,64 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/api"
|
||||
"github.com/safing/portbase/dataroot"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
|
||||
"github.com/safing/portbase/api"
|
||||
"github.com/safing/portbase/dataroot"
|
||||
"github.com/safing/portbase/log"
|
||||
)
|
||||
|
||||
const hostStatTTL = 1 * time.Second
|
||||
|
||||
func registeHostMetrics() (err error) {
|
||||
// Register load average metrics.
|
||||
_, err = NewGauge("host_load_avg_1", nil, getFloat64HostStat(LoadAvg1), &Options{Name: "Host Load Avg 1min", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/load/avg/1", nil, getFloat64HostStat(LoadAvg1), &Options{Name: "Host Load Avg 1min", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_load_avg_5", nil, getFloat64HostStat(LoadAvg5), &Options{Name: "Host Load Avg 5min", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/load/avg/5", nil, getFloat64HostStat(LoadAvg5), &Options{Name: "Host Load Avg 5min", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_load_avg_15", nil, getFloat64HostStat(LoadAvg15), &Options{Name: "Host Load Avg 15min", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/load/avg/15", nil, getFloat64HostStat(LoadAvg15), &Options{Name: "Host Load Avg 15min", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Register memory usage metrics.
|
||||
_, err = NewGauge("host_mem_total", nil, getUint64HostStat(MemTotal), &Options{Name: "Host Memory Total", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/mem/total", nil, getUint64HostStat(MemTotal), &Options{Name: "Host Memory Total", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_mem_used", nil, getUint64HostStat(MemUsed), &Options{Name: "Host Memory Used", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/mem/used", nil, getUint64HostStat(MemUsed), &Options{Name: "Host Memory Used", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_mem_available", nil, getUint64HostStat(MemAvailable), &Options{Name: "Host Memory Available", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/mem/available", nil, getUint64HostStat(MemAvailable), &Options{Name: "Host Memory Available", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_mem_used_percent", nil, getFloat64HostStat(MemUsedPercent), &Options{Name: "Host Memory Used in Percent", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/mem/used/percent", nil, getFloat64HostStat(MemUsedPercent), &Options{Name: "Host Memory Used in Percent", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Register disk usage metrics.
|
||||
_, err = NewGauge("host_disk_total", nil, getUint64HostStat(DiskTotal), &Options{Name: "Host Disk Total", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/disk/total", nil, getUint64HostStat(DiskTotal), &Options{Name: "Host Disk Total", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_disk_used", nil, getUint64HostStat(DiskUsed), &Options{Name: "Host Disk Used", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/disk/used", nil, getUint64HostStat(DiskUsed), &Options{Name: "Host Disk Used", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_disk_free", nil, getUint64HostStat(DiskFree), &Options{Name: "Host Disk Free", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/disk/free", nil, getUint64HostStat(DiskFree), &Options{Name: "Host Disk Free", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = NewGauge("host_disk_used_percent", nil, getFloat64HostStat(DiskUsedPercent), &Options{Name: "Host Disk Used in Percent", Permission: api.PermitUser})
|
||||
_, err = NewGauge("host/disk/used/percent", nil, getFloat64HostStat(DiskUsedPercent), &Options{Name: "Host Disk Used in Percent", Permission: api.PermitUser})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"io"
|
||||
|
||||
vm "github.com/VictoriaMetrics/metrics"
|
||||
|
||||
"github.com/safing/portbase/api"
|
||||
"github.com/safing/portbase/config"
|
||||
)
|
||||
|
|
|
@ -6,10 +6,11 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tevino/abool"
|
||||
|
||||
"github.com/safing/portbase/database"
|
||||
"github.com/safing/portbase/database/record"
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/tevino/abool"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -96,6 +97,7 @@ func storePersistentMetrics() {
|
|||
|
||||
// Create new storage.
|
||||
newStorage := &metricsStorage{
|
||||
// TODO: This timestamp should be taken from previous save, if possible.
|
||||
Start: time.Now(),
|
||||
Counters: make(map[string]uint64),
|
||||
}
|
||||
|
@ -134,18 +136,18 @@ func getMetricsStorage(key string) (*metricsStorage, error) {
|
|||
// unwrap
|
||||
if r.IsWrapped() {
|
||||
// only allocate a new struct, if we need it
|
||||
new := &metricsStorage{}
|
||||
err = record.Unwrap(r, new)
|
||||
newStorage := &metricsStorage{}
|
||||
err = record.Unwrap(r, newStorage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return new, nil
|
||||
return newStorage, nil
|
||||
}
|
||||
|
||||
// or adjust type
|
||||
new, ok := r.(*metricsStorage)
|
||||
newStorage, ok := r.(*metricsStorage)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("record not of type *metricsStorage, but %T", r)
|
||||
}
|
||||
return new, nil
|
||||
return newStorage, nil
|
||||
}
|
||||
|
|
|
@ -106,9 +106,9 @@ func Start() error {
|
|||
|
||||
// complete startup
|
||||
if moduleMgmtEnabled.IsSet() {
|
||||
log.Info("modules: initiated subsystems manager")
|
||||
log.Info("modules: started enabled modules")
|
||||
} else {
|
||||
log.Infof("modules: started %d modules", len(modules))
|
||||
log.Infof("modules: started all %d modules", len(modules))
|
||||
}
|
||||
|
||||
go taskQueueHandler()
|
||||
|
|
Loading…
Add table
Reference in a new issue