safing-portmaster/base/metrics/config.go
Daniel Hååvi 80664d1a27
Restructure modules ()
* Move portbase into monorepo

* Add new simple module mgr

* [WIP] Switch to new simple module mgr

* Add StateMgr and more worker variants

* [WIP] Switch more modules

* [WIP] Switch more modules

* [WIP] swtich more modules

* [WIP] switch all SPN modules

* [WIP] switch all service modules

* [WIP] Convert all workers to the new module system

* [WIP] add new task system to module manager

* [WIP] Add second take for scheduling workers

* [WIP] Add FIXME for bugs in new scheduler

* [WIP] Add minor improvements to scheduler

* [WIP] Add new worker scheduler

* [WIP] Fix more bug related to new module system

* [WIP] Fix start handing of the new module system

* [WIP] Improve startup process

* [WIP] Fix minor issues

* [WIP] Fix missing subsystem in settings

* [WIP] Initialize managers in constructor

* [WIP] Move module event initialization to constrictors

* [WIP] Fix setting for enabling and disabling the SPN module

* [WIP] Move API registeration into module construction

* [WIP] Update states mgr for all modules

* [WIP] Add CmdLine operation support

* Add state helper methods to module group and instance

* Add notification and module status handling to status package

* Fix starting issues

* Remove pilot widget and update security lock to new status data

* Remove debug logs

* Improve http server shutdown

* Add workaround for cleanly shutting down firewall+netquery

* Improve logging

* Add syncing states with notifications for new module system

* Improve starting, stopping, shutdown; resolve FIXMEs/TODOs

* [WIP] Fix most unit tests

* Review new module system and fix minor issues

* Push shutdown and restart events again via API

* Set sleep mode via interface

* Update example/template module

* [WIP] Fix spn/cabin unit test

* Remove deprecated UI elements

* Make log output more similar for the logging transition phase

* Switch spn hub and observer cmds to new module system

* Fix log sources

* Make worker mgr less error prone

* Fix tests and minor issues

* Fix observation hub

* Improve shutdown and restart handling

* Split up big connection.go source file

* Move varint and dsd packages to structures repo

* Improve expansion test

* Fix linter warnings

* Fix interception module on windows

* Fix linter errors

---------

Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
2024-08-09 18:15:48 +03:00

108 lines
3.2 KiB
Go

package metrics
import (
"flag"
"os"
"strings"
"github.com/safing/portmaster/base/config"
)
// Configuration Keys.
var (
CfgOptionInstanceKey = "core/metrics/instance"
instanceOption config.StringOption
cfgOptionInstanceOrder = 0
CfgOptionCommentKey = "core/metrics/comment"
commentOption config.StringOption
cfgOptionCommentOrder = 0
CfgOptionPushKey = "core/metrics/push"
pushOption config.StringOption
cfgOptionPushOrder = 0
instanceFlag string
defaultInstance string
commentFlag string
pushFlag string
)
func init() {
hostname, err := os.Hostname()
if err == nil {
hostname = strings.ReplaceAll(hostname, "-", "")
if prometheusFormat.MatchString(hostname) {
defaultInstance = hostname
}
}
flag.StringVar(&instanceFlag, "metrics-instance", defaultInstance, "set the default metrics instance label for all metrics")
flag.StringVar(&commentFlag, "metrics-comment", "", "set the default metrics comment label")
flag.StringVar(&pushFlag, "push-metrics", "", "set default URL to push prometheus metrics to")
}
func prepConfig() error {
err := config.Register(&config.Option{
Name: "Metrics Instance Name",
Key: CfgOptionInstanceKey,
Description: "Define the prometheus instance label for all exported metrics. Please note that changing the metrics instance name will reset persisted metrics.",
Sensitive: true,
OptType: config.OptTypeString,
ExpertiseLevel: config.ExpertiseLevelExpert,
ReleaseLevel: config.ReleaseLevelStable,
DefaultValue: instanceFlag,
RequiresRestart: true,
Annotations: config.Annotations{
config.DisplayOrderAnnotation: cfgOptionInstanceOrder,
config.CategoryAnnotation: "Metrics",
},
ValidationRegex: "^(" + prometheusBaseFormt + ")?$",
})
if err != nil {
return err
}
instanceOption = config.Concurrent.GetAsString(CfgOptionInstanceKey, instanceFlag)
err = config.Register(&config.Option{
Name: "Metrics Comment Label",
Key: CfgOptionCommentKey,
Description: "Define a metrics comment label, which is added to the info metric.",
Sensitive: true,
OptType: config.OptTypeString,
ExpertiseLevel: config.ExpertiseLevelExpert,
ReleaseLevel: config.ReleaseLevelStable,
DefaultValue: commentFlag,
RequiresRestart: true,
Annotations: config.Annotations{
config.DisplayOrderAnnotation: cfgOptionCommentOrder,
config.CategoryAnnotation: "Metrics",
},
})
if err != nil {
return err
}
commentOption = config.Concurrent.GetAsString(CfgOptionCommentKey, commentFlag)
err = config.Register(&config.Option{
Name: "Push Prometheus Metrics",
Key: CfgOptionPushKey,
Description: "Push metrics to this URL in the prometheus format.",
Sensitive: true,
OptType: config.OptTypeString,
ExpertiseLevel: config.ExpertiseLevelExpert,
ReleaseLevel: config.ReleaseLevelStable,
DefaultValue: pushFlag,
RequiresRestart: true,
Annotations: config.Annotations{
config.DisplayOrderAnnotation: cfgOptionPushOrder,
config.CategoryAnnotation: "Metrics",
},
})
if err != nil {
return err
}
pushOption = config.Concurrent.GetAsString(CfgOptionPushKey, pushFlag)
return nil
}