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 +}