...

Source file src/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go

Documentation: go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig

     1  // Code created by gotmpl. DO NOT MODIFY.
     2  // source: internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl
     3  
     4  // Copyright The OpenTelemetry Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig"
    19  
    20  import (
    21  	"crypto/tls"
    22  	"fmt"
    23  	"path"
    24  	"strings"
    25  	"time"
    26  
    27  	"google.golang.org/grpc"
    28  	"google.golang.org/grpc/backoff"
    29  	"google.golang.org/grpc/credentials"
    30  	"google.golang.org/grpc/credentials/insecure"
    31  	"google.golang.org/grpc/encoding/gzip"
    32  
    33  	"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
    34  	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/retry"
    35  )
    36  
    37  const (
    38  	// DefaultTracesPath is a default URL path for endpoint that
    39  	// receives spans.
    40  	DefaultTracesPath string = "/v1/traces"
    41  	// DefaultTimeout is a default max waiting time for the backend to process
    42  	// each span batch.
    43  	DefaultTimeout time.Duration = 10 * time.Second
    44  )
    45  
    46  type (
    47  	SignalConfig struct {
    48  		Endpoint    string
    49  		Insecure    bool
    50  		TLSCfg      *tls.Config
    51  		Headers     map[string]string
    52  		Compression Compression
    53  		Timeout     time.Duration
    54  		URLPath     string
    55  
    56  		// gRPC configurations
    57  		GRPCCredentials credentials.TransportCredentials
    58  	}
    59  
    60  	Config struct {
    61  		// Signal specific configurations
    62  		Traces SignalConfig
    63  
    64  		RetryConfig retry.Config
    65  
    66  		// gRPC configurations
    67  		ReconnectionPeriod time.Duration
    68  		ServiceConfig      string
    69  		DialOptions        []grpc.DialOption
    70  		GRPCConn           *grpc.ClientConn
    71  	}
    72  )
    73  
    74  // NewHTTPConfig returns a new Config with all settings applied from opts and
    75  // any unset setting using the default HTTP config values.
    76  func NewHTTPConfig(opts ...HTTPOption) Config {
    77  	cfg := Config{
    78  		Traces: SignalConfig{
    79  			Endpoint:    fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorHTTPPort),
    80  			URLPath:     DefaultTracesPath,
    81  			Compression: NoCompression,
    82  			Timeout:     DefaultTimeout,
    83  		},
    84  		RetryConfig: retry.DefaultConfig,
    85  	}
    86  	cfg = ApplyHTTPEnvConfigs(cfg)
    87  	for _, opt := range opts {
    88  		cfg = opt.ApplyHTTPOption(cfg)
    89  	}
    90  	cfg.Traces.URLPath = cleanPath(cfg.Traces.URLPath, DefaultTracesPath)
    91  	return cfg
    92  }
    93  
    94  // cleanPath returns a path with all spaces trimmed and all redundancies
    95  // removed. If urlPath is empty or cleaning it results in an empty string,
    96  // defaultPath is returned instead.
    97  func cleanPath(urlPath string, defaultPath string) string {
    98  	tmp := path.Clean(strings.TrimSpace(urlPath))
    99  	if tmp == "." {
   100  		return defaultPath
   101  	}
   102  	if !path.IsAbs(tmp) {
   103  		tmp = fmt.Sprintf("/%s", tmp)
   104  	}
   105  	return tmp
   106  }
   107  
   108  // NewGRPCConfig returns a new Config with all settings applied from opts and
   109  // any unset setting using the default gRPC config values.
   110  func NewGRPCConfig(opts ...GRPCOption) Config {
   111  	userAgent := "OTel OTLP Exporter Go/" + otlptrace.Version()
   112  	cfg := Config{
   113  		Traces: SignalConfig{
   114  			Endpoint:    fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorGRPCPort),
   115  			URLPath:     DefaultTracesPath,
   116  			Compression: NoCompression,
   117  			Timeout:     DefaultTimeout,
   118  		},
   119  		RetryConfig: retry.DefaultConfig,
   120  		DialOptions: []grpc.DialOption{grpc.WithUserAgent(userAgent)},
   121  	}
   122  	cfg = ApplyGRPCEnvConfigs(cfg)
   123  	for _, opt := range opts {
   124  		cfg = opt.ApplyGRPCOption(cfg)
   125  	}
   126  
   127  	if cfg.ServiceConfig != "" {
   128  		cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultServiceConfig(cfg.ServiceConfig))
   129  	}
   130  	// Priroritize GRPCCredentials over Insecure (passing both is an error).
   131  	if cfg.Traces.GRPCCredentials != nil {
   132  		cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(cfg.Traces.GRPCCredentials))
   133  	} else if cfg.Traces.Insecure {
   134  		cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
   135  	} else {
   136  		// Default to using the host's root CA.
   137  		creds := credentials.NewTLS(nil)
   138  		cfg.Traces.GRPCCredentials = creds
   139  		cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(creds))
   140  	}
   141  	if cfg.Traces.Compression == GzipCompression {
   142  		cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
   143  	}
   144  	if cfg.ReconnectionPeriod != 0 {
   145  		p := grpc.ConnectParams{
   146  			Backoff:           backoff.DefaultConfig,
   147  			MinConnectTimeout: cfg.ReconnectionPeriod,
   148  		}
   149  		cfg.DialOptions = append(cfg.DialOptions, grpc.WithConnectParams(p))
   150  	}
   151  
   152  	return cfg
   153  }
   154  
   155  type (
   156  	// GenericOption applies an option to the HTTP or gRPC driver.
   157  	GenericOption interface {
   158  		ApplyHTTPOption(Config) Config
   159  		ApplyGRPCOption(Config) Config
   160  
   161  		// A private method to prevent users implementing the
   162  		// interface and so future additions to it will not
   163  		// violate compatibility.
   164  		private()
   165  	}
   166  
   167  	// HTTPOption applies an option to the HTTP driver.
   168  	HTTPOption interface {
   169  		ApplyHTTPOption(Config) Config
   170  
   171  		// A private method to prevent users implementing the
   172  		// interface and so future additions to it will not
   173  		// violate compatibility.
   174  		private()
   175  	}
   176  
   177  	// GRPCOption applies an option to the gRPC driver.
   178  	GRPCOption interface {
   179  		ApplyGRPCOption(Config) Config
   180  
   181  		// A private method to prevent users implementing the
   182  		// interface and so future additions to it will not
   183  		// violate compatibility.
   184  		private()
   185  	}
   186  )
   187  
   188  // genericOption is an option that applies the same logic
   189  // for both gRPC and HTTP.
   190  type genericOption struct {
   191  	fn func(Config) Config
   192  }
   193  
   194  func (g *genericOption) ApplyGRPCOption(cfg Config) Config {
   195  	return g.fn(cfg)
   196  }
   197  
   198  func (g *genericOption) ApplyHTTPOption(cfg Config) Config {
   199  	return g.fn(cfg)
   200  }
   201  
   202  func (genericOption) private() {}
   203  
   204  func newGenericOption(fn func(cfg Config) Config) GenericOption {
   205  	return &genericOption{fn: fn}
   206  }
   207  
   208  // splitOption is an option that applies different logics
   209  // for gRPC and HTTP.
   210  type splitOption struct {
   211  	httpFn func(Config) Config
   212  	grpcFn func(Config) Config
   213  }
   214  
   215  func (g *splitOption) ApplyGRPCOption(cfg Config) Config {
   216  	return g.grpcFn(cfg)
   217  }
   218  
   219  func (g *splitOption) ApplyHTTPOption(cfg Config) Config {
   220  	return g.httpFn(cfg)
   221  }
   222  
   223  func (splitOption) private() {}
   224  
   225  func newSplitOption(httpFn func(cfg Config) Config, grpcFn func(cfg Config) Config) GenericOption {
   226  	return &splitOption{httpFn: httpFn, grpcFn: grpcFn}
   227  }
   228  
   229  // httpOption is an option that is only applied to the HTTP driver.
   230  type httpOption struct {
   231  	fn func(Config) Config
   232  }
   233  
   234  func (h *httpOption) ApplyHTTPOption(cfg Config) Config {
   235  	return h.fn(cfg)
   236  }
   237  
   238  func (httpOption) private() {}
   239  
   240  func NewHTTPOption(fn func(cfg Config) Config) HTTPOption {
   241  	return &httpOption{fn: fn}
   242  }
   243  
   244  // grpcOption is an option that is only applied to the gRPC driver.
   245  type grpcOption struct {
   246  	fn func(Config) Config
   247  }
   248  
   249  func (h *grpcOption) ApplyGRPCOption(cfg Config) Config {
   250  	return h.fn(cfg)
   251  }
   252  
   253  func (grpcOption) private() {}
   254  
   255  func NewGRPCOption(fn func(cfg Config) Config) GRPCOption {
   256  	return &grpcOption{fn: fn}
   257  }
   258  
   259  // Generic Options
   260  
   261  func WithEndpoint(endpoint string) GenericOption {
   262  	return newGenericOption(func(cfg Config) Config {
   263  		cfg.Traces.Endpoint = endpoint
   264  		return cfg
   265  	})
   266  }
   267  
   268  func WithCompression(compression Compression) GenericOption {
   269  	return newGenericOption(func(cfg Config) Config {
   270  		cfg.Traces.Compression = compression
   271  		return cfg
   272  	})
   273  }
   274  
   275  func WithURLPath(urlPath string) GenericOption {
   276  	return newGenericOption(func(cfg Config) Config {
   277  		cfg.Traces.URLPath = urlPath
   278  		return cfg
   279  	})
   280  }
   281  
   282  func WithRetry(rc retry.Config) GenericOption {
   283  	return newGenericOption(func(cfg Config) Config {
   284  		cfg.RetryConfig = rc
   285  		return cfg
   286  	})
   287  }
   288  
   289  func WithTLSClientConfig(tlsCfg *tls.Config) GenericOption {
   290  	return newSplitOption(func(cfg Config) Config {
   291  		cfg.Traces.TLSCfg = tlsCfg.Clone()
   292  		return cfg
   293  	}, func(cfg Config) Config {
   294  		cfg.Traces.GRPCCredentials = credentials.NewTLS(tlsCfg)
   295  		return cfg
   296  	})
   297  }
   298  
   299  func WithInsecure() GenericOption {
   300  	return newGenericOption(func(cfg Config) Config {
   301  		cfg.Traces.Insecure = true
   302  		return cfg
   303  	})
   304  }
   305  
   306  func WithSecure() GenericOption {
   307  	return newGenericOption(func(cfg Config) Config {
   308  		cfg.Traces.Insecure = false
   309  		return cfg
   310  	})
   311  }
   312  
   313  func WithHeaders(headers map[string]string) GenericOption {
   314  	return newGenericOption(func(cfg Config) Config {
   315  		cfg.Traces.Headers = headers
   316  		return cfg
   317  	})
   318  }
   319  
   320  func WithTimeout(duration time.Duration) GenericOption {
   321  	return newGenericOption(func(cfg Config) Config {
   322  		cfg.Traces.Timeout = duration
   323  		return cfg
   324  	})
   325  }
   326  

View as plain text