Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cscli metrics: rename buckets -> scenarios #2848

Merged
merged 6 commits into from Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .golangci.yml
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
Expand Up @@ -44,9 +44,8 @@
)

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 @@
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 @@

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 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 @@
for _, section := range args {
switch section {
case "engine":
ret = append(ret, "acquisition", "parsers", "buckets", "stash", "whitelists")
ret = append(ret, "acquisition", "parsers", "scenarios", "stash", "whitelists")

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics.go#L385

Added line #L385 was not covered by tests
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 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 @@
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
@@ -1,6 +1,7 @@
package main

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

// 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 @@
}

t.AddRow(row...)

Check warning on line 48 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L48

Added line #L48 was not covered by tests
numRows++
}
}
Expand Down Expand Up @@ -82,6 +84,7 @@
}

t.AddRow(row...)

Check warning on line 87 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L87

Added line #L87 was not covered by tests
numRows++
}
}
Expand Down Expand Up @@ -120,14 +123,15 @@
}

t.AddRow(row...)

Check warning on line 126 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L126

Added line #L126 was not covered by tests
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) 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)

Check warning on line 156 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L156

Added line #L156 was not covered by tests
} else if numRows > 0 || showEmpty {
title, _ := s.Description()
renderTableTitle(out, "\n"+title+":")
Expand Down Expand Up @@ -352,6 +356,7 @@
strconv.Itoa(astats.Count),
}
t.AddRow(row...)

Check warning on line 359 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L359

Added line #L359 was not covered by tests
numRows++
}

Expand Down Expand Up @@ -400,7 +405,9 @@
sl,
strconv.Itoa(astats[sl]),
}

t.AddRow(row...)

numRows++
}
}
Expand Down Expand Up @@ -515,6 +522,7 @@
strconv.Itoa(hits.Empty),
strconv.Itoa(hits.NonEmpty),
)

Check warning on line 525 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L525

Added line #L525 was not covered by tests
numRows++
}

Expand Down Expand Up @@ -560,6 +568,7 @@
action,
strconv.Itoa(hits),
)

Check warning on line 571 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L571

Added line #L571 was not covered by tests
numRows++
}
}
Expand Down Expand Up @@ -594,6 +603,7 @@
scenario,
strconv.Itoa(hits),
)

Check warning on line 606 in cmd/crowdsec-cli/metrics_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/metrics_table.go#L606

Added line #L606 was not covered by tests
numRows++
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/exprhelpers/exprlib_test.go
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
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
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