Debian Codegraph @

exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package otlpconfig
8
9 import (
10 "errors"
11 "net/http"
12 "net/url"
13 "testing"
14 "time"
15
16 "github.com/stretchr/testify/assert"
17
18 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/envconfig"
19 )
20
21 const (
22 WeakCertificate = `
23 -----BEGIN CERTIFICATE-----
24 MIIBhzCCASygAwIBAgIRANHpHgAWeTnLZpTSxCKs0ggwCgYIKoZIzj0EAwIwEjEQ
25 MA4GA1UEChMHb3RlbC1nbzAeFw0yMTA0MDExMzU5MDNaFw0yMTA0MDExNDU5MDNa
26 MBIxEDAOBgNVBAoTB290ZWwtZ28wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAS9
27 nWSkmPCxShxnp43F+PrOtbGV7sNfkbQ/kxzi9Ego0ZJdiXxkmv/C05QFddCW7Y0Z
28 sJCLHGogQsYnWJBXUZOVo2MwYTAOBgNVHQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYI
29 KwYBBQUHAwEwDAYDVR0TAQH/BAIwADAsBgNVHREEJTAjgglsb2NhbGhvc3SHEAAA
30 AAAAAAAAAAAAAAAAAAGHBH8AAAEwCgYIKoZIzj0EAwIDSQAwRgIhANwZVVKvfvQ/
31 1HXsTvgH+xTQswOwSSKYJ1cVHQhqK7ZbAiEAus8NxpTRnp5DiTMuyVmhVNPB+bVH
32 Lhnm4N/QDk5rek0=
33 -----END CERTIFICATE-----
34 `
35 WeakPrivateKey = `
36 -----BEGIN PRIVATE KEY-----
37 MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgN8HEXiXhvByrJ1zK
38 SFT6Y2l2KqDWwWzKf+t4CyWrNKehRANCAAS9nWSkmPCxShxnp43F+PrOtbGV7sNf
39 kbQ/kxzi9Ego0ZJdiXxkmv/C05QFddCW7Y0ZsJCLHGogQsYnWJBXUZOV
40 -----END PRIVATE KEY-----
41 `
42 )
43
44 type env map[string]string
45
46 func (e *env) getEnv(env string) string {
47 return (*e)[env]
48 }
49
50 type fileReader map[string][]byte
51
52 func (f *fileReader) readFile(filename string) ([]byte, error) {
53 if b, ok := (*f)[filename]; ok {
54 return b, nil
55 }
56 return nil, errors.New("file not found")
57 }
58
59 func TestConfigs(t *testing.T) {
60 tlsCert, err := CreateTLSConfig([]byte(WeakCertificate))
61 assert.NoError(t, err)
62
63 tests := []struct {
64 name string
65 opts []GenericOption
66 env env
67 fileReader fileReader
68 asserts func(t *testing.T, c *Config, grpcOption bool)
69 }{
70 {
71 name: "Test default configs",
72 asserts: func(t *testing.T, c *Config, grpcOption bool) {
73 if grpcOption {
74 assert.Equal(t, "localhost:4317", c.Traces.Endpoint)
75 } else {
76 assert.Equal(t, "localhost:4318", c.Traces.Endpoint)
77 }
78 assert.Equal(t, NoCompression, c.Traces.Compression)
79 assert.Equal(t, map[string]string(nil), c.Traces.Headers)
80 assert.Equal(t, 10*time.Second, c.Traces.Timeout)
81 },
82 },
83
84 // Endpoint Tests
85 {
86 name: "Test With Endpoint",
87 opts: []GenericOption{
88 WithEndpoint("someendpoint"),
89 },
90 asserts: func(t *testing.T, c *Config, grpcOption bool) {
91 assert.Equal(t, "someendpoint", c.Traces.Endpoint)
92 },
93 },
94 {
95 name: "Test With Endpoint URL",
96 opts: []GenericOption{
97 WithEndpointURL("http://someendpoint/somepath"),
98 },
99 asserts: func(t *testing.T, c *Config, grpcOption bool) {
100 assert.Equal(t, "someendpoint", c.Traces.Endpoint)
101 assert.Equal(t, "/somepath", c.Traces.URLPath)
102 assert.True(t, c.Traces.Insecure)
103 },
104 },
105 {
106 name: "Test With Secure Endpoint URL",
107 opts: []GenericOption{
108 WithEndpointURL("https://someendpoint/somepath"),
109 },
110 asserts: func(t *testing.T, c *Config, grpcOption bool) {
111 assert.Equal(t, "someendpoint", c.Traces.Endpoint)
112 assert.Equal(t, "/somepath", c.Traces.URLPath)
113 assert.False(t, c.Traces.Insecure)
114 },
115 },
116 {
117 name: "Test With Invalid Endpoint URL",
118 opts: []GenericOption{
119 WithEndpointURL("%invalid"),
120 },
121 asserts: func(t *testing.T, c *Config, grpcOption bool) {
122 if grpcOption {
123 assert.Equal(t, "localhost:4317", c.Traces.Endpoint)
124 } else {
125 assert.Equal(t, "localhost:4318", c.Traces.Endpoint)
126 }
127 assert.Equal(t, "/v1/traces", c.Traces.URLPath)
128 },
129 },
130 {
131 name: "Test With Endpoint last used",
132 opts: []GenericOption{
133 WithEndpointURL("https://someendpoint/somepath"),
134 WithEndpoint("someendpoint2"),
135 },
136 asserts: func(t *testing.T, c *Config, grpcOption bool) {
137 assert.Equal(t, "someendpoint2", c.Traces.Endpoint)
138 },
139 },
140 {
141 name: "Test With WithEndpointURL last used",
142 opts: []GenericOption{
143 WithEndpoint("someendpoint2"),
144 WithEndpointURL("https://someendpoint/somepath"),
145 },
146 asserts: func(t *testing.T, c *Config, grpcOption bool) {
147 assert.Equal(t, "someendpoint", c.Traces.Endpoint)
148 },
149 },
150 {
151 name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure",
152 env: map[string]string{
153 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix",
154 },
155 opts: []GenericOption{
156 WithEndpointURL("https://someendpoint/somepath"),
157 },
158 asserts: func(t *testing.T, c *Config, grpcOption bool) {
159 assert.Equal(t, "someendpoint", c.Traces.Endpoint)
160 assert.Equal(t, "/somepath", c.Traces.URLPath)
161 assert.False(t, c.Traces.Insecure)
162 },
163 },
164 {
165 name: "Test Environment Endpoint",
166 env: map[string]string{
167 "OTEL_EXPORTER_OTLP_ENDPOINT": "https://env.endpoint/prefix",
168 },
169 asserts: func(t *testing.T, c *Config, grpcOption bool) {
170 assert.False(t, c.Traces.Insecure)
171 if grpcOption {
172 assert.Equal(t, "env.endpoint/prefix", c.Traces.Endpoint)
173 } else {
174 assert.Equal(t, "env.endpoint", c.Traces.Endpoint)
175 assert.Equal(t, "/prefix/v1/traces", c.Traces.URLPath)
176 }
177 },
178 },
179 {
180 name: "Test Environment Signal Specific Endpoint",
181 env: map[string]string{
182 "OTEL_EXPORTER_OTLP_ENDPOINT": "https://overrode.by.signal.specific/env/var",
183 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "http://env.traces.endpoint",
184 },
185 asserts: func(t *testing.T, c *Config, grpcOption bool) {
186 assert.True(t, c.Traces.Insecure)
187 assert.Equal(t, "env.traces.endpoint", c.Traces.Endpoint)
188 if !grpcOption {
189 assert.Equal(t, "/", c.Traces.URLPath)
190 }
191 },
192 },
193 {
194 name: "Test Mixed Environment and With Endpoint",
195 opts: []GenericOption{
196 WithEndpointURL("https://traces_endpoint2/somepath"),
197 WithEndpoint("traces_endpoint"),
198 },
199 env: map[string]string{
200 "OTEL_EXPORTER_OTLP_ENDPOINT": "env_endpoint",
201 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "env_endpoint2",
202 },
203 asserts: func(t *testing.T, c *Config, grpcOption bool) {
204 assert.Equal(t, "traces_endpoint", c.Traces.Endpoint)
205 },
206 },
207 {
208 name: "Test Mixed Environment and With Endpoint",
209 opts: []GenericOption{
210 WithEndpoint("traces_endpoint"),
211 WithEndpointURL("https://traces_endpoint2/somepath"),
212 },
213 env: map[string]string{
214 "OTEL_EXPORTER_OTLP_ENDPOINT": "env_endpoint",
215 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "env_endpoint2",
216 },
217 asserts: func(t *testing.T, c *Config, grpcOption bool) {
218 assert.Equal(t, "traces_endpoint2", c.Traces.Endpoint)
219 },
220 },
221 {
222 name: "Test Environment Endpoint with HTTP scheme",
223 env: map[string]string{
224 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://env_endpoint",
225 },
226 asserts: func(t *testing.T, c *Config, grpcOption bool) {
227 assert.Equal(t, "env_endpoint", c.Traces.Endpoint)
228 assert.True(t, c.Traces.Insecure)
229 },
230 },
231 {
232 name: "Test Environment Endpoint with HTTP scheme and leading & trailingspaces",
233 env: map[string]string{
234 "OTEL_EXPORTER_OTLP_ENDPOINT": " http://env_endpoint ",
235 },
236 asserts: func(t *testing.T, c *Config, grpcOption bool) {
237 assert.Equal(t, "env_endpoint", c.Traces.Endpoint)
238 assert.True(t, c.Traces.Insecure)
239 },
240 },
241 {
242 name: "Test Environment Endpoint with HTTPS scheme",
243 env: map[string]string{
244 "OTEL_EXPORTER_OTLP_ENDPOINT": "https://env_endpoint",
245 },
246 asserts: func(t *testing.T, c *Config, grpcOption bool) {
247 assert.Equal(t, "env_endpoint", c.Traces.Endpoint)
248 assert.False(t, c.Traces.Insecure)
249 },
250 },
251 {
252 name: "Test Environment Signal Specific Endpoint with uppercase scheme",
253 env: map[string]string{
254 "OTEL_EXPORTER_OTLP_ENDPOINT": "HTTPS://overrode_by_signal_specific",
255 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "HtTp://env_traces_endpoint",
256 },
257 asserts: func(t *testing.T, c *Config, grpcOption bool) {
258 assert.Equal(t, "env_traces_endpoint", c.Traces.Endpoint)
259 assert.True(t, c.Traces.Insecure)
260 },
261 },
262
263 // Certificate tests
264 {
265 name: "Test Default Certificate",
266 asserts: func(t *testing.T, c *Config, grpcOption bool) {
267 if grpcOption {
268 assert.NotNil(t, c.Traces.GRPCCredentials)
269 } else {
270 assert.Nil(t, c.Traces.TLSCfg)
271 }
272 },
273 },
274 {
275 name: "Test With Certificate",
276 opts: []GenericOption{
277 WithTLSClientConfig(tlsCert),
278 },
279 asserts: func(t *testing.T, c *Config, grpcOption bool) {
280 if grpcOption {
281 // TODO: make sure gRPC's credentials actually works
282 assert.NotNil(t, c.Traces.GRPCCredentials)
283 } else {
284 // nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
285 assert.Equal(t, tlsCert.RootCAs.Subjects(), c.Traces.TLSCfg.RootCAs.Subjects())
286 }
287 },
288 },
289 {
290 name: "Test Environment Certificate",
291 env: map[string]string{
292 "OTEL_EXPORTER_OTLP_CERTIFICATE": "cert_path",
293 },
294 fileReader: fileReader{
295 "cert_path": []byte(WeakCertificate),
296 },
297 asserts: func(t *testing.T, c *Config, grpcOption bool) {
298 if grpcOption {
299 assert.NotNil(t, c.Traces.GRPCCredentials)
300 } else {
301 // nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
302 assert.Equal(t, tlsCert.RootCAs.Subjects(), c.Traces.TLSCfg.RootCAs.Subjects())
303 }
304 },
305 },
306 {
307 name: "Test Environment Signal Specific Certificate",
308 env: map[string]string{
309 "OTEL_EXPORTER_OTLP_CERTIFICATE": "overrode_by_signal_specific",
310 "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE": "cert_path",
311 },
312 fileReader: fileReader{
313 "cert_path": []byte(WeakCertificate),
314 "invalid_cert": []byte("invalid certificate file."),
315 },
316 asserts: func(t *testing.T, c *Config, grpcOption bool) {
317 if grpcOption {
318 assert.NotNil(t, c.Traces.GRPCCredentials)
319 } else {
320 // nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
321 assert.Equal(t, tlsCert.RootCAs.Subjects(), c.Traces.TLSCfg.RootCAs.Subjects())
322 }
323 },
324 },
325 {
326 name: "Test Mixed Environment and With Certificate",
327 opts: []GenericOption{},
328 env: map[string]string{
329 "OTEL_EXPORTER_OTLP_CERTIFICATE": "cert_path",
330 },
331 fileReader: fileReader{
332 "cert_path": []byte(WeakCertificate),
333 },
334 asserts: func(t *testing.T, c *Config, grpcOption bool) {
335 if grpcOption {
336 assert.NotNil(t, c.Traces.GRPCCredentials)
337 } else {
338 // nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
339 assert.Equal(t, tlsCert.RootCAs.Subjects(), c.Traces.TLSCfg.RootCAs.Subjects())
340 }
341 },
342 },
343
344 // Headers tests
345 {
346 name: "Test With Headers",
347 opts: []GenericOption{
348 WithHeaders(map[string]string{"h1": "v1"}),
349 },
350 asserts: func(t *testing.T, c *Config, grpcOption bool) {
351 assert.Equal(t, map[string]string{"h1": "v1"}, c.Traces.Headers)
352 },
353 },
354 {
355 name: "Test Environment Headers",
356 env: map[string]string{"OTEL_EXPORTER_OTLP_HEADERS": "h1=v1,h2=v2"},
357 asserts: func(t *testing.T, c *Config, grpcOption bool) {
358 assert.Equal(t, map[string]string{"h1": "v1", "h2": "v2"}, c.Traces.Headers)
359 },
360 },
361 {
362 name: "Test Environment Signal Specific Headers",
363 env: map[string]string{
364 "OTEL_EXPORTER_OTLP_HEADERS": "overrode_by_signal_specific",
365 "OTEL_EXPORTER_OTLP_TRACES_HEADERS": "h1=v1,h2=v2",
366 },
367 asserts: func(t *testing.T, c *Config, grpcOption bool) {
368 assert.Equal(t, map[string]string{"h1": "v1", "h2": "v2"}, c.Traces.Headers)
369 },
370 },
371 {
372 name: "Test Mixed Environment and With Headers",
373 env: map[string]string{"OTEL_EXPORTER_OTLP_HEADERS": "h1=v1,h2=v2"},
374 opts: []GenericOption{},
375 asserts: func(t *testing.T, c *Config, grpcOption bool) {
376 assert.Equal(t, map[string]string{"h1": "v1", "h2": "v2"}, c.Traces.Headers)
377 },
378 },
379
380 // Compression Tests
381 {
382 name: "Test With Compression",
383 opts: []GenericOption{
384 WithCompression(GzipCompression),
385 },
386 asserts: func(t *testing.T, c *Config, grpcOption bool) {
387 assert.Equal(t, GzipCompression, c.Traces.Compression)
388 },
389 },
390 {
391 name: "Test Environment Compression",
392 env: map[string]string{
393 "OTEL_EXPORTER_OTLP_COMPRESSION": "gzip",
394 },
395 asserts: func(t *testing.T, c *Config, grpcOption bool) {
396 assert.Equal(t, GzipCompression, c.Traces.Compression)
397 },
398 },
399 {
400 name: "Test Environment Signal Specific Compression",
401 env: map[string]string{
402 "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION": "gzip",
403 },
404 asserts: func(t *testing.T, c *Config, grpcOption bool) {
405 assert.Equal(t, GzipCompression, c.Traces.Compression)
406 },
407 },
408 {
409 name: "Test Mixed Environment and With Compression",
410 opts: []GenericOption{
411 WithCompression(NoCompression),
412 },
413 env: map[string]string{
414 "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION": "gzip",
415 },
416 asserts: func(t *testing.T, c *Config, grpcOption bool) {
417 assert.Equal(t, NoCompression, c.Traces.Compression)
418 },
419 },
420
421 // Timeout Tests
422 {
423 name: "Test With Timeout",
424 opts: []GenericOption{
425 WithTimeout(5 * time.Second),
426 },
427 asserts: func(t *testing.T, c *Config, grpcOption bool) {
428 assert.Equal(t, 5*time.Second, c.Traces.Timeout)
429 },
430 },
431 {
432 name: "Test Environment Timeout",
433 env: map[string]string{
434 "OTEL_EXPORTER_OTLP_TIMEOUT": "15000",
435 },
436 asserts: func(t *testing.T, c *Config, grpcOption bool) {
437 assert.Equal(t, 15*time.Second, c.Traces.Timeout)
438 },
439 },
440 {
441 name: "Test Environment Signal Specific Timeout",
442 env: map[string]string{
443 "OTEL_EXPORTER_OTLP_TIMEOUT": "15000",
444 "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT": "27000",
445 },
446 asserts: func(t *testing.T, c *Config, grpcOption bool) {
447 assert.Equal(t, 27*time.Second, c.Traces.Timeout)
448 },
449 },
450 {
451 name: "Test Mixed Environment and With Timeout",
452 env: map[string]string{
453 "OTEL_EXPORTER_OTLP_TIMEOUT": "15000",
454 "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT": "27000",
455 },
456 opts: []GenericOption{
457 WithTimeout(5 * time.Second),
458 },
459 asserts: func(t *testing.T, c *Config, grpcOption bool) {
460 assert.Equal(t, 5*time.Second, c.Traces.Timeout)
461 },
462 },
463
464 // Proxy Tests
465 {
466 name: "Test With Proxy",
467 opts: []GenericOption{
468 WithProxy(func(r *http.Request) (*url.URL, error) {
469 return url.Parse("http://proxy.com")
470 }),
471 },
472 asserts: func(t *testing.T, c *Config, grpcOption bool) {
473 assert.NotNil(t, c.Traces.Proxy)
474 proxyURL, err := c.Traces.Proxy(&http.Request{})
475 assert.NoError(t, err)
476 assert.Equal(t, "http://proxy.com", proxyURL.String())
477 },
478 },
479 {
480 name: "Test Without Proxy",
481 opts: []GenericOption{},
482 asserts: func(t *testing.T, c *Config, grpcOption bool) {
483 assert.Nil(t, c.Traces.Proxy)
484 },
485 },
486
487 // HTTP Client Tests
488 {
489 name: "Test With HTTP Client",
490 opts: []GenericOption{
491 WithHTTPClient(http.DefaultClient),
492 },
493 asserts: func(t *testing.T, c *Config, grpcOption bool) {
494 assert.Equal(t, http.DefaultClient, c.Traces.HTTPClient)
495 },
496 },
497 {
498 name: "Test Without HTTP Client",
499 opts: []GenericOption{},
500 asserts: func(t *testing.T, c *Config, grpcOption bool) {
501 assert.Nil(t, c.Traces.HTTPClient)
502 },
503 },
504 }
505
506 for _, tt := range tests {
507 t.Run(tt.name, func(t *testing.T) {
508 origEOR := DefaultEnvOptionsReader
509 DefaultEnvOptionsReader = envconfig.EnvOptionsReader{
510 GetEnv: tt.env.getEnv,
511 ReadFile: tt.fileReader.readFile,
512 Namespace: "OTEL_EXPORTER_OTLP",
513 }
514 t.Cleanup(func() { DefaultEnvOptionsReader = origEOR })
515
516 // Tests Generic options as HTTP Options
517 cfg := NewHTTPConfig(asHTTPOptions(tt.opts)...)
518 tt.asserts(t, &cfg, false)
519
520 // Tests Generic options as gRPC Options
521 cfg = NewGRPCConfig(asGRPCOptions(tt.opts)...)
522 tt.asserts(t, &cfg, true)
523 })
524 }
525 }
526
527 func asHTTPOptions(opts []GenericOption) []HTTPOption {
528 converted := make([]HTTPOption, len(opts))
529 for i, o := range opts {
530 converted[i] = NewHTTPOption(o.ApplyHTTPOption)
531 }
532 return converted
533 }
534
535 func asGRPCOptions(opts []GenericOption) []GRPCOption {
536 converted := make([]GRPCOption, len(opts))
537 for i, o := range opts {
538 converted[i] = NewGRPCOption(o.ApplyGRPCOption)
539 }
540 return converted
541 }
542
543 func TestCleanPath(t *testing.T) {
544 type args struct {
545 urlPath string
546 defaultPath string
547 }
548 tests := []struct {
549 name string
550 args args
551 want string
552 }{
553 {
554 name: "clean empty path",
555 args: args{
556 urlPath: "",
557 defaultPath: "DefaultPath",
558 },
559 want: "DefaultPath",
560 },
561 {
562 name: "clean metrics path",
563 args: args{
564 urlPath: "/prefix/v1/metrics",
565 defaultPath: "DefaultMetricsPath",
566 },
567 want: "/prefix/v1/metrics",
568 },
569 {
570 name: "clean traces path",
571 args: args{
572 urlPath: "https://env_endpoint/ ",
573 defaultPath: "DefaultTracesPath",
574 },
575 want: "/https://env_endpoint/",
576 },
577 {
578 name: "spaces trimmed",
579 args: args{
580 urlPath: " /dir",
581 },
582 want: "/dir",
583 },
584 {
585 name: "make absolute",
586 args: args{
587 urlPath: "dir/a",
588 },
589 want: "/dir/a",
590 },
591 }
592 for _, tt := range tests {
593 t.Run(tt.name, func(t *testing.T) {
594 if got := cleanPath(tt.args.urlPath, tt.args.defaultPath); got != tt.want {
595 t.Errorf("CleanPath() = %v, want %v", got, tt.want)
596 }
597 })
598 }
599 }