Skip to content

Commit

Permalink
cscli metrics: rename buckets -> scenarios (#2848)
Browse files Browse the repository at this point in the history
* cscli metrics: rename buckets -> scenarios
* update lint configuration
* lint
  • Loading branch information
mmetc authored Feb 15, 2024
1 parent 717fc97 commit e976614
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ linters-settings:
- pkg: "github.com/pkg/errors"
desc: "errors.Wrap() is deprecated in favor of fmt.Errorf()"

wsl:
# Allow blocks to end with comments
allow-trailing-comment: true

linters:
enable-all: true
disable:
Expand Down Expand Up @@ -105,6 +109,7 @@ linters:
# - durationcheck # check for two durations multiplied together
# - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
# - errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
# - execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds
# - exportloopref # checks for pointers to enclosing loop variables
# - funlen # Tool for detection of long functions
# - ginkgolinter # enforces standards of using ginkgo and gomega
Expand Down Expand Up @@ -203,7 +208,6 @@ linters:
#
# Too strict / too many false positives (for now?)
#
- execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds
- exhaustruct # Checks if all structure fields are initialized
- forbidigo # Forbids identifiers
- gochecknoglobals # check that no global variables exist
Expand Down
20 changes: 12 additions & 8 deletions cmd/crowdsec-cli/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ type (
)

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

)

