tracing: option to set endpoint without environment variables
This commit is contained in:
parent
61d73f7be3
commit
0a92ad768e
@ -37,6 +37,7 @@ type GetClientCertificate func(*tls.CertificateRequestInfo) (*tls.Certificate, e
|
||||
type TracerConfig struct {
|
||||
ServiceName string
|
||||
Environment string
|
||||
Endpoint string
|
||||
|
||||
CertificateProvider GetClientCertificate
|
||||
RootCAs *x509.CertPool
|
||||
@ -55,7 +56,7 @@ func InitTracer(ctx context.Context, cfg *TracerConfig) (TpShutdownFunc, error)
|
||||
var err error
|
||||
var exporter otelsdktrace.SpanExporter
|
||||
|
||||
if otlpEndPoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"); len(otlpEndPoint) > 0 {
|
||||
if otlpEndPoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"); len(otlpEndPoint) > 0 || len(cfg.Endpoint) > 0 {
|
||||
exporter, err = newOLTPExporter(ctx, cfg)
|
||||
}
|
||||
|
||||
@ -75,7 +76,13 @@ func InitTracer(ctx context.Context, cfg *TracerConfig) (TpShutdownFunc, error)
|
||||
)
|
||||
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
||||
|
||||
otel.SetTextMapPropagator(
|
||||
propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, // W3C Trace Context format; https://www.w3.org/TR/trace-context/
|
||||
propagation.Baggage{},
|
||||
),
|
||||
)
|
||||
|
||||
return tp.Shutdown, nil
|
||||
}
|
||||
@ -91,6 +98,10 @@ func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (otelsdktrace.SpanE
|
||||
}))
|
||||
}
|
||||
|
||||
if len(cfg.Endpoint) > 0 {
|
||||
opts = append(opts, otlptracehttp.WithEndpoint(cfg.Endpoint))
|
||||
}
|
||||
|
||||
client := otlptracehttp.NewClient(opts...)
|
||||
exporter, err := otlptrace.New(ctx, client)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user