From 225620da480f04041bcf1e1577dd63fcec01c68e Mon Sep 17 00:00:00 2001 From: Natalia Goc Date: Tue, 4 Jun 2024 13:02:29 +0200 Subject: [PATCH] feat: enable filtering console output by severity level --- example/main.go | 8 +++++++- .../console_exporter/console_exporter.go | 13 +++++++++++-- pkg/level/level.go | 17 ++++++++++------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/example/main.go b/example/main.go index 9efe544..586041e 100644 --- a/example/main.go +++ b/example/main.go @@ -23,7 +23,13 @@ func main() { main := fiber.New() exps := make([]exporters.TraceExporter, 0) - exps = append(exps, exporters.DevConsoleExporter(console_exporter.ProcessorOptions{})) + + envFilter := "OBSERVER_CONSOLE" + + exps = append(exps, exporters.DevConsoleExporter(console_exporter.ProcessorOptions{ + FilterFromEnvVar: &envFilter, + })) + gelfExp, err := exporters.GelfExporter() if err == nil { exps = append(exps, gelfExp) diff --git a/pkg/exporters/console_exporter/console_exporter.go b/pkg/exporters/console_exporter/console_exporter.go index 284d45c..517ef54 100644 --- a/pkg/exporters/console_exporter/console_exporter.go +++ b/pkg/exporters/console_exporter/console_exporter.go @@ -3,6 +3,7 @@ package console_exporter import ( "context" "fmt" + "os" "slices" "git.ma-al.com/maal-libraries/observer/pkg/attr" @@ -22,7 +23,8 @@ type TraceFormatter interface { // Most of options are passed to the formatter. type ProcessorOptions struct { // Try to parse filters from an environment variable with a name provided by this field. - // Result will only by applied to unset options. NOT IMPLEMENTED! + // Result will only by applied to unset options. Currently it will only attempt to parse + // severity level from the variable and use that as a filter. FilterFromEnvVar *string // Filter the output based on the [level.SeverityLevel]. FilterOnLevel level.SeverityLevel @@ -79,9 +81,16 @@ func NewProcessor(opts ProcessorOptions) trace.SpanProcessor { fmt := NewPrettyMultilineFormatter() formatter = fmt } + if opts.FilterFromEnvVar != nil { + envFilter := os.Getenv(*opts.FilterFromEnvVar) + parsedLvl := level.FromString(envFilter) + if parsedLvl != level.SeverityLevel(0) { + lvl = parsedLvl + } + } if opts.FilterOnLevel != level.SeverityLevel(0) { lvl = opts.FilterOnLevel - } else { + } else if lvl == level.SeverityLevel(0) { lvl = level.TRACE + 1 } diff --git a/pkg/level/level.go b/pkg/level/level.go index bab75aa..f4c3c7b 100644 --- a/pkg/level/level.go +++ b/pkg/level/level.go @@ -1,6 +1,8 @@ package level import ( + "strings" + "git.ma-al.com/maal-libraries/observer/pkg/syslog" "go.opentelemetry.io/otel/attribute" ) @@ -61,20 +63,21 @@ func (l SeverityLevel) String() string { } func FromString(level string) SeverityLevel { + level = strings.ToLower(level) switch level { - case "ALERT": + case "alert": return ALERT - case "CRIT": + case "crit", "critical": return CRIT - case "ERR": + case "err", "error": return ERR - case "WARN": + case "warn", "warning": return WARN - case "INFO": + case "info": return INFO - case "DEBUG": + case "debug": return DEBUG - case "TRACE": + case "trace": return TRACE default: return unset