Debian Codegraph @

exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 // Package otlpconfig provides configuration for the otlptrace exporters.
8 package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig"
9
10 import (
11 "crypto/tls"
12 "fmt"
13 "net/http"
14 "net/url"
15 "path"
16 "strings"
17 "time"
18
19 "google.golang.org/grpc"
20 "google.golang.org/grpc/backoff"
21 "google.golang.org/grpc/credentials"
22 "google.golang.org/grpc/credentials/insecure"
23 "google.golang.org/grpc/encoding/gzip"
24
25 "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
26 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/retry"
27 "go.opentelemetry.io/otel/internal/global"
28 )
29
30 const (
31 // DefaultTracesPath is a default URL path for endpoint that
32 // receives spans.
33 DefaultTracesPath string = "/v1/traces"
34 // DefaultTimeout is a default max waiting time for the backend to process
35 // each span batch.
36 DefaultTimeout time.Duration = 10 * time.Second
37 )
38
39 type (
40 // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request.
41 // This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client.
42 HTTPTransportProxyFunc func(*http.Request) (*url.URL, error)
43
44 SignalConfig struct {
45 Endpoint string
46 Insecure bool
47 TLSCfg *tls.Config
48 Headers map[string]string
49 Compression Compression
50 Timeout time.Duration
51 URLPath string
52
53 // gRPC configurations
54 GRPCCredentials credentials.TransportCredentials
55
56 // HTTP configurations
57 Proxy HTTPTransportProxyFunc
58 HTTPClient *http.Client
59 }
60
61 Config struct {
62 // Signal specific configurations
63 Traces SignalConfig
64
65 RetryConfig retry.Config
66
67 // gRPC configurations
68 ReconnectionPeriod time.Duration
69 ServiceConfig string
70 DialOptions []grpc.DialOption
71 GRPCConn *grpc.ClientConn
72 }
73 )
74
75 // NewHTTPConfig returns a new Config with all settings applied from opts and
76 // any unset setting using the default HTTP config values.
77 func NewHTTPConfig(opts ...HTTPOption) Config {
78 cfg := Config{
79 Traces: SignalConfig{
80 Endpoint: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorHTTPPort),
81 URLPath: DefaultTracesPath,
82 Compression: NoCompression,
83 Timeout: DefaultTimeout,
84 },
85 RetryConfig: retry.DefaultConfig,
86 }
87 cfg = ApplyHTTPEnvConfigs(cfg)
88 for _, opt := range opts {
89 cfg = opt.ApplyHTTPOption(cfg)
90 }
91 cfg.Traces.URLPath = cleanPath(cfg.Traces.URLPath, DefaultTracesPath)
92 return cfg
93 }
94
95 // cleanPath returns a path with all spaces trimmed. If urlPath is empty,
96 // defaultPath is returned instead.
97 func cleanPath(urlPath string, defaultPath string) string {
98 tmp := strings.TrimSpace(urlPath)
99 if tmp == "" || tmp == "." {
100 return defaultPath
101 }
102 if !path.IsAbs(tmp) {
103 tmp = "/" + tmp
104 }
105 return tmp
106 }
107
108 // NewGRPCConfig returns a new Config with all settings applied from opts and
109 // any unset setting using the default gRPC config values.
110 func NewGRPCConfig(opts ...GRPCOption) Config {
111 userAgent := "OTel OTLP Exporter Go/" + otlptrace.Version()
112 cfg := Config{
113 Traces: SignalConfig{
114 Endpoint: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorGRPCPort),
115 URLPath: DefaultTracesPath,
116 Compression: NoCompression,
117 Timeout: DefaultTimeout,
118 },
119 RetryConfig: retry.DefaultConfig,
120 DialOptions: []grpc.DialOption{grpc.WithUserAgent(userAgent)},
121 }
122 cfg = ApplyGRPCEnvConfigs(cfg)
123 for _, opt := range opts {
124 cfg = opt.ApplyGRPCOption(cfg)
125 }
126
127 if cfg.ServiceConfig != "" {
128 cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultServiceConfig(cfg.ServiceConfig))
129 }
130 // Prioritize GRPCCredentials over Insecure (passing both is an error).
131 if cfg.Traces.GRPCCredentials != nil {
132 cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(cfg.Traces.GRPCCredentials))
133 } else if cfg.Traces.Insecure {
134 cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
135 } else {
136 // Default to using the host's root CA.
137 creds := credentials.NewTLS(nil)
138 cfg.Traces.GRPCCredentials = creds
139 cfg.DialOptions = append(cfg.DialOptions, grpc.WithTransportCredentials(creds))
140 }
141 if cfg.Traces.Compression == GzipCompression {
142 cfg.DialOptions = append(cfg.DialOptions, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
143 }
144 if cfg.ReconnectionPeriod != 0 {
145 p := grpc.ConnectParams{
146 Backoff: backoff.DefaultConfig,
147 MinConnectTimeout: cfg.ReconnectionPeriod,
148 }
149 cfg.DialOptions = append(cfg.DialOptions, grpc.WithConnectParams(p))
150 }
151
152 return cfg
153 }
154
155 type (
156 // GenericOption applies an option to the HTTP or gRPC driver.
157 GenericOption interface {
158 ApplyHTTPOption(Config) Config
159 ApplyGRPCOption(Config) Config
160
161 // A private method to prevent users implementing the
162 // interface and so future additions to it will not
163 // violate compatibility.
164 private()
165 }
166
167 // HTTPOption applies an option to the HTTP driver.
168 HTTPOption interface {
169 ApplyHTTPOption(Config) Config
170
171 // A private method to prevent users implementing the
172 // interface and so future additions to it will not
173 // violate compatibility.
174 private()
175 }
176
177 // GRPCOption applies an option to the gRPC driver.
178 GRPCOption interface {
179 ApplyGRPCOption(Config) Config
180
181 // A private method to prevent users implementing the
182 // interface and so future additions to it will not
183 // violate compatibility.
184 private()
185 }
186 )
187
188 // genericOption is an option that applies the same logic
189 // for both gRPC and HTTP.
190 type genericOption struct {
191 fn func(Config) Config
192 }
193
194 func (g *genericOption) ApplyGRPCOption(cfg Config) Config {
195 return g.fn(cfg)
196 }
197
198 func (g *genericOption) ApplyHTTPOption(cfg Config) Config {
199 return g.fn(cfg)
200 }
201
202 func (genericOption) private() {}
203
204 func newGenericOption(fn func(cfg Config) Config) GenericOption {
205 return &genericOption{fn: fn}
206 }
207
208 // splitOption is an option that applies different logics
209 // for gRPC and HTTP.
210 type splitOption struct {
211 httpFn func(Config) Config
212 grpcFn func(Config) Config
213 }
214
215 func (g *splitOption) ApplyGRPCOption(cfg Config) Config {
216 return g.grpcFn(cfg)
217 }
218
219 func (g *splitOption) ApplyHTTPOption(cfg Config) Config {
220 return g.httpFn(cfg)
221 }
222
223 func (splitOption) private() {}
224
225 func newSplitOption(httpFn func(cfg Config) Config, grpcFn func(cfg Config) Config) GenericOption {
226 return &splitOption{httpFn: httpFn, grpcFn: grpcFn}
227 }
228
229 // httpOption is an option that is only applied to the HTTP driver.
230 type httpOption struct {
231 fn func(Config) Config
232 }
233
234 func (h *httpOption) ApplyHTTPOption(cfg Config) Config {
235 return h.fn(cfg)
236 }
237
238 func (httpOption) private() {}
239
240 func NewHTTPOption(fn func(cfg Config) Config) HTTPOption {
241 return &httpOption{fn: fn}
242 }
243
244 // grpcOption is an option that is only applied to the gRPC driver.
245 type grpcOption struct {
246 fn func(Config) Config
247 }
248
249 func (h *grpcOption) ApplyGRPCOption(cfg Config) Config {
250 return h.fn(cfg)
251 }
252
253 func (grpcOption) private() {}
254
255 func NewGRPCOption(fn func(cfg Config) Config) GRPCOption {
256 return &grpcOption{fn: fn}
257 }
258
259 // Generic Options
260
261 // WithEndpoint configures the trace host and port only; endpoint should
262 // resemble "example.com" or "localhost:4317". To configure the scheme and path,
263 // use WithEndpointURL.
264 func WithEndpoint(endpoint string) GenericOption {
265 return newGenericOption(func(cfg Config) Config {
266 cfg.Traces.Endpoint = endpoint
267 return cfg
268 })
269 }
270
271 // WithEndpointURL configures the trace scheme, host, port, and path; the
272 // provided value should resemble "https://example.com:4318/v1/traces".
273 func WithEndpointURL(v string) GenericOption {
274 return newGenericOption(func(cfg Config) Config {
275 u, err := url.Parse(v)
276 if err != nil {
277 global.Error(err, "otlptrace: parse endpoint url", "url", v)
278 return cfg
279 }
280
281 cfg.Traces.Endpoint = u.Host
282 cfg.Traces.URLPath = u.Path
283 cfg.Traces.Insecure = u.Scheme != "https"
284
285 return cfg
286 })
287 }
288
289 func WithCompression(compression Compression) GenericOption {
290 return newGenericOption(func(cfg Config) Config {
291 cfg.Traces.Compression = compression
292 return cfg
293 })
294 }
295
296 func WithURLPath(urlPath string) GenericOption {
297 return newGenericOption(func(cfg Config) Config {
298 cfg.Traces.URLPath = urlPath
299 return cfg
300 })
301 }
302
303 func WithRetry(rc retry.Config) GenericOption {
304 return newGenericOption(func(cfg Config) Config {
305 cfg.RetryConfig = rc
306 return cfg
307 })
308 }
309
310 func WithTLSClientConfig(tlsCfg *tls.Config) GenericOption {
311 return newSplitOption(func(cfg Config) Config {
312 cfg.Traces.TLSCfg = tlsCfg.Clone()
313 return cfg
314 }, func(cfg Config) Config {
315 cfg.Traces.GRPCCredentials = credentials.NewTLS(tlsCfg)
316 return cfg
317 })
318 }
319
320 func WithInsecure() GenericOption {
321 return newGenericOption(func(cfg Config) Config {
322 cfg.Traces.Insecure = true
323 return cfg
324 })
325 }
326
327 func WithSecure() GenericOption {
328 return newGenericOption(func(cfg Config) Config {
329 cfg.Traces.Insecure = false
330 return cfg
331 })
332 }
333
334 func WithHeaders(headers map[string]string) GenericOption {
335 return newGenericOption(func(cfg Config) Config {
336 cfg.Traces.Headers = headers
337 return cfg
338 })
339 }
340
341 func WithTimeout(duration time.Duration) GenericOption {
342 return newGenericOption(func(cfg Config) Config {
343 cfg.Traces.Timeout = duration
344 return cfg
345 })
346 }
347
348 func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
349 return newGenericOption(func(cfg Config) Config {
350 cfg.Traces.Proxy = pf
351 return cfg
352 })
353 }
354
355 func WithHTTPClient(c *http.Client) GenericOption {
356 return newGenericOption(func(cfg Config) Config {
357 cfg.Traces.HTTPClient = c
358 return cfg
359 })
360 }