mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
b2d07e56b5
Related to #5898, this reports an anonymized summary of errors stored in Redis into the analytics payload. For each error stored, this includes: - A `count` attribute with the number of occurrences of the error - A `loc` attribute with the 3 topmost lines in the stack trace. Note that stack traces only contain package name + line number (example: github.com/fleetdm/fleet/server.go:12 This also includes a minor refactor around error types.
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package ctxerr
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAggregate(t *testing.T) {
|
|
t.Run("returns an error if can't find a handler in the context", func(t *testing.T) {
|
|
_, err := Aggregate(context.Background())
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("returns an error if it can't decode one of the errors stored", func(t *testing.T) {
|
|
eh := MockHandler{}
|
|
eh.RetrieveImpl = func(flush bool) ([]*StoredError, error) {
|
|
return []*StoredError{{Chain: []byte("invalid")}}, nil
|
|
}
|
|
ctx := NewContext(context.Background(), eh)
|
|
_, err := Aggregate(ctx)
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("returns an aggregation of the errors stored", func(t *testing.T) {
|
|
eh := MockHandler{}
|
|
eh.RetrieveImpl = func(flush bool) ([]*StoredError, error) {
|
|
return []*StoredError{
|
|
{Count: 10, Chain: []byte(`[{"stack": ["a", "b", "c", "d"]}]`)},
|
|
{Count: 20, Chain: []byte(`[{"stack": ["x", "y"]}]`)},
|
|
{Count: 30, Chain: []byte(`[{"stack": ["a", "b", "c", "d"]}, {"stack": ["x", "y"]}]`)},
|
|
{Count: 40, Chain: []byte(`[{"stack": ["a"]}, {"stack": ["x", "y"]}]`)},
|
|
}, nil
|
|
}
|
|
ctx := NewContext(context.Background(), eh)
|
|
rawAgg, err := Aggregate(ctx)
|
|
require.NoError(t, err)
|
|
|
|
var aggs []errorAgg
|
|
err = json.Unmarshal(rawAgg, &aggs)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []errorAgg{
|
|
{Count: 10, Loc: []string{"a", "b", "c"}},
|
|
{Count: 20, Loc: []string{"x", "y"}},
|
|
{Count: 30, Loc: []string{"a", "b", "c"}},
|
|
{Count: 40, Loc: []string{"a", "x", "y"}},
|
|
}, aggs)
|
|
})
|
|
}
|