1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package otlpconfig
19
20 import (
21 "crypto/tls"
22 "crypto/x509"
23 "net/url"
24 "os"
25 "path"
26 "strings"
27 "time"
28
29 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/envconfig"
30 )
31
32
33 var DefaultEnvOptionsReader = envconfig.EnvOptionsReader{
34 GetEnv: os.Getenv,
35 ReadFile: os.ReadFile,
36 Namespace: "OTEL_EXPORTER_OTLP",
37 }
38
39
40 func ApplyGRPCEnvConfigs(cfg Config) Config {
41 opts := getOptionsFromEnv()
42 for _, opt := range opts {
43 cfg = opt.ApplyGRPCOption(cfg)
44 }
45 return cfg
46 }
47
48
49 func ApplyHTTPEnvConfigs(cfg Config) Config {
50 opts := getOptionsFromEnv()
51 for _, opt := range opts {
52 cfg = opt.ApplyHTTPOption(cfg)
53 }
54 return cfg
55 }
56
57 func 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
67
68
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
78
79
80
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
107 func 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
116 func withEndpointForGRPC(u *url.URL) func(cfg Config) Config {
117 return func(cfg Config) Config {
118
119
120 cfg.Traces.Endpoint = path.Join(u.Host, u.Path)
121 return cfg
122 }
123 }
124
125
126 func 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
140 func withInsecure(b bool) GenericOption {
141 if b {
142 return WithInsecure()
143 }
144 return WithSecure()
145 }
146
147 func 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 }
154
View as plain text