dubbo.apache.org/dubbo-go/v3@v3.1.1/otel/trace/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 trace
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  )
    24  
    25  import (
    26  	"github.com/dubbogo/gost/log/logger"
    27  
    28  	"go.opentelemetry.io/contrib/propagators/b3"
    29  
    30  	"go.opentelemetry.io/otel/propagation"
    31  
    32  	"go.opentelemetry.io/otel/sdk/resource"
    33  	sdktrace "go.opentelemetry.io/otel/sdk/trace"
    34  
    35  	semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
    36  )
    37  
    38  type ExporterConfig struct {
    39  	Exporter         string
    40  	Endpoint         string
    41  	SampleMode       string
    42  	SampleRatio      float64
    43  	Propagator       string
    44  	ServiceNamespace string
    45  	ServiceName      string
    46  	ServiceVersion   string
    47  }
    48  
    49  type Exporter interface {
    50  	GetTracerProvider() *sdktrace.TracerProvider
    51  	GetPropagator() propagation.TextMapPropagator
    52  }
    53  
    54  type DefaultExporter struct {
    55  	TracerProvider *sdktrace.TracerProvider
    56  	Propagator     propagation.TextMapPropagator
    57  }
    58  
    59  func (e *DefaultExporter) GetTracerProvider() *sdktrace.TracerProvider {
    60  	return e.TracerProvider
    61  }
    62  
    63  func (e *DefaultExporter) GetPropagator() propagation.TextMapPropagator {
    64  	return e.Propagator
    65  }
    66  
    67  // NewExporter is an absolute function with @customFunc to create a spec exporter
    68  func NewExporter(config *ExporterConfig, customFunc func() (sdktrace.SpanExporter, error)) (tracerProvider *sdktrace.TracerProvider, propagator propagation.TextMapPropagator, err error) {
    69  	if config == nil {
    70  		err = errors.New("otel exporter config is nil")
    71  		return
    72  	}
    73  
    74  	exporter, err := customFunc()
    75  	if err != nil {
    76  		err = fmt.Errorf("failed to create %s exporter: %v", config.Exporter, err)
    77  		logger.Error(err)
    78  		return
    79  	}
    80  
    81  	var samplerOption sdktrace.TracerProviderOption
    82  	switch config.SampleMode {
    83  	case "ratio":
    84  		samplerOption = sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(config.SampleRatio)))
    85  	case "always":
    86  		samplerOption = sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.AlwaysSample()))
    87  	case "never":
    88  		samplerOption = sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.NeverSample()))
    89  	default:
    90  		err = fmt.Errorf("otel sample mode %s not supported", config.SampleMode)
    91  		logger.Error(err)
    92  		return
    93  	}
    94  
    95  	tracerProvider = sdktrace.NewTracerProvider(
    96  		samplerOption,
    97  		sdktrace.WithBatcher(exporter),
    98  		sdktrace.WithResource(resource.NewSchemaless(
    99  			semconv.ServiceNamespaceKey.String(config.ServiceNamespace),
   100  			semconv.ServiceNameKey.String(config.ServiceName),
   101  			semconv.ServiceVersionKey.String(config.ServiceVersion),
   102  		)),
   103  	)
   104  
   105  	switch config.Propagator {
   106  	case "w3c":
   107  		propagator = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
   108  	case "b3":
   109  		b3Propagator := b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader | b3.B3SingleHeader))
   110  		propagator = propagation.NewCompositeTextMapPropagator(b3Propagator, propagation.Baggage{})
   111  	}
   112  
   113  	return tracerProvider, propagator, nil
   114  }