feat: enable filtering console output by severity level

This commit is contained in:
Natalia Goc 2024-06-04 13:02:29 +02:00
parent a2b61ea706
commit 225620da48
3 changed files with 28 additions and 10 deletions

View File

@ -23,7 +23,13 @@ func main() {
main := fiber.New() main := fiber.New()
exps := make([]exporters.TraceExporter, 0) 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() gelfExp, err := exporters.GelfExporter()
if err == nil { if err == nil {
exps = append(exps, gelfExp) exps = append(exps, gelfExp)

View File

@ -3,6 +3,7 @@ package console_exporter
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"slices" "slices"
"git.ma-al.com/maal-libraries/observer/pkg/attr" "git.ma-al.com/maal-libraries/observer/pkg/attr"
@ -22,7 +23,8 @@ type TraceFormatter interface {
// Most of options are passed to the formatter. // Most of options are passed to the formatter.
type ProcessorOptions struct { type ProcessorOptions struct {
// Try to parse filters from an environment variable with a name provided by this field. // 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 FilterFromEnvVar *string
// Filter the output based on the [level.SeverityLevel]. // Filter the output based on the [level.SeverityLevel].
FilterOnLevel level.SeverityLevel FilterOnLevel level.SeverityLevel
@ -79,9 +81,16 @@ func NewProcessor(opts ProcessorOptions) trace.SpanProcessor {
fmt := NewPrettyMultilineFormatter() fmt := NewPrettyMultilineFormatter()
formatter = fmt 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) { if opts.FilterOnLevel != level.SeverityLevel(0) {
lvl = opts.FilterOnLevel lvl = opts.FilterOnLevel
} else { } else if lvl == level.SeverityLevel(0) {
lvl = level.TRACE + 1 lvl = level.TRACE + 1
} }

View File

@ -1,6 +1,8 @@
package level package level
import ( import (
"strings"
"git.ma-al.com/maal-libraries/observer/pkg/syslog" "git.ma-al.com/maal-libraries/observer/pkg/syslog"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
) )
@ -61,20 +63,21 @@ func (l SeverityLevel) String() string {
} }
func FromString(level string) SeverityLevel { func FromString(level string) SeverityLevel {
level = strings.ToLower(level)
switch level { switch level {
case "ALERT": case "alert":
return ALERT return ALERT
case "CRIT": case "crit", "critical":
return CRIT return CRIT
case "ERR": case "err", "error":
return ERR return ERR
case "WARN": case "warn", "warning":
return WARN return WARN
case "INFO": case "info":
return INFO return INFO
case "DEBUG": case "debug":
return DEBUG return DEBUG
case "TRACE": case "trace":
return TRACE return TRACE
default: default:
return unset return unset