2024-05-16 16:19:36 +00:00
|
|
|
package console_exporter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-05-17 13:31:35 +00:00
|
|
|
"slices"
|
2024-05-16 16:19:36 +00:00
|
|
|
|
2024-05-20 06:20:13 +00:00
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/attr"
|
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/level"
|
2024-05-16 16:19:36 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TraceFormatter interface {
|
2024-05-17 13:31:35 +00:00
|
|
|
FormatSpanStart(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error)
|
|
|
|
FormatSpanEnd(span trace.ReadOnlySpan, selectedAttrs []attribute.KeyValue, lvl level.SeverityLevel) (string, error)
|
2024-05-17 16:21:09 +00:00
|
|
|
FormatEvent(event trace.Event, span trace.ReadOnlySpan, selectedAttr []attribute.KeyValue, lvl level.SeverityLevel) (string, error)
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configuration for the exporter.
|
|
|
|
//
|
|
|
|
// Most of options are passed to the formatter.
|
2024-05-17 13:31:35 +00:00
|
|
|
type ProcessorOptions struct {
|
2024-05-16 16:19:36 +00:00
|
|
|
// Try to parse filters from an environment variable with a name provided by this field.
|
|
|
|
// Result will only by applied to unset options. NOT IMPLEMENTED!
|
|
|
|
FilterFromEnvVar *string
|
|
|
|
// Filter the output based on the [level.SeverityLevel].
|
2024-05-17 13:31:35 +00:00
|
|
|
FilterOnLevel level.SeverityLevel
|
2024-05-16 16:19:36 +00:00
|
|
|
// Fields that should be removed from the output.
|
|
|
|
FilterOutFields []attribute.Key
|
|
|
|
// Print only trace events instead of whole traces.
|
|
|
|
EmitEventsOnly bool
|
2024-05-17 13:31:35 +00:00
|
|
|
// Add trace id as an attribute.
|
|
|
|
AddTraceId bool
|
2024-05-16 16:19:36 +00:00
|
|
|
// Print output only when an error is found
|
2024-05-17 13:31:35 +00:00
|
|
|
SkipNonErrors bool
|
2024-05-16 16:19:36 +00:00
|
|
|
// Used only when `EmitEventsOnly` is set to true.
|
2024-05-17 13:31:35 +00:00
|
|
|
TraceFormatter *TraceFormatter
|
|
|
|
SkipEmittingOnSpanStart bool
|
|
|
|
SkipEmittingOnSpanEnd bool
|
|
|
|
SkipAttributesOnSpanEnd bool
|
|
|
|
SkippAttributesOnSpanStart bool
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
type Processor struct {
|
|
|
|
lvl level.SeverityLevel
|
|
|
|
removedFields []attribute.Key
|
|
|
|
addTraceId bool
|
|
|
|
onlyErrs bool
|
|
|
|
onlyEvents bool
|
|
|
|
traceFormatter TraceFormatter
|
|
|
|
skipSpanStart bool
|
|
|
|
skipSpanEnd bool
|
|
|
|
skipAttrsOnSpanEnd bool
|
|
|
|
skipAttrsOnSpanStart bool
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 08:37:05 +00:00
|
|
|
// NOTE: The configuration might change in future releases
|
2024-05-17 13:31:35 +00:00
|
|
|
func DefaultConsoleExportProcessor() trace.SpanProcessor {
|
2024-05-17 16:21:09 +00:00
|
|
|
fmt := NewPrettyMultilineFormatter()
|
2024-05-17 13:31:35 +00:00
|
|
|
return NewProcessor(ProcessorOptions{
|
|
|
|
FilterFromEnvVar: nil,
|
|
|
|
FilterOutFields: []attribute.Key{},
|
|
|
|
EmitEventsOnly: false,
|
|
|
|
SkipEmittingOnSpanStart: false,
|
|
|
|
SkippAttributesOnSpanStart: true,
|
|
|
|
AddTraceId: false,
|
|
|
|
TraceFormatter: &fmt,
|
2024-05-17 08:37:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
func NewProcessor(opts ProcessorOptions) trace.SpanProcessor {
|
2024-05-16 16:19:36 +00:00
|
|
|
var formatter TraceFormatter
|
|
|
|
var lvl level.SeverityLevel
|
|
|
|
|
|
|
|
if opts.TraceFormatter != nil {
|
|
|
|
formatter = *opts.TraceFormatter
|
|
|
|
} else {
|
2024-05-20 10:18:03 +00:00
|
|
|
fmt := NewPrettyMultilineFormatter()
|
|
|
|
formatter = fmt
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
if opts.FilterOnLevel != 0 {
|
|
|
|
lvl = opts.FilterOnLevel
|
2024-05-16 16:19:36 +00:00
|
|
|
} else {
|
2024-05-17 13:31:35 +00:00
|
|
|
lvl = level.TRACE + 10
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
return &Processor{
|
2024-05-17 16:21:09 +00:00
|
|
|
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,
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
// Implements [trace.SpanProcessor]
|
|
|
|
func (e *Processor) OnStart(ctx context.Context, span trace.ReadWriteSpan) {
|
|
|
|
if !e.skipSpanStart && !e.onlyEvents {
|
|
|
|
attrs := span.Attributes()
|
|
|
|
filteredAttrs := make([]attribute.KeyValue, 0)
|
|
|
|
severityLvl := level.TRACE
|
|
|
|
|
|
|
|
if !e.skipAttrsOnSpanStart {
|
|
|
|
for i := range attrs {
|
|
|
|
if !slices.Contains(e.removedFields, attrs[i].Key) {
|
|
|
|
filteredAttrs = append(filteredAttrs, attrs[i])
|
|
|
|
}
|
|
|
|
if attrs[i].Key == attr.SeverityLevelKey {
|
|
|
|
severityLvl = level.FromString(attrs[i].Value.AsString())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e.addTraceId {
|
|
|
|
filteredAttrs = append(filteredAttrs, attribute.String("trace_id", span.SpanContext().TraceID().String()))
|
|
|
|
}
|
|
|
|
if severityLvl <= e.lvl {
|
|
|
|
line, err := e.traceFormatter.FormatSpanStart(span, filteredAttrs, severityLvl)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("FAILED TO FORMAT SPAN START")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s", line)
|
|
|
|
}
|
|
|
|
}
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements [trace.SpanProcessor]
|
|
|
|
func (e *Processor) OnEnd(span trace.ReadOnlySpan) {
|
|
|
|
eventsString := ""
|
|
|
|
spanEndString := ""
|
|
|
|
|
|
|
|
for _, event := range span.Events() {
|
|
|
|
attrs := event.Attributes
|
|
|
|
filteredAttrs := make([]attribute.KeyValue, 0)
|
|
|
|
severityLvl := level.TRACE
|
|
|
|
for i := range attrs {
|
|
|
|
if !slices.Contains(e.removedFields, attrs[i].Key) {
|
|
|
|
filteredAttrs = append(filteredAttrs, attrs[i])
|
|
|
|
}
|
|
|
|
if attrs[i].Key == attr.SeverityLevelKey {
|
|
|
|
severityLvl = level.FromString(attrs[i].Value.AsString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e.addTraceId {
|
|
|
|
filteredAttrs = append(filteredAttrs, attribute.String("trace_id", span.SpanContext().TraceID().String()))
|
|
|
|
}
|
|
|
|
if severityLvl <= e.lvl {
|
2024-05-17 16:21:09 +00:00
|
|
|
eventString, err := e.traceFormatter.FormatEvent(event, span, filteredAttrs, severityLvl)
|
2024-05-17 13:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("FAILED TO FORMAT TRACE EVENT")
|
|
|
|
} else {
|
|
|
|
eventsString += eventString
|
|
|
|
}
|
|
|
|
}
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
if !e.skipSpanEnd && !e.onlyEvents {
|
|
|
|
attrs := span.Attributes()
|
|
|
|
filteredAttrs := make([]attribute.KeyValue, len(attrs))
|
|
|
|
severityLvl := level.TRACE
|
|
|
|
|
|
|
|
if !e.skipAttrsOnSpanEnd {
|
|
|
|
for i := range attrs {
|
|
|
|
if !slices.Contains(e.removedFields, attrs[i].Key) {
|
|
|
|
filteredAttrs = append(filteredAttrs, attrs[i])
|
|
|
|
}
|
|
|
|
if attrs[i].Key == attr.SeverityLevelKey {
|
|
|
|
severityLvl = level.FromString(attrs[i].Value.AsString())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e.addTraceId {
|
|
|
|
filteredAttrs = append(filteredAttrs, attribute.String("trace_id", span.SpanContext().TraceID().String()))
|
|
|
|
}
|
|
|
|
if severityLvl <= e.lvl {
|
|
|
|
spanString, err := e.traceFormatter.FormatSpanEnd(span, filteredAttrs, severityLvl)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("FAILED TO FORMAT SPAN END")
|
|
|
|
} else {
|
|
|
|
spanEndString += spanString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
|
|
|
|
if e.skipSpanStart {
|
|
|
|
fmt.Printf("%s", spanEndString+eventsString)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s", eventsString+spanEndString)
|
2024-05-16 16:19:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements [trace.SpanProcessor]
|
|
|
|
func (e *Processor) ForceFlush(ctx context.Context) error {
|
2024-05-16 16:19:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements [trace.SpanExporter]
|
2024-05-17 13:31:35 +00:00
|
|
|
func (e *Processor) Shutdown(ctx context.Context) error {
|
2024-05-16 16:19:36 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|