type metricSection interface {
Expand All @@ -59,7 +58,7 @@ type metricStore map[string]metricSection
func NewMetricStore() metricStore {
return metricStore{
"acquisition": statAcquis{},
"buckets": statBucket{},
"scenarios": statBucket{},
"parsers": statParser{},
"lapi": statLapi{},
"lapi-machine": statLapiMachine{},
Expand Down Expand Up @@ -110,7 +109,7 @@ func (ms metricStore) Fetch(url string) error {

mAcquis := ms["acquisition"].(statAcquis)
mParser := ms["parsers"].(statParser)
mBucket := ms["buckets"].(statBucket)
mBucket := ms["scenarios"].(statBucket)
mLapi := ms["lapi"].(statLapi)
mLapiMachine := ms["lapi-machine"].(statLapiMachine)
mLapiBouncer := ms["lapi-bouncer"].(statLapiBouncer)
Expand Down Expand Up @@ -361,7 +360,7 @@ cscli metrics --url http://lapi.local:6060/metrics show acquisition parsers
cscli metrics list`,
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
return cli.show(nil, url, noUnit)
},
}
Expand All @@ -383,7 +382,7 @@ func (cli *cliMetrics) expandSectionGroups(args []string) []string {
for _, section := range args {
switch section {
case "engine":
ret = append(ret, "acquisition", "parsers", "buckets", "stash", "whitelists")
ret = append(ret, "acquisition", "parsers", "scenarios", "stash", "whitelists")
case "lapi":
ret = append(ret, "alerts", "decisions", "lapi", "lapi-bouncer", "lapi-decisions", "lapi-machine")
case "appsec":
Expand Down Expand Up @@ -413,10 +412,13 @@ cscli metrics show
cscli metrics show engine
# Show some specific metrics, show empty tables, connect to a different url
cscli metrics show acquisition parsers buckets stash --url http://lapi.local:6060/metrics
cscli metrics show acquisition parsers scenarios stash --url http://lapi.local:6060/metrics
# To list available metric types, use "cscli metrics list"
cscli metrics list; cscli metrics list -o json
# Show metrics in json format
cscli metrics show acquisition parsers buckets stash -o json`,
cscli metrics show acquisition parsers scenarios stash -o json`,
// Positional args are optional
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -467,12 +469,14 @@ func (cli *cliMetrics) list() error {
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))
}

Expand Down
18 changes: 14 additions & 4 deletions cmd/crowdsec-cli/metrics_table.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io"
"sort"
Expand All @@ -13,7 +14,7 @@ import (
)

// ErrNilTable means a nil pointer was passed instead of a table instance. This is a programming error.
var ErrNilTable = fmt.Errorf("nil table")
var ErrNilTable = errors.New("nil table")

func lapiMetricsToTable(t *table.Table, stats map[string]map[string]map[string]int) int {
// stats: machine -> route -> method -> count
Expand Down Expand Up @@ -44,6 +45,7 @@ func lapiMetricsToTable(t *table.Table, stats map[string]map[string]map[string]i
}

t.AddRow(row...)

numRows++
}
}
Expand Down Expand Up @@ -82,6 +84,7 @@ func wlMetricsToTable(t *table.Table, stats map[string]map[string]map[string]int
}

t.AddRow(row...)

numRows++
}
}
Expand Down Expand Up @@ -120,14 +123,15 @@ func metricsToTable(t *table.Table, stats map[string]map[string]int, keys []stri
}

t.AddRow(row...)

numRows++
}

return numRows, nil
}

func (s statBucket) Description() (string, string) {
return "Bucket Metrics",
return "Scenario Metrics",
`Measure events in different scenarios. Current count is the number of buckets during metrics collection. ` +
`Overflows are past event-producing buckets, while Expired are the ones that didn’t receive enough events to Overflow.`
}
Expand All @@ -143,13 +147,13 @@ func (s statBucket) Process(bucket, metric string, val int) {
func (s statBucket) Table(out io.Writer, noUnit bool, showEmpty bool) {
t := newTable(out)
t.SetRowLines(false)
t.SetHeaders("Bucket", "Current Count", "Overflows", "Instantiated", "Poured", "Expired")
t.SetHeaders("Scenario", "Current Count", "Overflows", "Instantiated", "Poured", "Expired")
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)

keys := []string{"curr_count", "overflow", "instantiation", "pour", "underflow"}

if numRows, err := metricsToTable(t, s, keys, noUnit); err != nil {
log.Warningf("while collecting bucket stats: %s", err)
log.Warningf("while collecting scenario stats: %s", err)
} else if numRows > 0 || showEmpty {
title, _ := s.Description()
renderTableTitle(out, "\n"+title+":")
Expand Down Expand Up @@ -352,6 +356,7 @@ func (s statStash) Table(out io.Writer, noUnit bool, showEmpty bool) {
strconv.Itoa(astats.Count),
}
t.AddRow(row...)

numRows++
}

Expand Down Expand Up @@ -400,7 +405,9 @@ func (s statLapi) Table(out io.Writer, noUnit bool, showEmpty bool) {
sl,
strconv.Itoa(astats[sl]),
}

t.AddRow(row...)

numRows++
}
}
Expand Down Expand Up @@ -515,6 +522,7 @@ func (s statLapiDecision) Table(out io.Writer, noUnit bool, showEmpty bool) {
strconv.Itoa(hits.Empty),
strconv.Itoa(hits.NonEmpty),
)

numRows++
}

Expand Down Expand Up @@ -560,6 +568,7 @@ func (s statDecision) Table(out io.Writer, noUnit bool, showEmpty bool) {
action,
strconv.Itoa(hits),
)

numRows++
}
}
Expand Down Expand Up @@ -594,6 +603,7 @@ func (s statAlert) Table(out io.Writer, noUnit bool, showEmpty bool) {
scenario,
strconv.Itoa(hits),
)

numRows++
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/exprhelpers/exprlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestDistanceHelper(t *testing.T) {
ret, err := expr.Run(vm, env)
if test.valid {
require.NoError(t, err)
assert.Equal(t, test.dist, ret)
assert.InDelta(t, test.dist, ret, 0.000001)
} else {
require.Error(t, err)
}
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestAtof(t *testing.T) {
require.NoError(t, err)
output, err := expr.Run(program, test.env)
require.NoError(t, err)
require.Equal(t, test.result, output)
require.InDelta(t, test.result, output, 0.000001)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ statics:
> `filter: "Line.Src endsWith '/foobar'"`

- *optional* `filter` : an [expression](https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.md) that will be evaluated against the runtime of a line (`Event`)
- *optional* `filter` : an [expression](https://github.com/antonmedv/expr/blob/master/docs/language-definition.md) that will be evaluated against the runtime of a line (`Event`)
- if the `filter` is present and returns false, node is not evaluated
- if `filter` is absent or present and returns true, node is evaluated

Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ services:
and must all return true for a service to be detected (implied *and* clause, no
short-circuit). A missing or empty `when:` section is evaluated as true.
The [expression
engine](https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.md)
engine](https://github.com/antonmedv/expr/blob/master/docs/language-definition.md)
is the same one used by CrowdSec parser filters. You can force the detection of
a process by using the `cscli setup detect... --force-process <processname>`
flag. It will always behave as if `<processname>` was running.
Expand Down

0 comments on commit e976614

Please sign in to comment.