observer/pkg/exporters/exporters.go

77 lines
2.3 KiB
Go
Raw Normal View History

package exporters
import (
"context"
"git.ma-al.com/gora_filip/pkg/exporters/console_exporter"
gelf_exporter "git.ma-al.com/gora_filip/pkg/exporters/gelf_exporter"
otlphttp_exporter "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// Private type preventing implementation of TraceProcessor by external packages.
type traceProviderOpt sdktrace.TracerProviderOption
type TraceExporter interface {
IntoTraceProviderOption() traceProviderOpt
}
func NewFromSpanExporter(exporter sdktrace.SpanExporter) ExporterWithOptions {
return ExporterWithOptions{
exporter: exporter,
}
}
// Sneaky wrapper that makes it so that the TraceExporter can be created from SpanProcessor.
type proc struct {
sdktrace.SpanProcessor
}
func (p proc) IntoTraceProviderOption() traceProviderOpt {
return sdktrace.WithSpanProcessor(p)
}
func NewFromSpanProcessor(processor sdktrace.SpanProcessor) TraceExporter {
return TraceExporter(proc{
SpanProcessor: processor,
})
}
// Combined exporter with batch processor config
type ExporterWithOptions struct {
exporter sdktrace.SpanExporter
opts []sdktrace.BatchSpanProcessorOption
}
func (ecfg ExporterWithOptions) AddOption(opt sdktrace.BatchSpanProcessorOption) ExporterWithOptions {
ecfg.opts = append(ecfg.opts, opt)
return ecfg
}
func (ecfg ExporterWithOptions) IntoTraceProviderOption() traceProviderOpt {
return sdktrace.WithBatcher(ecfg.exporter, ecfg.opts...)
}
// An exporter printing to console with very small delay
func DevConsoleExporter(opts ...console_exporter.ProcessorOptions) TraceExporter {
var exporter TraceExporter
if len(opts) > 0 {
exporter = NewFromSpanProcessor(console_exporter.NewProcessor(opts[0]))
} else {
exporter = NewFromSpanProcessor(console_exporter.DefaultConsoleExportProcessor())
}
return TraceExporter(exporter)
}
// Default exporter to Graylog.
func GelfExporter(opts ...gelf_exporter.Option) (ExporterWithOptions, error) {
gelfExp, err := gelf_exporter.New(opts...)
return NewFromSpanExporter(gelfExp), err
}
// Exporter for traces over HTTP. Can be used with Jaeger.
// See documentation of [otlhttp_exporter] for details.
func OtlpHTTPExporter(opts ...otlphttp_exporter.Option) (ExporterWithOptions, error) {
otlpExp, err := otlphttp_exporter.New(context.Background(), opts...)
return NewFromSpanExporter(otlpExp), err
}