fix: numbers invisible in console exporter output

closes #5
This commit is contained in:
Natalia Goc 2024-06-25 15:26:25 +02:00
parent 225620da48
commit fc38f26e1f

View File

@ -18,6 +18,30 @@ func NewPrettyMultilineFormatter() TraceFormatter {
// It uses attributes from the [attr] and [semconv] packages.
type PrettyMultilineFormatter struct{}
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 ""
}
func (f *PrettyMultilineFormatter) FormatSpanStart(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) {
attrs := ""
slices.SortFunc(selectedAttrs, func(a, b attribute.KeyValue) int {
@ -30,7 +54,7 @@ func (f *PrettyMultilineFormatter) FormatSpanStart(span trace.ReadOnlySpan, sele
for _, kv := range selectedAttrs {
if len(kv.Key) > 0 {
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), AttrValueToString(kv.Value))
}
}
@ -54,7 +78,7 @@ func (f *PrettyMultilineFormatter) FormatSpanEnd(span trace.ReadOnlySpan, select
for _, kv := range selectedAttrs {
if len(kv.Key) > 0 {
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), AttrValueToString(kv.Value))
}
}
@ -79,7 +103,7 @@ func (f *PrettyMultilineFormatter) FormatEvent(event trace.Event, span trace.Rea
for _, kv := range selectedAttrs {
if len(kv.Key) > 0 {
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), AttrValueToString(kv.Value))
}
}