// Code generated by gotmpl. DO NOT MODIFY.
// source: internal/shared/otlp/envconfig/envconfig.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package envconfig provides functionality to parse configuration from // environment variables.
package envconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/envconfig" import ( ) // ConfigFn is the generic function used to set a config. type ConfigFn func(*EnvOptionsReader) // EnvOptionsReader reads the required environment variables. type EnvOptionsReader struct { GetEnv func(string) string ReadFile func(string) ([]byte, error) Namespace string } // Apply runs every ConfigFn. func ( *EnvOptionsReader) ( ...ConfigFn) { for , := range { () } } // GetEnvValue gets an OTLP environment variable value of the specified key // using the GetEnv function. // This function prepends the OTLP specified namespace to all key lookups. func ( *EnvOptionsReader) ( string) (string, bool) { := strings.TrimSpace(.GetEnv(keyWithNamespace(.Namespace, ))) return , != "" } // WithString retrieves the specified config and passes it to ConfigFn as a string. func ( string, func(string)) func( *EnvOptionsReader) { return func( *EnvOptionsReader) { if , := .GetEnvValue(); { () } } } // WithBool returns a ConfigFn that reads the environment variable n and if it exists passes its parsed bool value to fn. func ( string, func(bool)) ConfigFn { return func( *EnvOptionsReader) { if , := .GetEnvValue(); { := strings.ToLower() == "true" () } } } // WithDuration retrieves the specified config and passes it to ConfigFn as a duration. func ( string, func(time.Duration)) func( *EnvOptionsReader) { return func( *EnvOptionsReader) { if , := .GetEnvValue(); { , := strconv.Atoi() if != nil { global.Error(, "parse duration", "input", ) return } (time.Duration() * time.Millisecond) } } } // WithHeaders retrieves the specified config and passes it to ConfigFn as a map of HTTP headers. func ( string, func(map[string]string)) func( *EnvOptionsReader) { return func( *EnvOptionsReader) { if , := .GetEnvValue(); { (stringToHeader()) } } } // WithURL retrieves the specified config and passes it to ConfigFn as a net/url.URL. func ( string, func(*url.URL)) func( *EnvOptionsReader) { return func( *EnvOptionsReader) { if , := .GetEnvValue(); { , := url.Parse() if != nil { global.Error(, "parse url", "input", ) return } () } } } // WithCertPool returns a ConfigFn that reads the environment variable n as a filepath to a TLS certificate pool. If it exists, it is parsed as a crypto/x509.CertPool and it is passed to fn. func ( string, func(*x509.CertPool)) ConfigFn { return func( *EnvOptionsReader) { if , := .GetEnvValue(); { , := .ReadFile() if != nil { global.Error(, "read tls ca cert file", "file", ) return } , := createCertPool() if != nil { global.Error(, "create tls cert pool") return } () } } } // WithClientCert returns a ConfigFn that reads the environment variable nc and nk as filepaths to a client certificate and key pair. If they exists, they are parsed as a crypto/tls.Certificate and it is passed to fn. func (, string, func(tls.Certificate)) ConfigFn { return func( *EnvOptionsReader) { , := .GetEnvValue() , := .GetEnvValue() if ! || ! { return } , := .ReadFile() if != nil { global.Error(, "read tls client cert", "file", ) return } , := .ReadFile() if != nil { global.Error(, "read tls client key", "file", ) return } , := tls.X509KeyPair(, ) if != nil { global.Error(, "create tls client key pair") return } () } } func keyWithNamespace(, string) string { if == "" { return } return fmt.Sprintf("%s_%s", , ) } func stringToHeader( string) map[string]string { := strings.Split(, ",") := make(map[string]string) for , := range { , , := strings.Cut(, "=") if ! { global.Error(errors.New("missing '="), "parse headers", "input", ) continue } := strings.TrimSpace() // Validate the key. if !isValidHeaderKey() { global.Error(errors.New("invalid header key"), "parse headers", "key", ) continue } // Only decode the value. , := url.PathUnescape() if != nil { global.Error(, "escape header value", "value", ) continue } := strings.TrimSpace() [] = } return } func createCertPool( []byte) (*x509.CertPool, error) { := x509.NewCertPool() if := .AppendCertsFromPEM(); ! { return nil, errors.New("failed to append certificate to the cert pool") } return , nil } func isValidHeaderKey( string) bool { if == "" { return false } for , := range { if !isTokenChar() { return false } } return true } func isTokenChar( rune) bool { return <= unicode.MaxASCII && (unicode.IsLetter() || unicode.IsDigit() || == '!' || == '#' || == '$' || == '%' || == '&' || == '\'' || == '*' || == '+' || == '-' || == '.' || == '^' || == '_' || == '`' || == '|' || == '~') }