2024-08-26 11:00:51 +00:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
2024-08-26 14:33:40 +00:00
|
|
|
"go.opentelemetry.io/otel/propagation"
|
2024-08-26 11:00:51 +00:00
|
|
|
"go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetupTraceProvider(ctx context.Context, plugName string) error {
|
|
|
|
res, err := newResource(plugName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-26 14:33:40 +00:00
|
|
|
prop := newPropagator()
|
|
|
|
otel.SetTextMapPropagator(prop)
|
|
|
|
|
2024-08-26 11:00:51 +00:00
|
|
|
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
|
|
|
|
}
|
2024-08-26 14:33:40 +00:00
|
|
|
|
|
|
|
func newPropagator() propagation.TextMapPropagator {
|
|
|
|
return propagation.NewCompositeTextMapPropagator(
|
|
|
|
propagation.TraceContext{},
|
|
|
|
propagation.Baggage{},
|
|
|
|
)
|
|
|
|
}
|