mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-17 05:44:57 +00:00
feat(lib): add log filtering rules
Closes #942 Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
@@ -14,16 +14,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
filterInvocations = promauto.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: "techaro",
|
||||
Subsystem: "anubis",
|
||||
Name: "slog_filter_invocations",
|
||||
}, []string{"name"})
|
||||
|
||||
filterExecutionTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: "techaro",
|
||||
Subsystem: "anubis",
|
||||
Name: "slog_filter_execution_time_nanoseconds",
|
||||
Namespace: "anubis",
|
||||
Subsystem: "slog",
|
||||
Name: "filter_execution_time_nanoseconds",
|
||||
Help: "How long each log filter took to execute (nanoseconds)",
|
||||
Buckets: []float64{10, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 5000000, 10000000}, // 10 nanoseconds to 10 milliseconds
|
||||
}, []string{"name"})
|
||||
)
|
||||
@@ -76,7 +71,6 @@ func (f Filter) Filter(ctx context.Context, r slog.Record) bool {
|
||||
}
|
||||
dur := time.Since(t0)
|
||||
filterExecutionTime.WithLabelValues(f.name).Observe(float64(dur.Nanoseconds()))
|
||||
filterInvocations.WithLabelValues(f.name).Inc()
|
||||
//f.log.Debug("filter execution", "dur", dur.Nanoseconds())
|
||||
|
||||
if val, ok := result.(types.Bool); ok {
|
||||
|
||||
@@ -8,8 +8,10 @@ import (
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/TecharoHQ/anubis/lib/logging"
|
||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
||||
"github.com/TecharoHQ/anubis/lib/policy/config"
|
||||
"github.com/TecharoHQ/anubis/lib/policy/expressions"
|
||||
"github.com/TecharoHQ/anubis/lib/store"
|
||||
"github.com/TecharoHQ/anubis/lib/thoth"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
@@ -35,17 +37,42 @@ type ParsedConfig struct {
|
||||
Thresholds []*Threshold
|
||||
DNSBL bool
|
||||
Impressum *config.Impressum
|
||||
Logging *config.Logging
|
||||
OpenGraph config.OpenGraph
|
||||
DefaultDifficulty int
|
||||
StatusCodes config.StatusCodes
|
||||
Store store.Interface
|
||||
}
|
||||
|
||||
func (pc *ParsedConfig) ApplyLogFilters(base *slog.Logger) (*slog.Logger, error) {
|
||||
var errs []error
|
||||
var filters []logging.Filterer
|
||||
|
||||
for _, f := range pc.Logging.Filters {
|
||||
filter, err := expressions.NewFilter(base, f.Name, f.Expression.String())
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("filter %s invalid: %w", f.Name, err))
|
||||
continue
|
||||
}
|
||||
filters = append(filters, filter)
|
||||
}
|
||||
|
||||
result := slog.New(logging.NewFilterHandler(base.Handler(), filters...))
|
||||
slog.SetDefault(result)
|
||||
|
||||
if len(errs) != 0 {
|
||||
return nil, errors.Join(errs...)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func newParsedConfig(orig *config.Config) *ParsedConfig {
|
||||
return &ParsedConfig{
|
||||
orig: orig,
|
||||
OpenGraph: orig.OpenGraph,
|
||||
StatusCodes: orig.StatusCodes,
|
||||
Logging: orig.Logging,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user