Skip to content

Commit

Permalink
command "cscli metrics show bouncers" (#3126)
Browse files Browse the repository at this point in the history
* cscli metrics show bouncers

* db metrics: increase payload size

* func tests
  • Loading branch information
mmetc authored Jul 15, 2024
1 parent 0672053 commit f130ce6
Show file tree
Hide file tree
Showing 27 changed files with 892 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package metrics
package climetrics

import (
"encoding/json"
"fmt"

"github.com/fatih/color"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"

"github.com/crowdsecurity/go-cs-lib/maptools"

Expand All @@ -32,31 +34,45 @@ func (cli *cliMetrics) list() error {
})
}

switch cli.cfg().Cscli.Output {
outputFormat := cli.cfg().Cscli.Output

switch outputFormat {
case "human":
t := cstable.New(color.Output, cli.cfg().Cscli.Color)
t.SetRowLines(true)
t.SetHeaders("Type", "Title", "Description")
out := color.Output
t := cstable.New(out, cli.cfg().Cscli.Color).Writer
t.AppendHeader(table.Row{"Type", "Title", "Description"})
t.SetColumnConfigs([]table.ColumnConfig{
{
Name: "Type",
AlignHeader: text.AlignCenter,
},
{
Name: "Title",
AlignHeader: text.AlignCenter,
},
{
Name: "Description",
AlignHeader: text.AlignCenter,
WidthMax: 60,
WidthMaxEnforcer: text.WrapSoft,
},
})
t.Style().Options.SeparateRows = true

for _, metric := range allMetrics {
t.AddRow(metric.Type, metric.Title, metric.Description)
t.AppendRow(table.Row{metric.Type, metric.Title, metric.Description})
}

t.Render()
fmt.Fprintln(out, t.Render())
case "json":
x, err := json.MarshalIndent(allMetrics, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal metric types: %w", err)
}

fmt.Println(string(x))
case "raw":
x, err := yaml.Marshal(allMetrics)
if err != nil {
return fmt.Errorf("failed to marshal metric types: %w", err)
}

fmt.Println(string(x))
default:
return fmt.Errorf("output format '%s' not supported for this command", outputFormat)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package climetrics

import (
"github.com/spf13/cobra"
Expand All @@ -12,7 +12,7 @@ type cliMetrics struct {
cfg configGetter
}

func NewCLI(cfg configGetter) *cliMetrics {
func New(cfg configGetter) *cliMetrics {
return &cliMetrics{
cfg: cfg,
}
Expand All @@ -38,8 +38,8 @@ cscli metrics --url http://lapi.local:6060/metrics show acquisition parsers
cscli metrics list`,
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.show(nil, url, noUnit)
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.show(cmd.Context(), nil, url, noUnit)
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package metrics
package climetrics

import (
"fmt"
"math"
"strconv"
)

type unit struct {
Expand All @@ -20,11 +21,15 @@ var ranges = []unit{
{value: 1, symbol: ""},
}

func formatNumber(num int) string {
goodUnit := unit{}
func formatNumber(num int64, withUnit bool) string {
if !withUnit {
return strconv.FormatInt(num, 10)
}

goodUnit := ranges[len(ranges)-1]

for _, u := range ranges {
if int64(num) >= u.value {
if num >= u.value {
goodUnit = u
break
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package metrics
package climetrics

import (
"context"
"errors"
"fmt"

log "github.com/sirupsen/logrus"

"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
)

var (
ErrMissingConfig = errors.New("prometheus section missing, can't show metrics")
ErrMetricsDisabled = errors.New("prometheus is not enabled, can't show metrics")
)

func (cli *cliMetrics) show(sections []string, url string, noUnit bool) error {
func (cli *cliMetrics) show(ctx context.Context, sections []string, url string, noUnit bool) error {
cfg := cli.cfg()

if url != "" {
Expand All @@ -30,8 +35,13 @@ func (cli *cliMetrics) show(sections []string, url string, noUnit bool) error {

ms := NewMetricStore()

if err := ms.Fetch(cfg.Cscli.PrometheusUrl); err != nil {
return err
db, err := require.DBClient(ctx, cfg.DbConfig)
if err != nil {
log.Warnf("unable to open database: %s", err)
}

if err := ms.Fetch(ctx, cfg.Cscli.PrometheusUrl, db); err != nil {
log.Warn(err)
}

// any section that we don't have in the store is an error
Expand Down Expand Up @@ -90,9 +100,9 @@ cscli metrics list; cscli metrics list -o json
cscli metrics show acquisition parsers scenarios stash -o json`,
// Positional args are optional
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
args = expandAlias(args)
return cli.show(args, url, noUnit)
return cli.show(cmd.Context(), args, url, noUnit)
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package climetrics

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package climetrics

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package climetrics

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package climetrics

import (
"fmt"
Expand Down
Loading

0 comments on commit f130ce6

Please sign in to comment.