reorganize exporters and simplify their use
This commit is contained in:
125
pkg/exporters/console_exporter/console_exporter.go
Normal file
125
pkg/exporters/console_exporter/console_exporter.go
Normal file
@ -0,0 +1,125 @@
|
||||
package console_exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.ma-al.com/gora_filip/pkg/level"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
type TraceFormatter interface {
|
||||
FormatSpans(spans []trace.ReadOnlySpan, removeFields []attribute.Key, verbosityLevel level.SeverityLevel, addTraceId bool, onlyErrors bool) (string, error)
|
||||
}
|
||||
|
||||
// Configuration for the exporter.
|
||||
//
|
||||
// Most of options are passed to the formatter.
|
||||
type ExporterOptions struct {
|
||||
// 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].
|
||||
FilterOnLevel *level.SeverityLevel
|
||||
// Fields that should be removed from the output.
|
||||
FilterOutFields []attribute.Key
|
||||
// Print only trace events instead of whole traces.
|
||||
EmitEventsOnly bool
|
||||
// Add trace id to output
|
||||
EmitTraceId bool
|
||||
// Print output only when an error is found
|
||||
EmitOnlyOnError bool
|
||||
// Used only when `EmitEventsOnly` is set to true.
|
||||
TraceFormatter *TraceFormatter
|
||||
}
|
||||
|
||||
type Exporter struct {
|
||||
lvl level.SeverityLevel
|
||||
removedFields []attribute.Key
|
||||
addTraceId bool
|
||||
onlyErrs bool
|
||||
traceFormatter TraceFormatter
|
||||
printerMu sync.Mutex
|
||||
stoppedMu sync.RWMutex
|
||||
stopped bool
|
||||
}
|
||||
|
||||
// NOTE: The configuration might change in future releases
|
||||
func DefaultConsoleExporter() trace.SpanExporter {
|
||||
lvl := level.DEBUG
|
||||
fmt := NewEventsOnlyFormatter()
|
||||
return NewExporter(ExporterOptions{
|
||||
FilterFromEnvVar: nil,
|
||||
FilterOnLevel: &lvl,
|
||||
FilterOutFields: []attribute.Key{},
|
||||
EmitEventsOnly: false,
|
||||
EmitTraceId: true,
|
||||
EmitOnlyOnError: false,
|
||||
TraceFormatter: &fmt,
|
||||
})
|
||||
}
|
||||
|
||||
func NewExporter(opts ExporterOptions) trace.SpanExporter {
|
||||
var formatter TraceFormatter
|
||||
var lvl level.SeverityLevel
|
||||
|
||||
if opts.TraceFormatter != nil {
|
||||
formatter = *opts.TraceFormatter
|
||||
} else {
|
||||
formatter = TraceFormatter(&EventsOnlyFormatter{})
|
||||
}
|
||||
if opts.FilterOnLevel != nil {
|
||||
lvl = *opts.FilterOnLevel
|
||||
} else {
|
||||
lvl = level.TRACE
|
||||
}
|
||||
|
||||
return &Exporter{
|
||||
traceFormatter: formatter,
|
||||
removedFields: opts.FilterOutFields,
|
||||
addTraceId: opts.EmitTraceId,
|
||||
onlyErrs: opts.EmitOnlyOnError,
|
||||
lvl: lvl,
|
||||
}
|
||||
}
|
||||
|
||||
// Implements [trace.SpanExporter]
|
||||
func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
|
||||
e.stoppedMu.RLock()
|
||||
stopped := e.stopped
|
||||
e.stoppedMu.RUnlock()
|
||||
if stopped {
|
||||
return nil
|
||||
}
|
||||
if len(spans) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
e.printerMu.Lock()
|
||||
defer e.printerMu.Unlock()
|
||||
printLine, err := e.traceFormatter.FormatSpans(spans, e.removedFields, e.lvl, e.addTraceId, e.onlyErrs)
|
||||
if err != nil {
|
||||
fmt.Printf("FAILED TO FORMAT A TRACE WITH ERR: %#v\n", err)
|
||||
}
|
||||
if len(printLine) > 0 {
|
||||
fmt.Println(printLine)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Implements [trace.SpanExporter]
|
||||
func (e *Exporter) Shutdown(ctx context.Context) error {
|
||||
e.stoppedMu.Lock()
|
||||
e.stopped = true
|
||||
e.stoppedMu.Unlock()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
return nil
|
||||
}
|
97
pkg/exporters/console_exporter/event_only_formatter.go
Normal file
97
pkg/exporters/console_exporter/event_only_formatter.go
Normal file
@ -0,0 +1,97 @@
|
||||
package console_exporter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"git.ma-al.com/gora_filip/pkg/attr"
|
||||
"git.ma-al.com/gora_filip/pkg/code_location"
|
||||
"git.ma-al.com/gora_filip/pkg/console_fmt"
|
||||
"git.ma-al.com/gora_filip/pkg/level"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
||||
"go.opentelemetry.io/otel/semconv/v1.25.0"
|
||||
)
|
||||
|
||||
func NewEventsOnlyFormatter() TraceFormatter {
|
||||
return &EventsOnlyFormatter{}
|
||||
}
|
||||
|
||||
// A formatter that will print only events using a multiline format with colors.
|
||||
// It uses attributes from the [attr] and [semconv] packages.
|
||||
type EventsOnlyFormatter struct{}
|
||||
|
||||
func (f *EventsOnlyFormatter) FormatSpans(spans []trace.ReadOnlySpan, removeFields []attribute.Key, verbosityLevel level.SeverityLevel, addTraceId bool, onlyOnError bool) (string, error) {
|
||||
stubs := tracetest.SpanStubsFromReadOnlySpans(spans)
|
||||
|
||||
var formattedSpanString string
|
||||
|
||||
for i := range stubs {
|
||||
stub := &stubs[i]
|
||||
for j := range stub.Events {
|
||||
var attributes map[attribute.Key]string = make(map[attribute.Key]string, 0)
|
||||
var msg string
|
||||
var lvl level.SeverityLevel
|
||||
var isErr bool
|
||||
var location code_location.CodeLocation
|
||||
|
||||
for _, attrKV := range stub.Attributes {
|
||||
if _, exists := attributes[attrKV.Key]; !exists {
|
||||
attributes[attrKV.Key] = attrKV.Value.AsString()
|
||||
}
|
||||
}
|
||||
|
||||
for _, attrKV := range stub.Events[j].Attributes {
|
||||
switch attrKV.Key {
|
||||
case attr.LogMessageLongKey:
|
||||
msg = attrKV.Value.AsString()
|
||||
case attr.LogMessageShortKey:
|
||||
if len(msg) == 0 {
|
||||
msg = attrKV.Value.AsString()
|
||||
}
|
||||
case attr.SeverityLevelKey:
|
||||
lvl = level.FromString(attrKV.Value.AsString())
|
||||
case semconv.CodeFilepathKey:
|
||||
location.FilePath = attrKV.Value.AsString()
|
||||
case semconv.CodeLineNumberKey:
|
||||
location.LineNumber = int(attrKV.Value.AsInt64())
|
||||
case semconv.CodeColumnKey:
|
||||
location.ColumnNumber = int(attrKV.Value.AsInt64())
|
||||
case semconv.ExceptionMessageKey:
|
||||
attributes[attrKV.Key] = attrKV.Value.AsString()
|
||||
isErr = true
|
||||
default:
|
||||
if !slices.Contains(removeFields, attrKV.Key) && len(attrKV.Key) > 0 {
|
||||
attributes[attrKV.Key] = attrKV.Value.AsString()
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(msg) == 0 {
|
||||
msg = stub.Name
|
||||
}
|
||||
if addTraceId {
|
||||
attributes[attribute.Key("trace_id")] = stub.SpanContext.TraceID().String()
|
||||
}
|
||||
if len(location.FilePath) > 0 {
|
||||
attributes["code.location"] = fmt.Sprintf("%s:%d:%d", location.FilePath, location.LineNumber, location.ColumnNumber)
|
||||
}
|
||||
|
||||
if !(!isErr && onlyOnError) && lvl <= verbosityLevel {
|
||||
attrs := ""
|
||||
for k, v := range attributes {
|
||||
attrs += fmt.Sprintf("\t%s%s%s = %s\n", console_fmt.ColorBold, k, console_fmt.ColorReset, v)
|
||||
}
|
||||
|
||||
formattedSpanString += fmt.Sprintf(
|
||||
"%s %s\n%s",
|
||||
fmt.Sprintf("%s[%s]", console_fmt.SeverityLevelToColor(lvl), lvl.String()),
|
||||
fmt.Sprintf("%s%s", msg, console_fmt.ColorReset),
|
||||
attrs,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return formattedSpanString, nil
|
||||
}
|
Reference in New Issue
Block a user