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
|
|
|
|
2024-05-20 06:20:13 +00:00
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/console_fmt"
|
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/level"
|
2024-05-17 13:31:35 +00:00
|
|
|
"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-06-25 13:26:25 +00:00
|
|
|
func AttrValueToString(val attribute.Value) string {
|
|
|
|
switch val.Type() {
|
|
|
|
case attribute.STRING:
|
|
|
|
return val.AsString()
|
|
|
|
case attribute.BOOL:
|
|
|
|
if val.AsBool() {
|
|
|
|
return "true"
|
|
|
|
} else {
|
|
|
|
return "false"
|
|
|
|
}
|
|
|
|
case attribute.BOOLSLICE, attribute.INT64SLICE, attribute.FLOAT64SLICE, attribute.STRINGSLICE:
|
|
|
|
json, _ := val.MarshalJSON()
|
|
|
|
return string(json)
|
|
|
|
case attribute.FLOAT64:
|
|
|
|
fmt.Sprintf("%f", val.AsFloat64())
|
|
|
|
case attribute.INT64:
|
|
|
|
return fmt.Sprintf("%d", val.AsInt64())
|
|
|
|
default:
|
|
|
|
json, _ := val.MarshalJSON()
|
|
|
|
return string(json)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-06-25 13:26:25 +00:00
|
|
|
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), AttrValueToString(kv.Value))
|
2024-05-17 13:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-06-25 13:26:25 +00:00
|
|
|
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), AttrValueToString(kv.Value))
|
2024-05-17 13:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-06-25 13:26:25 +00:00
|
|
|
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), AttrValueToString(kv.Value))
|
2024-05-17 13:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|