reorganize exporters and simplify their use
This commit is contained in:
parent
fc92995cc8
commit
e9c3ae1a7b
@ -10,8 +10,7 @@ import (
|
||||
|
||||
"git.ma-al.com/gora_filip/observer/pkg/tracer"
|
||||
"git.ma-al.com/gora_filip/pkg/attr"
|
||||
"git.ma-al.com/gora_filip/pkg/combined_exporter"
|
||||
"git.ma-al.com/gora_filip/pkg/console_exporter"
|
||||
"git.ma-al.com/gora_filip/pkg/exporters"
|
||||
"git.ma-al.com/gora_filip/pkg/fiber_tracing"
|
||||
"git.ma-al.com/gora_filip/pkg/level"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@ -26,19 +25,17 @@ func main() {
|
||||
StreamRequestBody: true,
|
||||
})
|
||||
|
||||
lvl := level.DEBUG
|
||||
exporter := combined_exporter.NewExporter(
|
||||
console_exporter.NewExporter(
|
||||
console_exporter.ExporterOptions{
|
||||
FilterOnLevel: &lvl,
|
||||
//EmitOnlyOnError: true,
|
||||
},
|
||||
))
|
||||
exps := make([]exporters.ExporterWithConfig, 2)
|
||||
exps = append(exps, exporters.DevConsoleExporter())
|
||||
gelfExp, err := exporters.GelfExporter()
|
||||
if err == nil {
|
||||
exps = append(exps, gelfExp)
|
||||
}
|
||||
main.Use(fiber_tracing.NewMiddleware(fiber_tracing.Config{
|
||||
AppName: "example",
|
||||
Version: "0.0.0",
|
||||
ServiceProvider: "maal",
|
||||
Exporter: exporter,
|
||||
Exporters: exps,
|
||||
}))
|
||||
defer fiber_tracing.ShutdownTracer()
|
||||
|
||||
|
@ -1,40 +0,0 @@
|
||||
package combined_exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
type Exporter struct {
|
||||
exporters []trace.SpanExporter
|
||||
}
|
||||
|
||||
func NewExporter(exporters ...trace.SpanExporter) trace.SpanExporter {
|
||||
return &Exporter{
|
||||
exporters: exporters,
|
||||
}
|
||||
}
|
||||
|
||||
// Implements [trace.SpanExporter]
|
||||
func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
|
||||
for _, exp := range e.exporters {
|
||||
exp.ExportSpans(ctx, spans)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Implements [trace.SpanExporter]
|
||||
func (e *Exporter) Shutdown(ctx context.Context) error {
|
||||
var errs []error
|
||||
for _, exp := range e.exporters {
|
||||
err := exp.Shutdown(ctx)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("multiple erros have occured: %#v", errs)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -46,6 +46,21 @@ type Exporter struct {
|
||||
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
|
@ -14,6 +14,10 @@ import (
|
||||
"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{}
|
58
pkg/exporters/exporters.go
Normal file
58
pkg/exporters/exporters.go
Normal file
@ -0,0 +1,58 @@
|
||||
package exporters
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func NewWithConfig(exporter sdktrace.SpanExporter) ExporterWithConfig {
|
||||
return ExporterWithConfig{
|
||||
exporter: exporter,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (ecfg ExporterWithConfig) IntoTraceProviderOption() sdktrace.TracerProviderOption {
|
||||
return sdktrace.WithBatcher(ecfg.exporter, ecfg.config...)
|
||||
}
|
||||
|
||||
// An exporter printing to console with very small delay
|
||||
func DevConsoleExporter(opts ...console_exporter.ExporterOptions) ExporterWithConfig {
|
||||
batchTimeout := (time.Millisecond * 250)
|
||||
exportTimeout := (time.Millisecond * 250)
|
||||
var exporter ExporterWithConfig
|
||||
if len(opts) > 0 {
|
||||
exporter = NewWithConfig(console_exporter.NewExporter(opts[0]))
|
||||
} else {
|
||||
exporter = NewWithConfig(console_exporter.DefaultConsoleExporter())
|
||||
}
|
||||
return exporter.Add(sdktrace.WithBatchTimeout(batchTimeout)).Add(sdktrace.WithExportTimeout(exportTimeout))
|
||||
}
|
||||
|
||||
// Default exporter to Graylog.
|
||||
func GelfExporter(opts ...gelf_exporter.Option) (ExporterWithConfig, error) {
|
||||
gelfExp, err := gelf_exporter.New(opts...)
|
||||
return NewWithConfig(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) (ExporterWithConfig, error) {
|
||||
otlpExp, err := otlphttp_exporter.New(context.Background(), opts...)
|
||||
return NewWithConfig(otlpExp), err
|
||||
}
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"git.ma-al.com/gora_filip/pkg/exporters"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
fiberOpentelemetry "github.com/psmarcin/fiber-opentelemetry/pkg/fiber-otel"
|
||||
"go.opentelemetry.io/otel"
|
||||
@ -22,8 +23,9 @@ var (
|
||||
type Config struct {
|
||||
AppName string
|
||||
Version string
|
||||
// Name of an organization providing the service
|
||||
ServiceProvider string
|
||||
Exporter trc.SpanExporter
|
||||
Exporters []exporters.ExporterWithConfig
|
||||
}
|
||||
|
||||
func newResource(config Config) *resource.Resource {
|
||||
@ -39,7 +41,9 @@ func newResource(config Config) *resource.Resource {
|
||||
func NewMiddleware(config Config) func(*fiber.Ctx) error {
|
||||
var tracerProviders []trc.TracerProviderOption
|
||||
|
||||
tracerProviders = append(tracerProviders, trc.WithBatcher(config.Exporter))
|
||||
for _, exp := range config.Exporters {
|
||||
tracerProviders = append(tracerProviders, exp.IntoTraceProviderOption())
|
||||
}
|
||||
tracerProviders = append(tracerProviders, trc.WithResource(newResource(config)))
|
||||
|
||||
TP = *trc.NewTracerProvider(tracerProviders...)
|
||||
|
Loading…
Reference in New Issue
Block a user