github.com/safing/portbase@v0.19.5/metrics/config.go (about) 1 package metrics 2 3 import ( 4 "flag" 5 "os" 6 "strings" 7 8 "github.com/safing/portbase/config" 9 ) 10 11 // Configuration Keys. 12 var ( 13 CfgOptionInstanceKey = "core/metrics/instance" 14 instanceOption config.StringOption 15 cfgOptionInstanceOrder = 0 16 17 CfgOptionCommentKey = "core/metrics/comment" 18 commentOption config.StringOption 19 cfgOptionCommentOrder = 0 20 21 CfgOptionPushKey = "core/metrics/push" 22 pushOption config.StringOption 23 cfgOptionPushOrder = 0 24 25 instanceFlag string 26 defaultInstance string 27 commentFlag string 28 pushFlag string 29 ) 30 31 func init() { 32 hostname, err := os.Hostname() 33 if err == nil { 34 hostname = strings.ReplaceAll(hostname, "-", "") 35 if prometheusFormat.MatchString(hostname) { 36 defaultInstance = hostname 37 } 38 } 39 40 flag.StringVar(&instanceFlag, "metrics-instance", defaultInstance, "set the default metrics instance label for all metrics") 41 flag.StringVar(&commentFlag, "metrics-comment", "", "set the default metrics comment label") 42 flag.StringVar(&pushFlag, "push-metrics", "", "set default URL to push prometheus metrics to") 43 } 44 45 func prepConfig() error { 46 err := config.Register(&config.Option{ 47 Name: "Metrics Instance Name", 48 Key: CfgOptionInstanceKey, 49 Description: "Define the prometheus instance label for all exported metrics. Please note that changing the metrics instance name will reset persisted metrics.", 50 Sensitive: true, 51 OptType: config.OptTypeString, 52 ExpertiseLevel: config.ExpertiseLevelExpert, 53 ReleaseLevel: config.ReleaseLevelStable, 54 DefaultValue: instanceFlag, 55 RequiresRestart: true, 56 Annotations: config.Annotations{ 57 config.DisplayOrderAnnotation: cfgOptionInstanceOrder, 58 config.CategoryAnnotation: "Metrics", 59 }, 60 ValidationRegex: "^(" + prometheusBaseFormt + ")?$", 61 }) 62 if err != nil { 63 return err 64 } 65 instanceOption = config.Concurrent.GetAsString(CfgOptionInstanceKey, instanceFlag) 66 67 err = config.Register(&config.Option{ 68 Name: "Metrics Comment Label", 69 Key: CfgOptionCommentKey, 70 Description: "Define a metrics comment label, which is added to the info metric.", 71 Sensitive: true, 72 OptType: config.OptTypeString, 73 ExpertiseLevel: config.ExpertiseLevelExpert, 74 ReleaseLevel: config.ReleaseLevelStable, 75 DefaultValue: commentFlag, 76 RequiresRestart: true, 77 Annotations: config.Annotations{ 78 config.DisplayOrderAnnotation: cfgOptionCommentOrder, 79 config.CategoryAnnotation: "Metrics", 80 }, 81 }) 82 if err != nil { 83 return err 84 } 85 commentOption = config.Concurrent.GetAsString(CfgOptionCommentKey, commentFlag) 86 87 err = config.Register(&config.Option{ 88 Name: "Push Prometheus Metrics", 89 Key: CfgOptionPushKey, 90 Description: "Push metrics to this URL in the prometheus format.", 91 Sensitive: true, 92 OptType: config.OptTypeString, 93 ExpertiseLevel: config.ExpertiseLevelExpert, 94 ReleaseLevel: config.ReleaseLevelStable, 95 DefaultValue: pushFlag, 96 RequiresRestart: true, 97 Annotations: config.Annotations{ 98 config.DisplayOrderAnnotation: cfgOptionPushOrder, 99 config.CategoryAnnotation: "Metrics", 100 }, 101 }) 102 if err != nil { 103 return err 104 } 105 pushOption = config.Concurrent.GetAsString(CfgOptionPushKey, pushFlag) 106 107 return nil 108 }