2024-05-17 08:37:05 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
// Private type preventing implementation of TraceProcessor by external packages.
|
|
|
|
type traceProviderOpt sdktrace.TracerProviderOption
|
|
|
|
|
|
|
|
type TraceExporter interface {
|
|
|
|
IntoTraceProviderOption() traceProviderOpt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFromSpanExporter(exporter sdktrace.SpanExporter) ExporterWithConfig {
|
2024-05-17 08:37:05 +00:00
|
|
|
return ExporterWithConfig{
|
|
|
|
exporter: exporter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-17 08:37:05 +00:00
|
|
|
// Combined exporter with batch processor config
|
|
|
|
type ExporterWithConfig struct {
|
|
|
|
exporter sdktrace.SpanExporter
|
|
|
|
config []sdktrace.BatchSpanProcessorOption
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ecfg ExporterWithConfig) Add(opt sdktrace.BatchSpanProcessorOption) ExporterWithConfig {
|
|
|
|
ecfg.config = append(ecfg.config, opt)
|
|
|
|
return ecfg
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
func (ecfg ExporterWithConfig) IntoTraceProviderOption() traceProviderOpt {
|
2024-05-17 08:37:05 +00:00
|
|
|
return sdktrace.WithBatcher(ecfg.exporter, ecfg.config...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// An exporter printing to console with very small delay
|
2024-05-17 13:31:35 +00:00
|
|
|
func DevConsoleExporter(opts ...console_exporter.ProcessorOptions) TraceExporter {
|
|
|
|
var exporter TraceExporter
|
2024-05-17 08:37:05 +00:00
|
|
|
if len(opts) > 0 {
|
2024-05-17 13:31:35 +00:00
|
|
|
exporter = NewFromSpanProcessor(console_exporter.NewProcessor(opts[0]))
|
2024-05-17 08:37:05 +00:00
|
|
|
} else {
|
2024-05-17 13:31:35 +00:00
|
|
|
exporter = NewFromSpanProcessor(console_exporter.DefaultConsoleExportProcessor())
|
2024-05-17 08:37:05 +00:00
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
return TraceExporter(exporter)
|
2024-05-17 08:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default exporter to Graylog.
|
|
|
|
func GelfExporter(opts ...gelf_exporter.Option) (ExporterWithConfig, error) {
|
|
|
|
gelfExp, err := gelf_exporter.New(opts...)
|
2024-05-17 13:31:35 +00:00
|
|
|
return NewFromSpanExporter(gelfExp), err
|
2024-05-17 08:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exporter for traces over HTTP. Can be used with Jaeger.
|
|
|
|
// See documentation of [otlhttp_exporter] for details.
|
|
|
|
func OtlpHTTPExporter(opts ...otlphttp_exporter.Option) (ExporterWithConfig, error) {
|
|
|
|
otlpExp, err := otlphttp_exporter.New(context.Background(), opts...)
|
2024-05-17 13:31:35 +00:00
|
|
|
return NewFromSpanExporter(otlpExp), err
|
2024-05-17 08:37:05 +00:00
|
|
|
}
|