103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
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
|
|
}
|