...

Text file src/go.opentelemetry.io/otel/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl

Documentation: go.opentelemetry.io/otel/internal/shared/otlp/otlpmetric/oconf

     1// Code created by gotmpl. DO NOT MODIFY.
     2// source: internal/shared/otlp/otlpmetric/oconf/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
    18package oconf
    19
    20import (
    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	"{{ .retryImportPath }}"
    34	"go.opentelemetry.io/otel/sdk/metric"
    35)
    36
    37const (
    38	// DefaultMaxAttempts describes how many times the driver
    39	// should retry the sending of the payload in case of a
    40	// retryable error.
    41	DefaultMaxAttempts int = 5
    42	// DefaultMetricsPath is a default URL path for endpoint that
    43	// receives metrics.
    44	DefaultMetricsPath string = "/v1/metrics"
    45	// DefaultBackoff is a default base backoff time used in the
    46	// exponential backoff strategy.
    47	DefaultBackoff time.Duration = 300 * time.Millisecond
    48	// DefaultTimeout is a default max waiting time for the backend to process
    49	// each span or metrics batch.
    50	DefaultTimeout time.Duration = 10 * time.Second
    51)
    52
    53type (
    54	SignalConfig struct {
    55		Endpoint    string
    56		Insecure    bool
    57		TLSCfg      *tls.Config
    58		Headers     map[string]string
    59		Compression Compression
    60		Timeout     time.Duration
    61		URLPath     string
    62
    63		// gRPC configurations
    64		GRPCCredentials credentials.TransportCredentials
    65
    66		TemporalitySelector metric.TemporalitySelector
    67		AggregationSelector metric.AggregationSelector
    68	}
    69
    70	Config struct {
    71		// Signal specific configurations
    72		Metrics SignalConfig
    73
    74		RetryConfig retry.Config
    75
    76		// gRPC configurations
    77		ReconnectionPeriod time.Duration
    78		ServiceConfig      string
    79		DialOptions        []grpc.DialOption
    80		GRPCConn           *grpc.ClientConn
    81	}
    82)
    83
    84// NewHTTPConfig returns a new Config with all settings applied from opts and
    85// any unset setting using the default HTTP config values.
    86func NewHTTPConfig(opts ...HTTPOption) Config {
    87	cfg := Config{
    88		Metrics: SignalConfig{
    89			Endpoint:    fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorHTTPPort),
    90			URLPath:     DefaultMetricsPath,
    91			Compression: NoCompression,
    92			Timeout:     DefaultTimeout,
    93
    94			TemporalitySelector: metric.DefaultTemporalitySelector,
    95			AggregationSelector: metric.DefaultAggregationSelector,
    96		},
    97		RetryConfig: retry.DefaultConfig,
    98	}
    99	cfg = ApplyHTTPEnvConfigs(cfg)
   100	for _, opt := range opts {
   101		cfg = opt.ApplyHTTPOption(cfg)
   102	}
   103	cfg.Metrics.URLPath = cleanPath(cfg.Metrics.URLPath, DefaultMetricsPath)
   104	return cfg
   105}
   106
   107// cleanPath returns a path with all spaces trimmed and all redundancies
   108// removed. If urlPath is empty or cleaning it results in an empty string,
   109// defaultPath is returned instead.
   110func cleanPath(urlPath string, defaultPath string) string {
   111	tmp := path.Clean(strings.TrimSpace(urlPath))
   112	if tmp == "." {
   113		return defaultPath
   114	}
   115	if !path.IsAbs(tmp) {
   116		tmp = fmt.Sprintf("/%s", tmp)
   117	}
   118	return tmp
   119}
   120
   121// NewGRPCConfig returns a new Config with all settings applied from opts and
   122// any unset setting using the default gRPC config values.
   123func NewGRPCConfig(opts ...GRPCOption) Config {
   124	cfg := Config{
   125		Metrics: SignalConfig{
   126			Endpoint:    fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorGRPCPort),
   127			URLPath:     DefaultMetricsPath,
   128			Compression: NoCompression,
   129			Timeout:     DefaultTimeout,
   130
   131			TemporalitySelector: metric.DefaultTemporalitySelector,
   132			AggregationSelector: metric.DefaultAggregationSelector,
   133		},
   134		RetryConfig: retry.DefaultConfig,
   135	}
   136	cfg = ApplyGRPCEnvConfigs(cfg)
   137	for _, opt := range opts {
   138		cfg = opt.ApplyGRPCOption(cfg)
   139	}
   140
   141	if cfg.ServiceConfig != "" {
   142		cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultServiceConfig(cfg.ServiceConfig))
   143	}
   144	// Priroritize GRPCCredentials over Insecure (passing both is an error).
   145	if cfg.Metrics.GRPCCredentials != nil {
   146		cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(cfg.Metrics.GRPCCredentials))
   147	} else if cfg.Metrics.Insecure {
   148		cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
   149	} else {
   150		// Default to using the host's root CA.
   151		creds := credentials.NewTLS(nil)
   152		cfg.Metrics.GRPCCredentials = creds
   153		cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(creds))
   154	}
   155	if cfg.Metrics.Compression == GzipCompression {
   156		cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
   157	}
   158	if cfg.ReconnectionPeriod != 0 {
   159		p := grpc.ConnectParams{
   160			Backoff:           backoff.DefaultConfig,
   161			MinConnectTimeout: cfg.ReconnectionPeriod,
   162		}
   163		cfg.DialOptions = append(cfg.DialOptions, grpc.WithConnectParams(p))
   164	}
   165
   166	return cfg
   167}
   168
   169type (
   170	// GenericOption applies an option to the HTTP or gRPC driver.
   171	GenericOption interface {
   172		ApplyHTTPOption(Config) Config
   173		ApplyGRPCOption(Config) Config
   174
   175		// A private method to prevent users implementing the
   176		// interface and so future additions to it will not
   177		// violate compatibility.
   178		private()
   179	}
   180
   181	// HTTPOption applies an option to the HTTP driver.
   182	HTTPOption interface {
   183		ApplyHTTPOption(Config) Config
   184
   185		// A private method to prevent users implementing the
   186		// interface and so future additions to it will not
   187		// violate compatibility.
   188		private()
   189	}
   190
   191	// GRPCOption applies an option to the gRPC driver.
   192	GRPCOption interface {
   193		ApplyGRPCOption(Config) Config
   194
   195		// A private method to prevent users implementing the
   196		// interface and so future additions to it will not
   197		// violate compatibility.
   198		private()
   199	}
   200)
   201
   202// genericOption is an option that applies the same logic
   203// for both gRPC and HTTP.
   204type genericOption struct {
   205	fn func(Config) Config
   206}
   207
   208func (g *genericOption) ApplyGRPCOption(cfg Config) Config {
   209	return g.fn(cfg)
   210}
   211
   212func (g *genericOption) ApplyHTTPOption(cfg Config) Config {
   213	return g.fn(cfg)
   214}
   215
   216func (genericOption) private() {}
   217
   218func newGenericOption(fn func(cfg Config) Config) GenericOption {
   219	return &genericOption{fn: fn}
   220}
   221
   222// splitOption is an option that applies different logics
   223// for gRPC and HTTP.
   224type splitOption struct {
   225	httpFn func(Config) Config
   226	grpcFn func(Config) Config
   227}
   228
   229func (g *splitOption) ApplyGRPCOption(cfg Config) Config {
   230	return g.grpcFn(cfg)
   231}
   232
   233func (g *splitOption) ApplyHTTPOption(cfg Config) Config {
   234	return g.httpFn(cfg)
   235}
   236
   237func (splitOption) private() {}
   238
   239func newSplitOption(httpFn func(cfg Config) Config, grpcFn func(cfg Config) Config) GenericOption {
   240	return &splitOption{httpFn: httpFn, grpcFn: grpcFn}
   241}
   242
   243// httpOption is an option that is only applied to the HTTP driver.
   244type httpOption struct {
   245	fn func(Config) Config
   246}
   247
   248func (h *httpOption) ApplyHTTPOption(cfg Config) Config {
   249	return h.fn(cfg)
   250}
   251
   252func (httpOption) private() {}
   253
   254func NewHTTPOption(fn func(cfg Config) Config) HTTPOption {
   255	return &httpOption{fn: fn}
   256}
   257
   258// grpcOption is an option that is only applied to the gRPC driver.
   259type grpcOption struct {
   260	fn func(Config) Config
   261}
   262
   263func (h *grpcOption) ApplyGRPCOption(cfg Config) Config {
   264	return h.fn(cfg)
   265}
   266
   267func (grpcOption) private() {}
   268
   269func NewGRPCOption(fn func(cfg Config) Config) GRPCOption {
   270	return &grpcOption{fn: fn}
   271}
   272
   273// Generic Options
   274
   275func WithEndpoint(endpoint string) GenericOption {
   276	return newGenericOption(func(cfg Config) Config {
   277		cfg.Metrics.Endpoint = endpoint
   278		return cfg
   279	})
   280}
   281
   282func WithCompression(compression Compression) GenericOption {
   283	return newGenericOption(func(cfg Config) Config {
   284		cfg.Metrics.Compression = compression
   285		return cfg
   286	})
   287}
   288
   289func WithURLPath(urlPath string) GenericOption {
   290	return newGenericOption(func(cfg Config) Config {
   291		cfg.Metrics.URLPath = urlPath
   292		return cfg
   293	})
   294}
   295
   296func WithRetry(rc retry.Config) GenericOption {
   297	return newGenericOption(func(cfg Config) Config {
   298		cfg.RetryConfig = rc
   299		return cfg
   300	})
   301}
   302
   303func WithTLSClientConfig(tlsCfg *tls.Config) GenericOption {
   304	return newSplitOption(func(cfg Config) Config {
   305		cfg.Metrics.TLSCfg = tlsCfg.Clone()
   306		return cfg
   307	}, func(cfg Config) Config {
   308		cfg.Metrics.GRPCCredentials = credentials.NewTLS(tlsCfg)
   309		return cfg
   310	})
   311}
   312
   313func WithInsecure() GenericOption {
   314	return newGenericOption(func(cfg Config) Config {
   315		cfg.Metrics.Insecure = true
   316		return cfg
   317	})
   318}
   319
   320func WithSecure() GenericOption {
   321	return newGenericOption(func(cfg Config) Config {
   322		cfg.Metrics.Insecure = false
   323		return cfg
   324	})
   325}
   326
   327func WithHeaders(headers map[string]string) GenericOption {
   328	return newGenericOption(func(cfg Config) Config {
   329		cfg.Metrics.Headers = headers
   330		return cfg
   331	})
   332}
   333
   334func WithTimeout(duration time.Duration) GenericOption {
   335	return newGenericOption(func(cfg Config) Config {
   336		cfg.Metrics.Timeout = duration
   337		return cfg
   338	})
   339}
   340
   341func WithTemporalitySelector(selector metric.TemporalitySelector) GenericOption {
   342	return newGenericOption(func(cfg Config) Config {
   343		cfg.Metrics.TemporalitySelector = selector
   344		return cfg
   345	})
   346}
   347
   348func WithAggregationSelector(selector metric.AggregationSelector) GenericOption {
   349	return newGenericOption(func(cfg Config) Config {
   350		cfg.Metrics.AggregationSelector = selector
   351		return cfg
   352	})
   353}

View as plain text