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