123456789101112131415161718192021222324252627282930313233343536 |
- package opentelemetry
- import (
- "context"
- extConfig "go-admin/config"
- "go.opentelemetry.io/otel"
- "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
- stdout "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
- "go.opentelemetry.io/otel/propagation"
- "go.opentelemetry.io/otel/sdk/resource"
- sdktrace "go.opentelemetry.io/otel/sdk/trace"
- )
- func initTracer(res *resource.Resource) (*sdktrace.TracerProvider, error) {
- otelConfig := extConfig.ExtConfig.Otel
- endpoint := otelConfig.Common.Endpoint
- exporter := new(sdktrace.SpanExporter)
- var err error
- if endpoint == "" {
- *exporter, err = stdout.New(stdout.WithPrettyPrint())
- } else {
- *exporter, err = otlptracehttp.New(context.TODO(), otlptracehttp.WithEndpoint(endpoint), otlptracehttp.WithInsecure())
- }
- if err != nil {
- return nil, err
- }
- tp := sdktrace.NewTracerProvider(
- sdktrace.WithResource(res),
- sdktrace.WithSampler(sdktrace.AlwaysSample()),
- sdktrace.WithBatcher(*exporter),
- )
- otel.SetTracerProvider(tp)
- otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
- return tp, nil
- }
|