tracing: add EndpointURL option

This commit is contained in:
2024-03-16 10:40:43 -07:00
parent 4ed44c72a4
commit a458dcb226
3 changed files with 19 additions and 136 deletions

View File

@@ -11,6 +11,7 @@ import (
"go.ntppool.org/common/logger"
"go.ntppool.org/common/version"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
@@ -41,6 +42,7 @@ type TracerConfig struct {
ServiceName string
Environment string
Endpoint string
EndpointURL string
CertificateProvider GetClientCertificate
RootCAs *x509.CertPool
@@ -97,10 +99,14 @@ func InitTracer(ctx context.Context, cfg *TracerConfig) (TpShutdownFunc, error)
func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (otelsdktrace.SpanExporter, error) {
opts := []otlptracehttp.Option{otlptracehttp.WithCompression(otlptracehttp.GzipCompression)}
log := logger.Setup()
opts := []otlptracehttp.Option{
otlptracehttp.WithCompression(otlptracehttp.GzipCompression),
}
if cfg.CertificateProvider != nil {
logger.Setup().Info("setting up cert provider")
log.InfoContext(ctx, "setting up cert provider")
opts = append(opts, otlptracehttp.WithTLSClientConfig(&tls.Config{
GetClientCertificate: cfg.CertificateProvider,
RootCAs: cfg.RootCAs,
@@ -111,10 +117,14 @@ func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (otelsdktrace.SpanE
opts = append(opts, otlptracehttp.WithEndpoint(cfg.Endpoint))
}
if len(cfg.EndpointURL) > 0 {
opts = append(opts, otlptracehttp.WithEndpointURL(cfg.EndpointURL))
}
client := otlptracehttp.NewClient(opts...)
exporter, err := otlptrace.New(ctx, client)
if err != nil {
logger.Setup().Error("creating OTLP trace exporter", "err", err)
log.ErrorContext(ctx, "creating OTLP trace exporter", "err", err)
}
return exporter, err
}