github.com/rudderlabs/rudder-go-kit@v0.30.0/stats/internal/otel/options.go (about)

     1  package otel
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"go.opentelemetry.io/otel/propagation"
     8  	"go.opentelemetry.io/otel/sdk/instrumentation"
     9  	sdkmetric "go.opentelemetry.io/otel/sdk/metric"
    10  	sdktrace "go.opentelemetry.io/otel/sdk/trace"
    11  )
    12  
    13  type (
    14  	// Option allows to configure the OpenTelemetry initialization
    15  	Option func(*config)
    16  	// TracerProviderOption allows to configure the tracer provider
    17  	TracerProviderOption func(providerConfig *tracerProviderConfig)
    18  	// MeterProviderOption allows to configure the meter provider
    19  	MeterProviderOption func(providerConfig *meterProviderConfig)
    20  	// SpanExporter can be used to set a custom span exporter (e.g. for testing purposes)
    21  	SpanExporter = sdktrace.SpanExporter
    22  )
    23  
    24  // WithRetryConfig allows to set the retry configuration
    25  func WithRetryConfig(rc RetryConfig) Option {
    26  	return func(c *config) {
    27  		c.retryConfig = &rc
    28  	}
    29  }
    30  
    31  // WithInsecure allows to set the GRPC connection to be insecure
    32  func WithInsecure() Option {
    33  	return func(c *config) {
    34  		// Note the use of insecure transport here. TLS is recommended in production.
    35  		c.withInsecure = true
    36  	}
    37  }
    38  
    39  // WithTextMapPropagator allows to set the text map propagator
    40  // e.g. propagation.TraceContext{}
    41  func WithTextMapPropagator(tmp propagation.TextMapPropagator) Option {
    42  	return func(c *config) {
    43  		c.tracerProviderConfig.textMapPropagator = tmp
    44  	}
    45  }
    46  
    47  // WithCustomTracerProvider forces the usage of a custom exporter for the tracer provider
    48  func WithCustomTracerProvider(se SpanExporter, opts ...TracerProviderOption) Option {
    49  	return func(c *config) {
    50  		c.tracerProviderConfig.enabled = true
    51  		c.tracerProviderConfig.customSpanExporter = se
    52  		for _, opt := range opts {
    53  			opt(&c.tracerProviderConfig)
    54  		}
    55  	}
    56  }
    57  
    58  // WithTracerProvider allows to set the tracer provider and specify if it should be the global one
    59  func WithTracerProvider(endpoint string, opts ...TracerProviderOption) Option {
    60  	return func(c *config) {
    61  		c.tracerProviderConfig.enabled = true
    62  		c.tracesEndpoint = endpoint
    63  		for _, opt := range opts {
    64  			opt(&c.tracerProviderConfig)
    65  		}
    66  	}
    67  }
    68  
    69  // WithTracingSamplingRate allows to set the sampling rate for the tracer provider
    70  // samplingRate >= 1 will always sample.
    71  // samplingRate < 0 is treated as zero.
    72  func WithTracingSamplingRate(rate float64) TracerProviderOption {
    73  	return func(c *tracerProviderConfig) {
    74  		c.samplingRate = rate
    75  	}
    76  }
    77  
    78  // WithTracingSyncer lets you register the exporter with a synchronous SimpleSpanProcessor (e.g. instead of a batching
    79  // asynchronous one).
    80  // NOT RECOMMENDED FOR PRODUCTION USE (use for testing and debugging only).
    81  func WithTracingSyncer() TracerProviderOption {
    82  	return func(c *tracerProviderConfig) {
    83  		c.withSyncer = true
    84  	}
    85  }
    86  
    87  // WithZipkin allows to set the tracer provider to use Zipkin
    88  // This means that the SDK will send the data to Zipkin directly instead of using the collector.
    89  func WithZipkin() TracerProviderOption {
    90  	return func(c *tracerProviderConfig) {
    91  		c.withZipkin = true
    92  	}
    93  }
    94  
    95  // WithGlobalTracerProvider allows to set the tracer provider as the global one
    96  func WithGlobalTracerProvider() TracerProviderOption {
    97  	return func(c *tracerProviderConfig) {
    98  		c.global = true
    99  	}
   100  }
   101  
   102  // WithMeterProvider allows to set the meter provider and specify if it should be the global one plus other options.
   103  func WithMeterProvider(opts ...MeterProviderOption) Option {
   104  	return func(c *config) {
   105  		c.meterProviderConfig.enabled = true
   106  		for _, opt := range opts {
   107  			opt(&c.meterProviderConfig)
   108  		}
   109  	}
   110  }
   111  
   112  // WithGlobalMeterProvider allows to set the meter provider as the global one
   113  func WithGlobalMeterProvider() MeterProviderOption {
   114  	return func(c *meterProviderConfig) {
   115  		c.global = true
   116  	}
   117  }
   118  
   119  // WithGRPCMeterProvider allows to set the meter provider to use GRPC
   120  func WithGRPCMeterProvider(grpcEndpoint string) MeterProviderOption {
   121  	return func(c *meterProviderConfig) {
   122  		c.grpcEndpoint = &grpcEndpoint
   123  	}
   124  }
   125  
   126  // WithMeterProviderExportsInterval configures the intervening time between exports (if less than or equal to zero,
   127  // 60 seconds is used)
   128  func WithMeterProviderExportsInterval(interval time.Duration) MeterProviderOption {
   129  	return func(c *meterProviderConfig) {
   130  		c.exportsInterval = interval
   131  	}
   132  }
   133  
   134  // WithPrometheusExporter allows to enable the Prometheus exporter
   135  func WithPrometheusExporter(registerer prometheus.Registerer) MeterProviderOption {
   136  	return func(c *meterProviderConfig) {
   137  		c.prometheusRegisterer = registerer
   138  	}
   139  }
   140  
   141  // WithDefaultHistogramBucketBoundaries lets you overwrite the default buckets for all histograms.
   142  func WithDefaultHistogramBucketBoundaries(boundaries []float64) MeterProviderOption {
   143  	return func(c *meterProviderConfig) {
   144  		c.defaultHistogramBuckets = sdkmetric.NewView(
   145  			sdkmetric.Instrument{
   146  				Kind: sdkmetric.InstrumentKindHistogram,
   147  			},
   148  			sdkmetric.Stream{
   149  				Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
   150  					Boundaries: boundaries,
   151  				},
   152  			},
   153  		)
   154  	}
   155  }
   156  
   157  // WithHistogramBucketBoundaries allows the creation of a view to overwrite the default buckets of a given histogram.
   158  // meterName is optional.
   159  func WithHistogramBucketBoundaries(instrumentName, meterName string, boundaries []float64) MeterProviderOption {
   160  	var scope instrumentation.Scope
   161  	if meterName != "" {
   162  		scope.Name = meterName
   163  	}
   164  	newView := sdkmetric.NewView(
   165  		sdkmetric.Instrument{
   166  			Name:  instrumentName,
   167  			Scope: scope,
   168  			Kind:  sdkmetric.InstrumentKindHistogram,
   169  		},
   170  		sdkmetric.Stream{
   171  			Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
   172  				Boundaries: boundaries,
   173  			},
   174  		},
   175  	)
   176  	return func(c *meterProviderConfig) {
   177  		c.views = append(c.views, newView)
   178  	}
   179  }
   180  
   181  // WithLogger allows to set the logger
   182  func WithLogger(l logger) Option {
   183  	return func(c *config) { c.logger = l }
   184  }