dubbo.apache.org/dubbo-go/v3@v3.1.1/otel/trace/otlp/exporter.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package otlp 19 20 import ( 21 "context" 22 "sync" 23 ) 24 25 import ( 26 "go.opentelemetry.io/otel/exporters/otlp/otlptrace" 27 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" 28 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" 29 30 sdktrace "go.opentelemetry.io/otel/sdk/trace" 31 ) 32 33 import ( 34 "dubbo.apache.org/dubbo-go/v3/common/extension" 35 "dubbo.apache.org/dubbo-go/v3/otel/trace" 36 ) 37 38 var ( 39 initHttpOnce sync.Once 40 httpInstance *Exporter 41 42 initGrpcOnce sync.Once 43 grpcInstance *Exporter 44 ) 45 46 func init() { 47 extension.SetTraceExporter("otlp-http", newHttpExporter) 48 extension.SetTraceExporter("otlp-grpc", newHttpExporter) 49 } 50 51 type Exporter struct { 52 *trace.DefaultExporter 53 } 54 55 func newHttpExporter(config *trace.ExporterConfig) (trace.Exporter, error) { 56 var initError error 57 if httpInstance == nil { 58 initHttpOnce.Do(func() { 59 customFunc := func() (sdktrace.SpanExporter, error) { 60 client := otlptracehttp.NewClient(otlptracehttp.WithEndpoint(config.Endpoint)) 61 return otlptrace.New(context.Background(), client) 62 } 63 64 tracerProvider, propagator, err := trace.NewExporter(config, customFunc) 65 if err != nil { 66 initError = err 67 return 68 } 69 70 httpInstance = &Exporter{ 71 DefaultExporter: &trace.DefaultExporter{ 72 TracerProvider: tracerProvider, 73 Propagator: propagator, 74 }, 75 } 76 }) 77 } 78 return httpInstance, initError 79 } 80 81 func newGrpcExporter(config *trace.ExporterConfig) (trace.Exporter, error) { 82 var initError error 83 if grpcInstance == nil { 84 initGrpcOnce.Do(func() { 85 customFunc := func() (sdktrace.SpanExporter, error) { 86 client := otlptracegrpc.NewClient(otlptracegrpc.WithEndpoint(config.Endpoint)) 87 return otlptrace.New(context.Background(), client) 88 } 89 90 tracerProvider, propagator, err := trace.NewExporter(config, customFunc) 91 if err != nil { 92 initError = err 93 return 94 } 95 96 grpcInstance = &Exporter{ 97 DefaultExporter: &trace.DefaultExporter{ 98 TracerProvider: tracerProvider, 99 Propagator: propagator, 100 }, 101 } 102 }) 103 } 104 return grpcInstance, initError 105 }