Debian Codegraph @

exporter/otlphttpexporter/config_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlphttpexporter
5
6 import (
7 "net/http"
8 "path/filepath"
9 "testing"
10 "time"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14
15 "go.opentelemetry.io/collector/config/confighttp"
16 "go.opentelemetry.io/collector/config/configopaque"
17 "go.opentelemetry.io/collector/config/configoptional"
18 "go.opentelemetry.io/collector/config/configretry"
19 "go.opentelemetry.io/collector/config/configtls"
20 "go.opentelemetry.io/collector/confmap"
21 "go.opentelemetry.io/collector/confmap/confmaptest"
22 "go.opentelemetry.io/collector/confmap/xconfmap"
23 "go.opentelemetry.io/collector/exporter/exporterhelper"
24 )
25
26 func TestUnmarshalDefaultConfig(t *testing.T) {
27 factory := NewFactory()
28 cfg := factory.CreateDefaultConfig()
29 require.NoError(t, confmap.New().Unmarshal(&cfg))
30 assert.Equal(t, factory.CreateDefaultConfig(), cfg)
31 // Default/Empty config is invalid.
32 assert.Error(t, xconfmap.Validate(cfg))
33 }
34
35 func TestUnmarshalConfig(t *testing.T) {
36 defaultMaxIdleConns := http.DefaultTransport.(*http.Transport).MaxIdleConns
37 defaultMaxIdleConnsPerHost := http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost
38 defaultMaxConnsPerHost := http.DefaultTransport.(*http.Transport).MaxConnsPerHost
39 defaultIdleConnTimeout := http.DefaultTransport.(*http.Transport).IdleConnTimeout
40
41 cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
42 require.NoError(t, err)
43 factory := NewFactory()
44 cfg := factory.CreateDefaultConfig()
45 require.NoError(t, cm.Unmarshal(&cfg))
46 assert.Equal(t,
47 &Config{
48 RetryConfig: configretry.BackOffConfig{
49 Enabled: true,
50 InitialInterval: 10 * time.Second,
51 RandomizationFactor: 0.7,
52 Multiplier: 1.3,
53 MaxInterval: 1 * time.Minute,
54 MaxElapsedTime: 10 * time.Minute,
55 },
56 QueueConfig: exporterhelper.QueueBatchConfig{
57 Enabled: true,
58 Sizer: exporterhelper.RequestSizerTypeRequests,
59 NumConsumers: 2,
60 QueueSize: 10,
61 Batch: configoptional.Default(exporterhelper.BatchConfig{
62 Sizer: exporterhelper.RequestSizerTypeItems,
63 FlushTimeout: 200 * time.Millisecond,
64 MinSize: 8192,
65 }),
66 },
67 Encoding: EncodingProto,
68 ClientConfig: confighttp.ClientConfig{
69 Headers: configopaque.MapList{
70 {Name: "another", Value: "somevalue"},
71 {Name: "can you have a . here?", Value: "F0000000-0000-0000-0000-000000000000"},
72 {Name: "header1", Value: "234"},
73 },
74 Endpoint: "https://1.2.3.4:1234",
75 TLS: configtls.ClientConfig{
76 Config: configtls.Config{
77 CAFile: "/var/lib/mycert.pem",
78 CertFile: "certfile",
79 KeyFile: "keyfile",
80 },
81 Insecure: true,
82 },
83 ReadBufferSize: 123,
84 WriteBufferSize: 345,
85 Timeout: time.Second * 10,
86 Compression: "gzip",
87 MaxIdleConns: defaultMaxIdleConns,
88 MaxIdleConnsPerHost: defaultMaxIdleConnsPerHost,
89 MaxConnsPerHost: defaultMaxConnsPerHost,
90 IdleConnTimeout: defaultIdleConnTimeout,
91 ForceAttemptHTTP2: true,
92 },
93 ProfilesEndpoint: "https://custom.profiles.endpoint:8080/v1development/profiles",
94 }, cfg)
95 }
96
97 func TestUnmarshalConfigInvalidEncoding(t *testing.T) {
98 cm, err := confmaptest.LoadConf(filepath.Join("testdata", "bad_invalid_encoding.yaml"))
99 require.NoError(t, err)
100 factory := NewFactory()
101 cfg := factory.CreateDefaultConfig()
102 assert.Error(t, cm.Unmarshal(&cfg))
103 }
104
105 func TestUnmarshalEncoding(t *testing.T) {
106 tests := []struct {
107 name string
108 encodingBytes []byte
109 expected EncodingType
110 shouldError bool
111 }{
112 {
113 name: "UnmarshalEncodingProto",
114 encodingBytes: []byte("proto"),
115 expected: EncodingProto,
116 shouldError: false,
117 },
118 {
119 name: "UnmarshalEncodingJson",
120 encodingBytes: []byte("json"),
121 expected: EncodingJSON,
122 shouldError: false,
123 },
124 {
125 name: "UnmarshalEmptyEncoding",
126 encodingBytes: []byte(""),
127 shouldError: true,
128 },
129 {
130 name: "UnmarshalInvalidEncoding",
131 encodingBytes: []byte("invalid"),
132 shouldError: true,
133 },
134 }
135
136 for _, tt := range tests {
137 t.Run(tt.name, func(t *testing.T) {
138 var encoding EncodingType
139 err := encoding.UnmarshalText(tt.encodingBytes)
140
141 if tt.shouldError {
142 assert.Error(t, err)
143 } else {
144 require.NoError(t, err)
145 assert.Equal(t, tt.expected, encoding)
146 }
147 })
148 }
149 }
150
151 func TestConfigValidate(t *testing.T) {
152 tests := []struct {
153 name string
154 cfg *Config
155 wantErr bool
156 }{
157 {
158 name: "no endpoints specified",
159 cfg: &Config{
160 ClientConfig: confighttp.ClientConfig{},
161 },
162 wantErr: true,
163 },
164 {
165 name: "main endpoint specified",
166 cfg: &Config{
167 ClientConfig: confighttp.ClientConfig{
168 Endpoint: "http://localhost:4318",
169 },
170 },
171 wantErr: false,
172 },
173 {
174 name: "only traces endpoint specified",
175 cfg: &Config{
176 ClientConfig: confighttp.ClientConfig{},
177 TracesEndpoint: "http://localhost:4318/v1/traces",
178 },
179 wantErr: false,
180 },
181 {
182 name: "only profiles endpoint specified",
183 cfg: &Config{
184 ClientConfig: confighttp.ClientConfig{},
185 ProfilesEndpoint: "http://localhost:4318/v1development/profiles",
186 },
187 wantErr: false,
188 },
189 {
190 name: "multiple endpoints specified",
191 cfg: &Config{
192 ClientConfig: confighttp.ClientConfig{},
193 TracesEndpoint: "http://localhost:4318/v1/traces",
194 MetricsEndpoint: "http://localhost:4318/v1/metrics",
195 LogsEndpoint: "http://localhost:4318/v1/logs",
196 ProfilesEndpoint: "http://localhost:4318/v1development/profiles",
197 },
198 wantErr: false,
199 },
200 }
201
202 for _, tt := range tests {
203 t.Run(tt.name, func(t *testing.T) {
204 err := tt.cfg.Validate()
205 if tt.wantErr {
206 require.Error(t, err)
207 assert.Contains(t, err.Error(), "at least one endpoint must be specified")
208 } else {
209 assert.NoError(t, err)
210 }
211 })
212 }
213 }