github.com/thanos-io/thanos@v0.32.5/pkg/tracing/otlp/config_yaml.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package otlp
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/thanos-io/thanos/pkg/exthttp"
    10  
    11  	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
    12  	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
    13  )
    14  
    15  type retryConfig struct {
    16  	RetryEnabled         bool          `yaml:"retry_enabled"`
    17  	RetryInitialInterval time.Duration `yaml:"retry_initial_interval"`
    18  	RetryMaxInterval     time.Duration `yaml:"retry_max_interval"`
    19  	RetryMaxElapsedTime  time.Duration `yaml:"retry_max_elapsed_time"`
    20  }
    21  
    22  type Config struct {
    23  	ClientType         string            `yaml:"client_type"`
    24  	ServiceName        string            `yaml:"service_name"`
    25  	ReconnectionPeriod time.Duration     `yaml:"reconnection_period"`
    26  	Compression        string            `yaml:"compression"`
    27  	Insecure           bool              `yaml:"insecure"`
    28  	Endpoint           string            `yaml:"endpoint"`
    29  	URLPath            string            `yaml:"url_path"`
    30  	Timeout            time.Duration     `yaml:"timeout"`
    31  	RetryConfig        retryConfig       `yaml:"retry_config"`
    32  	Headers            map[string]string `yaml:"headers"`
    33  	TLSConfig          exthttp.TLSConfig `yaml:"tls_config"`
    34  	SamplerType        string            `yaml:"sampler_type"`
    35  	SamplerParam       string            `yaml:"sampler_param"`
    36  }
    37  
    38  func traceGRPCOptions(config Config) []otlptracegrpc.Option {
    39  	var options []otlptracegrpc.Option
    40  	if config.Endpoint != "" {
    41  		options = append(options, otlptracegrpc.WithEndpoint(config.Endpoint))
    42  	}
    43  
    44  	if config.Insecure {
    45  		options = append(options, otlptracegrpc.WithInsecure())
    46  	}
    47  
    48  	if config.ReconnectionPeriod != 0 {
    49  		options = append(options, otlptracegrpc.WithReconnectionPeriod(config.ReconnectionPeriod))
    50  	}
    51  
    52  	if config.Timeout != 0 {
    53  		options = append(options, otlptracegrpc.WithTimeout(config.Timeout))
    54  	}
    55  
    56  	if config.Compression != "" {
    57  		if config.Compression == "gzip" {
    58  			options = append(options, otlptracegrpc.WithCompressor(config.Compression))
    59  		}
    60  	}
    61  
    62  	if config.RetryConfig.RetryEnabled {
    63  		options = append(options, otlptracegrpc.WithRetry(createGRPCRetryConfig(config)))
    64  	}
    65  
    66  	if config.Headers != nil {
    67  		options = append(options, otlptracegrpc.WithHeaders(config.Headers))
    68  	}
    69  
    70  	return options
    71  }
    72  
    73  func traceHTTPOptions(config Config) []otlptracehttp.Option {
    74  	var options []otlptracehttp.Option
    75  	if config.Endpoint != "" {
    76  		options = append(options, otlptracehttp.WithEndpoint(config.Endpoint))
    77  	}
    78  
    79  	if config.Insecure {
    80  		options = append(options, otlptracehttp.WithInsecure())
    81  	} else {
    82  		tlsConfig, _ := exthttp.NewTLSConfig(&config.TLSConfig)
    83  		options = append(options, otlptracehttp.WithTLSClientConfig(tlsConfig))
    84  	}
    85  
    86  	if config.URLPath != "" {
    87  		options = append(options, otlptracehttp.WithURLPath(config.URLPath))
    88  	}
    89  
    90  	if config.Compression != "" {
    91  		if config.Compression == "gzip" {
    92  			options = append(options, otlptracehttp.WithCompression(otlptracehttp.GzipCompression))
    93  		}
    94  	}
    95  
    96  	if config.Timeout != 0 {
    97  		options = append(options, otlptracehttp.WithTimeout(config.Timeout))
    98  	}
    99  
   100  	if config.RetryConfig.RetryEnabled {
   101  		options = append(options, otlptracehttp.WithRetry(createHTTPRetryConfig(config)))
   102  	}
   103  
   104  	if config.Headers != nil {
   105  		options = append(options, otlptracehttp.WithHeaders(config.Headers))
   106  	}
   107  	// how to specify JSON/binary format here?
   108  
   109  	return options
   110  }
   111  
   112  func createHTTPRetryConfig(config Config) otlptracehttp.RetryConfig {
   113  
   114  	var retryConfig otlptracehttp.RetryConfig
   115  	if config.RetryConfig.RetryInitialInterval != 0 {
   116  		retryConfig.InitialInterval = config.RetryConfig.RetryInitialInterval
   117  	}
   118  
   119  	if config.RetryConfig.RetryMaxInterval != 0 {
   120  		retryConfig.MaxInterval = config.RetryConfig.RetryMaxInterval
   121  	}
   122  
   123  	if config.RetryConfig.RetryMaxElapsedTime != 0 {
   124  		retryConfig.MaxElapsedTime = config.RetryConfig.RetryMaxElapsedTime
   125  	}
   126  
   127  	return retryConfig
   128  }
   129  
   130  func createGRPCRetryConfig(config Config) otlptracegrpc.RetryConfig {
   131  
   132  	var retryConfig otlptracegrpc.RetryConfig
   133  	if config.RetryConfig.RetryInitialInterval != 0 {
   134  		retryConfig.InitialInterval = config.RetryConfig.RetryInitialInterval
   135  	}
   136  
   137  	if config.RetryConfig.RetryMaxInterval != 0 {
   138  		retryConfig.MaxInterval = config.RetryConfig.RetryMaxInterval
   139  	}
   140  
   141  	if config.RetryConfig.RetryMaxElapsedTime != 0 {
   142  		retryConfig.MaxElapsedTime = config.RetryConfig.RetryMaxElapsedTime
   143  	}
   144  
   145  	return retryConfig
   146  }