github.com/MetalBlockchain/metalgo@v1.11.9/trace/exporter.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package trace 5 6 import ( 7 "context" 8 "time" 9 10 "go.opentelemetry.io/otel/exporters/otlp/otlptrace" 11 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" 12 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" 13 14 sdktrace "go.opentelemetry.io/otel/sdk/trace" 15 ) 16 17 const tracerProviderExportCreationTimeout = 5 * time.Second 18 19 type ExporterConfig struct { 20 Type ExporterType `json:"type"` 21 22 // Endpoint to send metrics to 23 Endpoint string `json:"endpoint"` 24 25 // Headers to send with metrics 26 Headers map[string]string `json:"headers"` 27 28 // If true, don't use TLS 29 Insecure bool `json:"insecure"` 30 } 31 32 func newExporter(config ExporterConfig) (sdktrace.SpanExporter, error) { 33 var client otlptrace.Client 34 switch config.Type { 35 case GRPC: 36 opts := []otlptracegrpc.Option{ 37 otlptracegrpc.WithEndpoint(config.Endpoint), 38 otlptracegrpc.WithHeaders(config.Headers), 39 otlptracegrpc.WithTimeout(tracerExportTimeout), 40 } 41 if config.Insecure { 42 opts = append(opts, otlptracegrpc.WithInsecure()) 43 } 44 client = otlptracegrpc.NewClient(opts...) 45 case HTTP: 46 opts := []otlptracehttp.Option{ 47 otlptracehttp.WithEndpoint(config.Endpoint), 48 otlptracehttp.WithHeaders(config.Headers), 49 otlptracehttp.WithTimeout(tracerExportTimeout), 50 } 51 if config.Insecure { 52 opts = append(opts, otlptracehttp.WithInsecure()) 53 } 54 client = otlptracehttp.NewClient(opts...) 55 default: 56 return nil, errUnknownExporterType 57 } 58 59 ctx, cancel := context.WithTimeout(context.Background(), tracerProviderExportCreationTimeout) 60 defer cancel() 61 return otlptrace.New(ctx, client) 62 }