Debian Codegraph @

exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/envconfig.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/otlpmetric/oconf/envconfig.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package oconf // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf"
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/otlpmetric/otlpmetricgrpc/internal/envconfig"
19 "go.opentelemetry.io/otel/internal/global"
20 "go.opentelemetry.io/otel/sdk/metric"
21 )
22
23 // DefaultEnvOptionsReader is the default environments reader.
24 var DefaultEnvOptionsReader = envconfig.EnvOptionsReader{
25 GetEnv: os.Getenv,
26 ReadFile: os.ReadFile,
27 Namespace: "OTEL_EXPORTER_OTLP",
28 }
29
30 // ApplyGRPCEnvConfigs applies the env configurations for gRPC.
31 func ApplyGRPCEnvConfigs(cfg Config) Config {
32 opts := getOptionsFromEnv()
33 for _, opt := range opts {
34 cfg = opt.ApplyGRPCOption(cfg)
35 }
36 return cfg
37 }
38
39 // ApplyHTTPEnvConfigs applies the env configurations for HTTP.
40 func ApplyHTTPEnvConfigs(cfg Config) Config {
41 opts := getOptionsFromEnv()
42 for _, opt := range opts {
43 cfg = opt.ApplyHTTPOption(cfg)
44 }
45 return cfg
46 }
47
48 func getOptionsFromEnv() []GenericOption {
49 opts := []GenericOption{}
50
51 tlsConf := &tls.Config{}
52 DefaultEnvOptionsReader.Apply(
53 envconfig.WithURL("ENDPOINT", func(u *url.URL) {
54 opts = append(opts, withEndpointScheme(u))
55 opts = append(opts, newSplitOption(func(cfg Config) Config {
56 cfg.Metrics.Endpoint = u.Host
57 // For OTLP/HTTP endpoint URLs without a per-signal
58 // configuration, the passed endpoint is used as a base URL
59 // and the signals are sent to these paths relative to that.
60 cfg.Metrics.URLPath = path.Join(u.Path, DefaultMetricsPath)
61 return cfg
62 }, withEndpointForGRPC(u)))
63 }),
64 envconfig.WithURL("METRICS_ENDPOINT", func(u *url.URL) {
65 opts = append(opts, withEndpointScheme(u))
66 opts = append(opts, newSplitOption(func(cfg Config) Config {
67 cfg.Metrics.Endpoint = u.Host
68 // For endpoint URLs for OTLP/HTTP per-signal variables, the
69 // URL MUST be used as-is without any modification. The only
70 // exception is that if an URL contains no path part, the root
71 // path / MUST be used.
72 path := u.Path
73 if path == "" {
74 path = "/"
75 }
76 cfg.Metrics.URLPath = path
77 return cfg
78 }, withEndpointForGRPC(u)))
79 }),
80 envconfig.WithCertPool("CERTIFICATE", func(p *x509.CertPool) { tlsConf.RootCAs = p }),
81 envconfig.WithCertPool("METRICS_CERTIFICATE", func(p *x509.CertPool) { tlsConf.RootCAs = p }),
82 envconfig.WithClientCert(
83 "CLIENT_CERTIFICATE",
84 "CLIENT_KEY",
85 func(c tls.Certificate) { tlsConf.Certificates = []tls.Certificate{c} },
86 ),
87 envconfig.WithClientCert(
88 "METRICS_CLIENT_CERTIFICATE",
89 "METRICS_CLIENT_KEY",
90 func(c tls.Certificate) { tlsConf.Certificates = []tls.Certificate{c} },
91 ),
92 envconfig.WithBool("INSECURE", func(b bool) { opts = append(opts, withInsecure(b)) }),
93 envconfig.WithBool("METRICS_INSECURE", func(b bool) { opts = append(opts, withInsecure(b)) }),
94 withTLSConfig(tlsConf, func(c *tls.Config) { opts = append(opts, WithTLSClientConfig(c)) }),
95 envconfig.WithHeaders("HEADERS", func(h map[string]string) { opts = append(opts, WithHeaders(h)) }),
96 envconfig.WithHeaders("METRICS_HEADERS", func(h map[string]string) { opts = append(opts, WithHeaders(h)) }),
97 WithEnvCompression("COMPRESSION", func(c Compression) { opts = append(opts, WithCompression(c)) }),
98 WithEnvCompression("METRICS_COMPRESSION", func(c Compression) { opts = append(opts, WithCompression(c)) }),
99 envconfig.WithDuration("TIMEOUT", func(d time.Duration) { opts = append(opts, WithTimeout(d)) }),
100 envconfig.WithDuration("METRICS_TIMEOUT", func(d time.Duration) { opts = append(opts, WithTimeout(d)) }),
101 withEnvTemporalityPreference(
102 "METRICS_TEMPORALITY_PREFERENCE",
103 func(t metric.TemporalitySelector) { opts = append(opts, WithTemporalitySelector(t)) },
104 ),
105 withEnvAggPreference(
106 "METRICS_DEFAULT_HISTOGRAM_AGGREGATION",
107 func(a metric.AggregationSelector) { opts = append(opts, WithAggregationSelector(a)) },
108 ),
109 )
110
111 return opts
112 }
113
114 func withEndpointForGRPC(u *url.URL) func(cfg Config) Config {
115 return func(cfg Config) Config {
116 // For OTLP/gRPC endpoints, this is the target to which the
117 // exporter is going to send telemetry.
118 cfg.Metrics.Endpoint = path.Join(u.Host, u.Path)
119 return cfg
120 }
121 }
122
123 // WithEnvCompression retrieves the specified config and passes it to ConfigFn as a Compression.
124 func WithEnvCompression(n string, fn func(Compression)) func(e *envconfig.EnvOptionsReader) {
125 return func(e *envconfig.EnvOptionsReader) {
126 if v, ok := e.GetEnvValue(n); ok {
127 cp := NoCompression
128 if v == "gzip" {
129 cp = GzipCompression
130 }
131
132 fn(cp)
133 }
134 }
135 }
136
137 func withEndpointScheme(u *url.URL) GenericOption {
138 switch strings.ToLower(u.Scheme) {
139 case "http", "unix":
140 return WithInsecure()
141 default:
142 return WithSecure()
143 }
144 }
145
146 // revive:disable-next-line:flag-parameter
147 func withInsecure(b bool) GenericOption {
148 if b {
149 return WithInsecure()
150 }
151 return WithSecure()
152 }
153
154 func withTLSConfig(c *tls.Config, fn func(*tls.Config)) func(e *envconfig.EnvOptionsReader) {
155 return func(e *envconfig.EnvOptionsReader) {
156 if c.RootCAs != nil || len(c.Certificates) > 0 {
157 fn(c)
158 }
159 }
160 }
161
162 func withEnvTemporalityPreference(n string, fn func(metric.TemporalitySelector)) func(e *envconfig.EnvOptionsReader) {
163 return func(e *envconfig.EnvOptionsReader) {
164 if s, ok := e.GetEnvValue(n); ok {
165 switch strings.ToLower(s) {
166 case "cumulative":
167 fn(metric.CumulativeTemporalitySelector)
168 case "delta":
169 fn(metric.DeltaTemporalitySelector)
170 case "lowmemory":
171 fn(metric.LowMemoryTemporalitySelector)
172 default:
173 global.Warn(
174 "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to an invalid value, ignoring.",
175 "value",
176 s,
177 )
178 }
179 }
180 }
181 }
182
183 func withEnvAggPreference(n string, fn func(metric.AggregationSelector)) func(e *envconfig.EnvOptionsReader) {
184 return func(e *envconfig.EnvOptionsReader) {
185 if s, ok := e.GetEnvValue(n); ok {
186 switch strings.ToLower(s) {
187 case "explicit_bucket_histogram":
188 fn(metric.DefaultAggregationSelector)
189 case "base2_exponential_bucket_histogram":
190 fn(func(kind metric.InstrumentKind) metric.Aggregation {
191 if kind == metric.InstrumentKindHistogram {
192 return metric.AggregationBase2ExponentialHistogram{
193 MaxSize: 160,
194 MaxScale: 20,
195 NoMinMax: false,
196 }
197 }
198 return metric.DefaultAggregationSelector(kind)
199 })
200 default:
201 global.Warn(
202 "OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION is set to an invalid value, ignoring.",
203 "value",
204 s,
205 )
206 }
207 }
208 }
209 }