102
									
								
								pkg/exporters/console_exporter/singleline_formatter.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								pkg/exporters/console_exporter/singleline_formatter.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,102 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user