feat: add a new single-line formatter for console_exporter

Closes #7
This commit is contained in:
Natalia Goc 2024-09-10 15:52:03 +02:00
parent dc4d3942f5
commit 2004e1b2f5
2 changed files with 105 additions and 0 deletions

View File

@ -26,8 +26,11 @@ func main() {
envFilter := "OBSERVER_CONSOLE" envFilter := "OBSERVER_CONSOLE"
singlelineFmt := console_exporter.NewSimpleSinglelineFormatter()
exps = append(exps, exporters.DevConsoleExporter(console_exporter.ProcessorOptions{ exps = append(exps, exporters.DevConsoleExporter(console_exporter.ProcessorOptions{
FilterFromEnvVar: &envFilter, FilterFromEnvVar: &envFilter,
TraceFormatter: &singlelineFmt,
})) }))
gelfExp, err := exporters.GelfExporter() gelfExp, err := exporters.GelfExporter()

View File

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