cmd/crowdsec-cli: remove global prometheusURL (#2542)

* cmd/crowdsec-cli: remove global prometheusURL
* PrometheusUrl now includes the path (/metrics)
This commit is contained in:
mmetc 2023-10-16 12:10:11 +02:00 committed by Marco Mariani
parent 734ba46e6a
commit a00bae6039
10 changed files with 56 additions and 50 deletions

View file

@ -235,13 +235,15 @@ func NewCollectionsUpgradeCmd() *cobra.Command {
func runCollectionsInspect(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
var err error
// XXX: set global
prometheusURL, err = flags.GetString("url")
url, err := flags.GetString("url")
if err != nil {
return err
}
if url != "" {
csConfig.Cscli.PrometheusUrl = url
}
for _, name := range args {
InspectItem(name, cwhub.COLLECTIONS)
}

View file

@ -29,8 +29,6 @@ var dbClient *database.Client
var OutputFormat string
var OutputColor string
var prometheusURL string
var mergedConfig string
func initConfig() {

View file

@ -284,7 +284,23 @@ var noUnit bool
func runMetrics(cmd *cobra.Command, args []string) error {
if err := csConfig.LoadPrometheus(); err != nil {
flags := cmd.Flags()
url, err := flags.GetString("url")
if err != nil {
return err
}
if url != "" {
csConfig.Cscli.PrometheusUrl = url
}
noUnit, err = flags.GetBool("no-unit")
if err != nil {
return err
}
if err = csConfig.LoadPrometheus(); err != nil {
return fmt.Errorf("failed to load prometheus config: %w", err)
}
@ -296,16 +312,7 @@ func runMetrics(cmd *cobra.Command, args []string) error {
return fmt.Errorf("prometheus is not enabled, can't show metrics")
}
if prometheusURL == "" {
prometheusURL = csConfig.Cscli.PrometheusUrl
}
if prometheusURL == "" {
return fmt.Errorf("no prometheus url, please specify in %s or via -u", *csConfig.FilePath)
}
err := FormatPrometheusMetrics(color.Output, prometheusURL+"/metrics", csConfig.Cscli.Output)
if err != nil {
if err = FormatPrometheusMetrics(color.Output, csConfig.Cscli.PrometheusUrl, csConfig.Cscli.Output); err != nil {
return err
}
return nil
@ -321,8 +328,10 @@ func NewMetricsCmd() *cobra.Command {
DisableAutoGenTag: true,
RunE: runMetrics,
}
cmdMetrics.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
cmdMetrics.PersistentFlags().BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units")
flags := cmdMetrics.PersistentFlags()
flags.StringP("url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
flags.Bool("no-unit", false, "Show the real number instead of formatted with units")
return cmdMetrics
}

View file

@ -223,13 +223,15 @@ func NewParsersUpgradeCmd() *cobra.Command {
func runParsersInspect(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
var err error
// XXX: set global
prometheusURL, err = flags.GetString("url")
url, err := flags.GetString("url")
if err != nil {
return err
}
if url != "" {
csConfig.Cscli.PrometheusUrl = url
}
InspectItem(args[0], cwhub.PARSERS)
return nil

View file

@ -223,13 +223,15 @@ func NewPostOverflowsUpgradeCmd() *cobra.Command {
func runPostOverflowsInspect(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
var err error
// XXX: set global
prometheusURL, err = flags.GetString("url")
url, err := flags.GetString("url")
if err != nil {
return err
}
if url != "" {
csConfig.Cscli.PrometheusUrl = url
}
InspectItem(args[0], cwhub.PARSERS_OVFLW)
return nil

View file

@ -223,13 +223,15 @@ func NewCmdScenariosUpgrade() *cobra.Command {
func runScenariosInspect(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
var err error
// XXX: set global
prometheusURL, err = flags.GetString("url")
url, err := flags.GetString("url")
if err != nil {
return err
}
if url != "" {
csConfig.Cscli.PrometheusUrl = url
}
InspectItem(args[0], cwhub.SCENARIOS)
return nil

View file

@ -69,13 +69,13 @@ func collectMetrics() ([]byte, []byte, error) {
}
humanMetrics := bytes.NewBuffer(nil)
err = FormatPrometheusMetrics(humanMetrics, csConfig.Cscli.PrometheusUrl+"/metrics", "human")
err = FormatPrometheusMetrics(humanMetrics, csConfig.Cscli.PrometheusUrl, "human")
if err != nil {
return nil, nil, fmt.Errorf("could not fetch promtheus metrics: %s", err)
}
req, err := http.NewRequest(http.MethodGet, csConfig.Cscli.PrometheusUrl+"/metrics", nil)
req, err := http.NewRequest(http.MethodGet, csConfig.Cscli.PrometheusUrl, nil)
if err != nil {
return nil, nil, fmt.Errorf("could not create requests to prometheus endpoint: %s", err)
}

View file

@ -210,15 +210,6 @@ func InspectItem(name string, objecitemType string) {
return
}
if prometheusURL == "" {
//This is technically wrong to do this, as the prometheus section contains a listen address, not an URL to query prometheus
//But for ease of use, we will use the listen address as the prometheus URL because it will be 127.0.0.1 in the default case
listenAddr := csConfig.Prometheus.ListenAddr
listenPort := csConfig.Prometheus.ListenPort
prometheusURL = fmt.Sprintf("http://%s:%d/metrics", listenAddr, listenPort)
log.Debugf("No prometheus URL provided using: %s", prometheusURL)
}
fmt.Printf("\nCurrent metrics : \n")
ShowMetrics(hubItem)
}
@ -256,18 +247,18 @@ func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *
func ShowMetrics(hubItem *cwhub.Item) {
switch hubItem.Type {
case cwhub.PARSERS:
metrics := GetParserMetric(prometheusURL, hubItem.Name)
metrics := GetParserMetric(hubItem.Name)
parserMetricsTable(color.Output, hubItem.Name, metrics)
case cwhub.SCENARIOS:
metrics := GetScenarioMetric(prometheusURL, hubItem.Name)
metrics := GetScenarioMetric(hubItem.Name)
scenarioMetricsTable(color.Output, hubItem.Name, metrics)
case cwhub.COLLECTIONS:
for _, item := range hubItem.Parsers {
metrics := GetParserMetric(prometheusURL, item)
metrics := GetParserMetric(item)
parserMetricsTable(color.Output, item, metrics)
}
for _, item := range hubItem.Scenarios {
metrics := GetScenarioMetric(prometheusURL, item)
metrics := GetScenarioMetric(item)
scenarioMetricsTable(color.Output, item, metrics)
}
for _, item := range hubItem.Collections {
@ -283,10 +274,10 @@ func ShowMetrics(hubItem *cwhub.Item) {
}
// GetParserMetric is a complete rip from prom2json
func GetParserMetric(url string, itemName string) map[string]map[string]int {
func GetParserMetric(itemName string) map[string]map[string]int {
stats := make(map[string]map[string]int)
result := GetPrometheusMetric(url)
result := GetPrometheusMetric()
for idx, fam := range result {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
@ -364,7 +355,7 @@ func GetParserMetric(url string, itemName string) map[string]map[string]int {
return stats
}
func GetScenarioMetric(url string, itemName string) map[string]int {
func GetScenarioMetric(itemName string) map[string]int {
stats := make(map[string]int)
stats["instantiation"] = 0
@ -373,7 +364,7 @@ func GetScenarioMetric(url string, itemName string) map[string]int {
stats["pour"] = 0
stats["underflow"] = 0
result := GetPrometheusMetric(url)
result := GetPrometheusMetric()
for idx, fam := range result {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
@ -419,7 +410,7 @@ func GetScenarioMetric(url string, itemName string) map[string]int {
return stats
}
func GetPrometheusMetric(url string) []*prom2json.Family {
func GetPrometheusMetric() []*prom2json.Family {
mfChan := make(chan *dto.MetricFamily, 1024)
// Start with the DefaultTransport for sane defaults.
@ -432,7 +423,7 @@ func GetPrometheusMetric(url string) []*prom2json.Family {
go func() {
defer trace.CatchPanic("crowdsec/GetPrometheusMetric")
err := prom2json.FetchMetricFamilies(url, mfChan, transport)
err := prom2json.FetchMetricFamilies(csConfig.Cscli.PrometheusUrl, mfChan, transport)
if err != nil {
log.Fatalf("failed to fetch prometheus metrics : %v", err)
}

View file

@ -12,7 +12,7 @@ type PrometheusCfg struct {
func (c *Config) LoadPrometheus() error {
if c.Cscli != nil && c.Cscli.PrometheusUrl == "" && c.Prometheus != nil {
if c.Prometheus.ListenAddr != "" && c.Prometheus.ListenPort != 0 {
c.Cscli.PrometheusUrl = fmt.Sprintf("http://%s:%d", c.Prometheus.ListenAddr, c.Prometheus.ListenPort)
c.Cscli.PrometheusUrl = fmt.Sprintf("http://%s:%d/metrics", c.Prometheus.ListenAddr, c.Prometheus.ListenPort)
}
}
return nil

View file

@ -26,7 +26,7 @@ func TestLoadPrometheus(t *testing.T) {
},
Cscli: &CscliCfg{},
},
expectedURL: "http://127.0.0.1:6060",
expectedURL: "http://127.0.0.1:6060/metrics",
},
}