added event wrappers, bug fixes, API improvements

This commit is contained in:
2024-05-17 18:21:09 +02:00
parent 4f4a7e09c5
commit 076196c03e
7 changed files with 218 additions and 49 deletions

View File

@ -14,7 +14,7 @@ import (
type TraceFormatter interface {
FormatSpanStart(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error)
FormatSpanEnd(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error)
FormatEvent(event trace.Event, selectedAttr []attribute.KeyValue, lvl level.SeverityLevel) (string, error)
FormatEvent(event trace.Event, span trace.ReadOnlySpan, selectedAttr []attribute.KeyValue, lvl level.SeverityLevel) (string, error)
}
// Configuration for the exporter.
@ -57,7 +57,7 @@ type Processor struct {
// NOTE: The configuration might change in future releases
func DefaultConsoleExportProcessor() trace.SpanProcessor {
fmt := NewMultilineFormatter()
fmt := NewPrettyMultilineFormatter()
return NewProcessor(ProcessorOptions{
FilterFromEnvVar: nil,
FilterOutFields: []attribute.Key{},
@ -85,15 +85,16 @@ func NewProcessor(opts ProcessorOptions) trace.SpanProcessor {
}
return &Processor{
traceFormatter: formatter,
removedFields: opts.FilterOutFields,
addTraceId: opts.AddTraceId,
onlyEvents: opts.EmitEventsOnly,
onlyErrs: opts.SkipNonErrors,
skipSpanStart: opts.SkipEmittingOnSpanStart,
skipSpanEnd: opts.SkipEmittingOnSpanEnd,
skipAttrsOnSpanEnd: opts.SkipAttributesOnSpanEnd,
lvl: lvl,
traceFormatter: formatter,
removedFields: opts.FilterOutFields,
addTraceId: opts.AddTraceId,
onlyEvents: opts.EmitEventsOnly,
onlyErrs: opts.SkipNonErrors,
skipSpanStart: opts.SkipEmittingOnSpanStart,
skipSpanEnd: opts.SkipEmittingOnSpanEnd,
skipAttrsOnSpanEnd: opts.SkipAttributesOnSpanEnd,
skipAttrsOnSpanStart: opts.SkippAttributesOnSpanStart,
lvl: lvl,
}
}
@ -151,7 +152,7 @@ func (e *Processor) OnEnd(span trace.ReadOnlySpan) {
filteredAttrs = append(filteredAttrs, attribute.String("trace_id", span.SpanContext().TraceID().String()))
}
if severityLvl <= e.lvl {
eventString, err := e.traceFormatter.FormatEvent(event, filteredAttrs, severityLvl)
eventString, err := e.traceFormatter.FormatEvent(event, span, filteredAttrs, severityLvl)
if err != nil {
fmt.Println("FAILED TO FORMAT TRACE EVENT")
} else {

View File

@ -2,6 +2,7 @@ package console_exporter
import (
"fmt"
"slices"
"git.ma-al.com/gora_filip/pkg/console_fmt"
"git.ma-al.com/gora_filip/pkg/level"
@ -9,16 +10,24 @@ import (
"go.opentelemetry.io/otel/sdk/trace"
)
func NewMultilineFormatter() TraceFormatter {
return &MultilineFormatter{}
func NewPrettyMultilineFormatter() TraceFormatter {
return &PrettyMultilineFormatter{}
}
// A formatter that will print only events using a multiline format with colors.
// It uses attributes from the [attr] and [semconv] packages.
type MultilineFormatter struct{}
type PrettyMultilineFormatter struct{}
func (f *MultilineFormatter) FormatSpanStart(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) {
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 {
if a.Key > b.Key {
return 1
} else {
return -1
}
})
for _, kv := range selectedAttrs {
if len(kv.Key) > 0 {
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
@ -33,8 +42,16 @@ func (f *MultilineFormatter) FormatSpanStart(span trace.ReadOnlySpan, selectedAt
return formattedSpanString, nil
}
func (f *MultilineFormatter) FormatSpanEnd(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) {
func (f *PrettyMultilineFormatter) 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
}
})
for _, kv := range selectedAttrs {
if len(kv.Key) > 0 {
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())
@ -50,8 +67,16 @@ func (f *MultilineFormatter) FormatSpanEnd(span trace.ReadOnlySpan, selectedAttr
return formattedSpanString, nil
}
func (f *MultilineFormatter) FormatEvent(event trace.Event, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error) {
func (f *PrettyMultilineFormatter) 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
}
})
for _, kv := range selectedAttrs {
if len(kv.Key) > 0 {
attrs += fmt.Sprintf("\t%s = %s\n", string(kv.Key), kv.Value.AsString())