...

Text file src/go.opentelemetry.io/otel/internal/shared/otlp/otlptrace/otlpconfig/envconfig.go.tmpl

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

     1// Code created by gotmpl. DO NOT MODIFY.
     2// source: internal/shared/otlp/otlptrace/otlpconfig/envconfig.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 otlpconfig
    19
    20import (
    21	"crypto/tls"
    22	"crypto/x509"
    23	"net/url"
    24	"os"
    25	"path"
    26	"strings"
    27	"time"
    28
    29	"{{ .envconfigImportPath }}"
    30)
    31
    32// DefaultEnvOptionsReader is the default environments reader.
    33var DefaultEnvOptionsReader = envconfig.EnvOptionsReader{
    34	GetEnv:    os.Getenv,
    35	ReadFile:  os.ReadFile,
    36	Namespace: "OTEL_EXPORTER_OTLP",
    37}
    38
    39// ApplyGRPCEnvConfigs applies the env configurations for gRPC.
    40func ApplyGRPCEnvConfigs(cfg Config) Config {
    41	opts := getOptionsFromEnv()
    42	for _, opt := range opts {
    43		cfg = opt.ApplyGRPCOption(cfg)
    44	}
    45	return cfg
    46}
    47
    48// ApplyHTTPEnvConfigs applies the env configurations for HTTP.
    49func ApplyHTTPEnvConfigs(cfg Config) Config {
    50	opts := getOptionsFromEnv()
    51	for _, opt := range opts {
    52		cfg = opt.ApplyHTTPOption(cfg)
    53	}
    54	return cfg
    55}
    56
    57func getOptionsFromEnv() []GenericOption {
    58	opts := []GenericOption{}
    59
    60	tlsConf := &tls.Config{}
    61	DefaultEnvOptionsReader.Apply(
    62		envconfig.WithURL("ENDPOINT", func(u *url.URL) {
    63			opts = append(opts, withEndpointScheme(u))
    64			opts = append(opts, newSplitOption(func(cfg Config) Config {
    65				cfg.Traces.Endpoint = u.Host
    66				// For OTLP/HTTP endpoint URLs without a per-signal
    67				// configuration, the passed endpoint is used as a base URL
    68				// and the signals are sent to these paths relative to that.
    69				cfg.Traces.URLPath = path.Join(u.Path, DefaultTracesPath)
    70				return cfg
    71			}, withEndpointForGRPC(u)))
    72		}),
    73		envconfig.WithURL("TRACES_ENDPOINT", func(u *url.URL) {
    74			opts = append(opts, withEndpointScheme(u))
    75			opts = append(opts, newSplitOption(func(cfg Config) Config {
    76				cfg.Traces.Endpoint = u.Host
    77				// For endpoint URLs for OTLP/HTTP per-signal variables, the
    78				// URL MUST be used as-is without any modification. The only
    79				// exception is that if an URL contains no path part, the root
    80				// path / MUST be used.
    81				path := u.Path
    82				if path == "" {
    83					path = "/"
    84				}
    85				cfg.Traces.URLPath = path
    86				return cfg
    87			}, withEndpointForGRPC(u)))
    88		}),
    89		envconfig.WithCertPool("CERTIFICATE", func(p *x509.CertPool) { tlsConf.RootCAs = p }),
    90		envconfig.WithCertPool("TRACES_CERTIFICATE", func(p *x509.CertPool) { tlsConf.RootCAs = p }),
    91		envconfig.WithClientCert("CLIENT_CERTIFICATE", "CLIENT_KEY", func(c tls.Certificate) { tlsConf.Certificates = []tls.Certificate{c} }),
    92		envconfig.WithClientCert("TRACES_CLIENT_CERTIFICATE", "TRACES_CLIENT_KEY", func(c tls.Certificate) { tlsConf.Certificates = []tls.Certificate{c} }),
    93		withTLSConfig(tlsConf, func(c *tls.Config) { opts = append(opts, WithTLSClientConfig(c)) }),
    94		envconfig.WithBool("INSECURE", func(b bool) { opts = append(opts, withInsecure(b)) }),
    95		envconfig.WithBool("TRACES_INSECURE", func(b bool) { opts = append(opts, withInsecure(b)) }),
    96		envconfig.WithHeaders("HEADERS", func(h map[string]string) { opts = append(opts, WithHeaders(h)) }),
    97		envconfig.WithHeaders("TRACES_HEADERS", func(h map[string]string) { opts = append(opts, WithHeaders(h)) }),
    98		WithEnvCompression("COMPRESSION", func(c Compression) { opts = append(opts, WithCompression(c)) }),
    99		WithEnvCompression("TRACES_COMPRESSION", func(c Compression) { opts = append(opts, WithCompression(c)) }),
   100		envconfig.WithDuration("TIMEOUT", func(d time.Duration) { opts = append(opts, WithTimeout(d)) }),
   101		envconfig.WithDuration("TRACES_TIMEOUT", func(d time.Duration) { opts = append(opts, WithTimeout(d)) }),
   102	)
   103
   104	return opts
   105}
   106
   107func withEndpointScheme(u *url.URL) GenericOption {
   108	switch strings.ToLower(u.Scheme) {
   109	case "http", "unix":
   110		return WithInsecure()
   111	default:
   112		return WithSecure()
   113	}
   114}
   115
   116func withEndpointForGRPC(u *url.URL) func(cfg Config) Config {
   117	return func(cfg Config) Config {
   118		// For OTLP/gRPC endpoints, this is the target to which the
   119		// exporter is going to send telemetry.
   120		cfg.Traces.Endpoint = path.Join(u.Host, u.Path)
   121		return cfg
   122	}
   123}
   124
   125// WithEnvCompression retrieves the specified config and passes it to ConfigFn as a Compression.
   126func WithEnvCompression(n string, fn func(Compression)) func(e *envconfig.EnvOptionsReader) {
   127	return func(e *envconfig.EnvOptionsReader) {
   128		if v, ok := e.GetEnvValue(n); ok {
   129			cp := NoCompression
   130			if v == "gzip" {
   131				cp = GzipCompression
   132			}
   133
   134			fn(cp)
   135		}
   136	}
   137}
   138
   139// revive:disable-next-line:flag-parameter
   140func withInsecure(b bool) GenericOption {
   141	if b {
   142		return WithInsecure()
   143	}
   144	return WithSecure()
   145}
   146
   147func withTLSConfig(c *tls.Config, fn func(*tls.Config)) func(e *envconfig.EnvOptionsReader) {
   148	return func(e *envconfig.EnvOptionsReader) {
   149		if c.RootCAs != nil || len(c.Certificates) > 0 {
   150			fn(c)
   151		}
   152	}
   153}

View as plain text