plug-sdk/pkg/telemetry/tracing.go

53 lines
984 B
Go
Raw Permalink Normal View History

package telemetry
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/trace"
)
func SetupTraceProvider(ctx context.Context, plugName string) error {
res, err := newResource(plugName)
if err != nil {
return err
}
prop := newPropagator()
otel.SetTextMapPropagator(prop)
exporter, err := traceExporter(ctx)
if err != nil {
return err
}
provider := trace.NewTracerProvider(
trace.WithResource(res),
trace.WithBatcher(exporter),
)
otel.SetTracerProvider(provider)
return nil
}
func traceExporter(ctx context.Context) (trace.SpanExporter, error) {
spanExporter, err := otlptracegrpc.New(ctx)
if err != nil {
return nil, err
}
return spanExporter, nil
}
func newPropagator() propagation.TextMapPropagator {
return propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
)
}