mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +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 (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/safing/portbase/config"
|
"github.com/safing/portbase/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Configuration Keys
|
// Configuration Keys.
|
||||||
var (
|
var (
|
||||||
CfgOptionInstanceKey = "core/metrics/instance"
|
CfgOptionInstanceKey = "core/metrics/instance"
|
||||||
instanceOption config.StringOption
|
instanceOption config.StringOption
|
||||||
|
@ -16,13 +17,19 @@ var (
|
||||||
pushOption config.StringOption
|
pushOption config.StringOption
|
||||||
cfgOptionPushOrder = 0
|
cfgOptionPushOrder = 0
|
||||||
|
|
||||||
pushFlag string
|
pushFlag string
|
||||||
instanceFlag string
|
instanceFlag string
|
||||||
|
defaultInstance string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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(&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 {
|
func prepConfig() error {
|
||||||
|
|
|
@ -7,16 +7,18 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
vm "github.com/VictoriaMetrics/metrics"
|
||||||
|
|
||||||
"github.com/safing/portbase/api"
|
"github.com/safing/portbase/api"
|
||||||
"github.com/safing/portbase/config"
|
"github.com/safing/portbase/config"
|
||||||
|
|
||||||
vm "github.com/VictoriaMetrics/metrics"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PrometheusFormatRequirement is required format defined by prometheus for
|
// PrometheusFormatRequirement is required format defined by prometheus for
|
||||||
// metric and label names.
|
// metric and label names.
|
||||||
const prometheusBaseFormt = "[a-zA-Z_][a-zA-Z0-9_]*"
|
const (
|
||||||
const PrometheusFormatRequirement = "^" + prometheusBaseFormt + "$"
|
prometheusBaseFormt = "[a-zA-Z_][a-zA-Z0-9_]*"
|
||||||
|
PrometheusFormatRequirement = "^" + prometheusBaseFormt + "$"
|
||||||
|
)
|
||||||
|
|
||||||
var prometheusFormat = regexp.MustCompile(PrometheusFormatRequirement)
|
var prometheusFormat = regexp.MustCompile(PrometheusFormatRequirement)
|
||||||
|
|
||||||
|
|
|
@ -5,64 +5,64 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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/disk"
|
||||||
"github.com/shirou/gopsutil/load"
|
"github.com/shirou/gopsutil/load"
|
||||||
"github.com/shirou/gopsutil/mem"
|
"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
|
const hostStatTTL = 1 * time.Second
|
||||||
|
|
||||||
func registeHostMetrics() (err error) {
|
func registeHostMetrics() (err error) {
|
||||||
// Register load average metrics.
|
// 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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register memory usage metrics.
|
// 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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register disk usage metrics.
|
// 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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
vm "github.com/VictoriaMetrics/metrics"
|
vm "github.com/VictoriaMetrics/metrics"
|
||||||
|
|
||||||
"github.com/safing/portbase/api"
|
"github.com/safing/portbase/api"
|
||||||
"github.com/safing/portbase/config"
|
"github.com/safing/portbase/config"
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,10 +6,11 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/tevino/abool"
|
||||||
|
|
||||||
"github.com/safing/portbase/database"
|
"github.com/safing/portbase/database"
|
||||||
"github.com/safing/portbase/database/record"
|
"github.com/safing/portbase/database/record"
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
"github.com/tevino/abool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -96,6 +97,7 @@ func storePersistentMetrics() {
|
||||||
|
|
||||||
// Create new storage.
|
// Create new storage.
|
||||||
newStorage := &metricsStorage{
|
newStorage := &metricsStorage{
|
||||||
|
// TODO: This timestamp should be taken from previous save, if possible.
|
||||||
Start: time.Now(),
|
Start: time.Now(),
|
||||||
Counters: make(map[string]uint64),
|
Counters: make(map[string]uint64),
|
||||||
}
|
}
|
||||||
|
@ -134,18 +136,18 @@ func getMetricsStorage(key string) (*metricsStorage, error) {
|
||||||
// unwrap
|
// unwrap
|
||||||
if r.IsWrapped() {
|
if r.IsWrapped() {
|
||||||
// only allocate a new struct, if we need it
|
// only allocate a new struct, if we need it
|
||||||
new := &metricsStorage{}
|
newStorage := &metricsStorage{}
|
||||||
err = record.Unwrap(r, new)
|
err = record.Unwrap(r, newStorage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return new, nil
|
return newStorage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// or adjust type
|
// or adjust type
|
||||||
new, ok := r.(*metricsStorage)
|
newStorage, ok := r.(*metricsStorage)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("record not of type *metricsStorage, but %T", r)
|
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
|
// complete startup
|
||||||
if moduleMgmtEnabled.IsSet() {
|
if moduleMgmtEnabled.IsSet() {
|
||||||
log.Info("modules: initiated subsystems manager")
|
log.Info("modules: started enabled modules")
|
||||||
} else {
|
} else {
|
||||||
log.Infof("modules: started %d modules", len(modules))
|
log.Infof("modules: started all %d modules", len(modules))
|
||||||
}
|
}
|
||||||
|
|
||||||
go taskQueueHandler()
|
go taskQueueHandler()
|
||||||
|
|
Loading…
Add table
Reference in a new issue