From 2004e1b2f5e5e31555cfe8f27ad3ca9dd183bc42 Mon Sep 17 00:00:00 2001 From: Natalia Goc Date: Tue, 10 Sep 2024 15:52:03 +0200 Subject: [PATCH] feat: add a new single-line formatter for console_exporter Closes #7 --- example/main.go | 3 + .../console_exporter/singleline_formatter.go | 102 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 pkg/exporters/console_exporter/singleline_formatter.go diff --git a/example/main.go b/example/main.go index 586041e..ca85833 100644 --- a/example/main.go +++ b/example/main.go @@ -26,8 +26,11 @@ func main() { envFilter := "OBSERVER_CONSOLE" + singlelineFmt := console_exporter.NewSimpleSinglelineFormatter() + exps = append(exps, exporters.DevConsoleExporter(console_exporter.ProcessorOptions{ FilterFromEnvVar: &envFilter, + TraceFormatter: &singlelineFmt, })) gelfExp, err := exporters.GelfExporter() diff --git a/pkg/exporters/console_exporter/singleline_formatter.go b/pkg/exporters/console_exporter/singleline_formatter.go new file mode 100644 index 0000000..2f3fa5e --- /dev/null +++ b/pkg/exporters/console_exporter/singleline_formatter.go @@ -0,0 +1,102 @@ +package console_exporter + +import ( + "fmt" + "slices" + "time" + + "git.ma-al.com/maal-libraries/observer/pkg/console_fmt" + "git.ma-al.com/maal-libraries/observer/pkg/level" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/sdk/trace" +) + +func NewSimpleSinglelineFormatter() TraceFormatter { + return &SimpleSinglelineFormatter{} +} + +// A simple formatter that will print only events using a single line per entry +type SimpleSinglelineFormatter struct{} + +func (f *SimpleSinglelineFormatter) FormatSpanStart(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) { + attrs := "" + slices.SortFunc(selectedAttrs, func(a, b attribute.KeyValue) int { + if a.Key > b.Key { + return 1 + } else { + return -1 + } + }) + + if len(selectedAttrs) > 0 { + attrs += " |" + for _, kv := range selectedAttrs { + if len(kv.Key) > 0 { + attrs += fmt.Sprintf(" %s=%s", string(kv.Key), AttrValueToString(kv.Value)) + } + } + } + + formattedSpanString := fmt.Sprintf( + "%s%s\n", + console_fmt.Bold(console_fmt.SeverityLevelToColor(lvl)+fmt.Sprintf("%s %s [SpanStart] - ", time.Now().Format("2006-01-02 15:04:05"), lvl.String())+span.Name()), + attrs, + ) + + return formattedSpanString, nil +} +func (f *SimpleSinglelineFormatter) FormatSpanEnd(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) { + attrs := "" + slices.SortFunc(selectedAttrs, func(a, b attribute.KeyValue) int { + if a.Key > b.Key { + return 1 + } else { + return -1 + } + }) + + if len(selectedAttrs) > 0 { + attrs += " |" + for _, kv := range selectedAttrs { + if len(kv.Key) > 0 { + attrs += fmt.Sprintf(" %s=%s", string(kv.Key), AttrValueToString(kv.Value)) + } + } + } + + formattedSpanString := fmt.Sprintf( + "%s%s\n", + console_fmt.Bold(console_fmt.SeverityLevelToColor(lvl)+fmt.Sprintf("%s %s [SpanEnd] - ", time.Now().Format("2006-01-02 15:04:05"), lvl.String())+span.Name()), + attrs, + ) + + return formattedSpanString, nil + +} +func (f *SimpleSinglelineFormatter) FormatEvent(event trace.Event, span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) { + attrs := "" + slices.SortFunc(selectedAttrs, func(a, b attribute.KeyValue) int { + if a.Key > b.Key { + return 1 + } else { + return -1 + } + }) + + if len(selectedAttrs) > 0 { + attrs += " |" + for _, kv := range selectedAttrs { + if len(kv.Key) > 0 { + attrs += fmt.Sprintf(" %s=%s", string(kv.Key), AttrValueToString(kv.Value)) + } + } + } + + formattedSpanString := fmt.Sprintf( + "%s%s\n", + console_fmt.Bold(console_fmt.SeverityLevelToColor(lvl)+fmt.Sprintf("%s %s [Event] - ", time.Now().Format("2006-01-02 15:04:05"), lvl.String())+event.Name), + attrs, + ) + + return formattedSpanString, nil +}