Skip to content

Commit

Permalink
cscli metrics show, metrics list
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 2, 2024
1 parent b207d03 commit 1ebd255
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 38 deletions.
130 changes: 93 additions & 37 deletions cmd/crowdsec-cli/metrics.go
Expand Up @@ -46,9 +46,22 @@ type metricSection interface {

type metricStore map[string]metricSection

func NewMetricStore(url string) (*metricStore, error) {
ms := make(metricStore)
var metricSections = []string{
"acquisition",
"buckets",
"parsers",
"lapi",
"lapi_machine",
"lapi_bouncer",
"lapi_decisions",
"decisions",
"alerts",
"stash",
"appsec_engine",
"appsec_rule",
}

func NewMetricStore(url string) (metricStore, error) {
mfChan := make(chan *dto.MetricFamily, 1024)
errChan := make(chan error, 1)

Expand Down Expand Up @@ -278,20 +291,20 @@ func NewMetricStore(url string) (*metricStore, error) {
}
}

ms["acquisition"] = mAcquis
ms["buckets"] = mBucket
ms["parsers"] = mParser
ms["lapi"] = mLapi
ms["lapi_machine"] = mLapiMachine
ms["lapi_bouncer"] = mLapiBouncer
ms["lapi_decisions"] = mLapiDecision
ms["decisions"] = mDecision
ms["alerts"] = mAlert
ms["stash"] = mStash
ms["appsec_engine"] = mAppsecEngine
ms["appsec_rule"] = mAppsecRule

return &ms, nil
return metricStore{
"acquisition": mAcquis,
"buckets": mBucket,
"parsers": mParser,
"lapi": mLapi,
"lapi_machine": mLapiMachine,
"lapi_bouncer": mLapiBouncer,
"lapi_decisions": mLapiDecision,
"decisions": mDecision,
"alerts": mAlert,
"stash": mStash,
"appsec_engine": mAppsecEngine,
"appsec_rule": mAppsecRule,
}, nil
}

type cliMetrics struct {
Expand All @@ -304,33 +317,31 @@ func NewCLIMetrics(getconfig configGetter) *cliMetrics {
}
}

func (ms *metricStore) Format(out io.Writer, formatType string, noUnit bool) error {
if formatType == "human" {
(*ms)["acquisition"].Table(out, noUnit)
(*ms)["buckets"].Table(out, noUnit)
(*ms)["parsers"].Table(out, noUnit)
(*ms)["lapi"].Table(out, noUnit)
(*ms)["lapi_machine"].Table(out, noUnit)
(*ms)["lapi_bouncer"].Table(out, noUnit)
(*ms)["lapi_decisions"].Table(out, noUnit)
(*ms)["decisions"].Table(out, noUnit)
(*ms)["alerts"].Table(out, noUnit)
(*ms)["stash"].Table(out, noUnit)
(*ms)["appsec_engine"].Table(out, noUnit)
(*ms)["appsec_rule"].Table(out, noUnit)

return nil
func (ms metricStore) Format(out io.Writer, sections []string, formatType string, noUnit bool) error {
// copy only the sections we want
want := map[string]metricSection{}

if len(sections) == 0 {
sections = metricSections
}

for _, section := range sections {
want[section] = ms[section]
}

switch formatType {
case "human":
for section := range want {
want[section].Table(out, noUnit)
}
case "json":
x, err := json.MarshalIndent(ms, "", " ")
x, err := json.MarshalIndent(want, "", " ")

Check warning on line 338 in cmd/crowdsec-cli/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics.go#L338

Added line #L338 was not covered by tests
if err != nil {
return fmt.Errorf("failed to unmarshal metrics : %v", err)
}
out.Write(x)
case "raw":
x, err := yaml.Marshal(ms)
x, err := yaml.Marshal(want)

Check warning on line 344 in cmd/crowdsec-cli/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics.go#L344

Added line #L344 was not covered by tests
if err != nil {
return fmt.Errorf("failed to unmarshal metrics : %v", err)
}
Expand All @@ -342,7 +353,7 @@ func (ms *metricStore) Format(out io.Writer, formatType string, noUnit bool) err
return nil
}

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

if url != "" {
Expand All @@ -362,7 +373,7 @@ func (cli *cliMetrics) run(url string, noUnit bool) error {
return err
}

if err := ms.Format(color.Output, cfg.Cscli.Output, noUnit); err != nil {
if err := ms.Format(color.Output, sections, cfg.Cscli.Output, noUnit); err != nil {
return err
}
return nil
Expand All @@ -381,13 +392,58 @@ func (cli *cliMetrics) NewCommand() *cobra.Command {
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.run(url, noUnit)
return cli.show(nil, url, noUnit)
},
}

flags := cmd.Flags()
flags.StringVarP(&url, "url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
flags.BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units")

cmd.AddCommand(cli.newShowCmd())
cmd.AddCommand(cli.newListCmd())

return cmd
}

func (cli *cliMetrics) newShowCmd() *cobra.Command {
var (
url string
noUnit bool
)

cmd := &cobra.Command{
Use: "show [section]...",
Short: "Display crowdsec prometheus metrics.",
Long: `Fetch metrics from the prometheus server and display them in a human-friendly way`,
Args: cobra.MinimumNArgs(1),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.show(args, url, noUnit)

Check warning on line 422 in cmd/crowdsec-cli/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics.go#L422

Added line #L422 was not covered by tests
},
}

flags := cmd.Flags()
flags.StringVarP(&url, "url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
flags.BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units")

return cmd
}

func (cli *cliMetrics) newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List available sections for metrics.",
Long: `List available sections for metrics.`,
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
for _, section := range metricSections {
fmt.Println(section)
}
return nil

Check warning on line 444 in cmd/crowdsec-cli/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics.go#L441-L444

Added lines #L441 - L444 were not covered by tests
},
}

return cmd
}
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/support.go
Expand Up @@ -72,7 +72,7 @@ func collectMetrics() ([]byte, []byte, error) {
return nil, nil, fmt.Errorf("could not fetch prometheus metrics: %s", err)
}

if err = ms.Format(humanMetrics, "human", false); err != nil {
if err = ms.Format(humanMetrics, nil, "human", false); err != nil {
return nil, nil, err

Check warning on line 76 in cmd/crowdsec-cli/support.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/support.go#L75-L76

Added lines #L75 - L76 were not covered by tests
}

Expand Down

0 comments on commit 1ebd255

Please sign in to comment.