2024-05-17 13:31:35 +00:00
|
|
|
package console_exporter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-17 16:21:09 +00:00
|
|
|
"slices"
|
2024-05-17 13:31:35 +00:00
|
|
|
|
|
|
|
"git.ma-al.com/gora_filip/pkg/console_fmt"
|
|
|
|
"git.ma-al.com/gora_filip/pkg/level"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
)
|
|
|
|
|
2024-05-17 16:21:09 +00:00
|
|
|
func NewPrettyMultilineFormatter() TraceFormatter {
|
|
|
|
return &PrettyMultilineFormatter{}
|
2024-05-17 13:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A formatter that will print only events using a multiline format with colors.
|
|
|
|
// It uses attributes from the [attr] and [semconv] packages.
|
2024-05-17 16:21:09 +00:00
|
|
|
type PrettyMultilineFormatter struct{}
|
2024-05-17 13:31:35 +00:00
|
|
|
|
2024-05-17 16:21:09 +00:00
|
|
|
func (f *PrettyMultilineFormatter) FormatSpanStart(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) {
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs := ""
|
2024-05-17 16:21:09 +00:00
|
|
|
slices.SortFunc(selectedAttrs, func(a, b attribute.KeyValue) int {
|
|
|
|
if a.Key > b.Key {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
for _, kv := range selectedAttrs {
|
|
|
|
if len(kv.Key) > 0 {
|
|
|
|
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedSpanString := fmt.Sprintf(
|
|
|
|
"%s\n%s",
|
|
|
|
console_fmt.Bold(console_fmt.SeverityLevelToColor(lvl)+fmt.Sprintf("[%s][SpanStart] ", lvl.String())+span.Name()),
|
|
|
|
attrs,
|
|
|
|
)
|
|
|
|
|
|
|
|
return formattedSpanString, nil
|
|
|
|
}
|
2024-05-17 16:21:09 +00:00
|
|
|
func (f *PrettyMultilineFormatter) FormatSpanEnd(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) {
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs := ""
|
2024-05-17 16:21:09 +00:00
|
|
|
slices.SortFunc(selectedAttrs, func(a, b attribute.KeyValue) int {
|
|
|
|
if a.Key > b.Key {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
for _, kv := range selectedAttrs {
|
|
|
|
if len(kv.Key) > 0 {
|
|
|
|
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedSpanString := fmt.Sprintf(
|
|
|
|
"%s\n%s",
|
|
|
|
console_fmt.Bold(console_fmt.SeverityLevelToColor(lvl)+fmt.Sprintf("[%s][SpanEnd] ", lvl.String())+span.Name()),
|
|
|
|
attrs,
|
|
|
|
)
|
|
|
|
|
|
|
|
return formattedSpanString, nil
|
|
|
|
|
|
|
|
}
|
2024-05-17 16:21:09 +00:00
|
|
|
func (f *PrettyMultilineFormatter) FormatEvent(event trace.Event, span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) {
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs := ""
|
2024-05-17 16:21:09 +00:00
|
|
|
slices.SortFunc(selectedAttrs, func(a, b attribute.KeyValue) int {
|
|
|
|
if a.Key > b.Key {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
for _, kv := range selectedAttrs {
|
|
|
|
if len(kv.Key) > 0 {
|
|
|
|
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedSpanString := fmt.Sprintf(
|
|
|
|
"%s\n%s",
|
|
|
|
console_fmt.Bold(console_fmt.SeverityLevelToColor(lvl)+fmt.Sprintf("[%s][Event] ", lvl.String())+event.Name),
|
|
|
|
attrs,
|
|
|
|
)
|
|
|
|
|
|
|
|
return formattedSpanString, nil
|
|
|
|
}
|