Debian Codegraph @

exporter/otlpexporter/factory_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlpexporter
5
6 import (
7 "context"
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/component/componenttest"
16 "go.opentelemetry.io/collector/config/configcompression"
17 "go.opentelemetry.io/collector/config/configgrpc"
18 "go.opentelemetry.io/collector/config/configopaque"
19 "go.opentelemetry.io/collector/config/configoptional"
20 "go.opentelemetry.io/collector/config/configretry"
21 "go.opentelemetry.io/collector/config/configtls"
22 "go.opentelemetry.io/collector/exporter/exporterhelper"
23 "go.opentelemetry.io/collector/exporter/exportertest"
24 "go.opentelemetry.io/collector/exporter/xexporter"
25 "go.opentelemetry.io/collector/internal/testutil"
26 )
27
28 func TestCreateDefaultConfig(t *testing.T) {
29 factory := NewFactory()
30 cfg := factory.CreateDefaultConfig()
31 assert.NotNil(t, cfg, "failed to create default config")
32 require.NoError(t, componenttest.CheckConfigStruct(cfg))
33 ocfg, ok := factory.CreateDefaultConfig().(*Config)
34 assert.True(t, ok)
35 assert.Equal(t, configretry.NewDefaultBackOffConfig(), ocfg.RetryConfig)
36 assert.Equal(t, exporterhelper.NewDefaultQueueConfig(), ocfg.QueueConfig)
37 assert.Equal(t, exporterhelper.NewDefaultTimeoutConfig(), ocfg.TimeoutConfig)
38 assert.Equal(t, configcompression.TypeGzip, ocfg.ClientConfig.Compression)
39 }
40
41 func TestCreateMetrics(t *testing.T) {
42 factory := NewFactory()
43 cfg := factory.CreateDefaultConfig().(*Config)
44 cfg.ClientConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
45
46 set := exportertest.NewNopSettings(factory.Type())
47 oexp, err := factory.CreateMetrics(context.Background(), set, cfg)
48 require.NoError(t, err)
49 require.NotNil(t, oexp)
50 }
51
52 func TestCreateTraces(t *testing.T) {
53 endpoint := testutil.GetAvailableLocalAddress(t)
54 tests := []struct {
55 name string
56 config *Config
57 mustFailOnStart bool
58 }{
59 {
60 name: "UseSecure",
61 config: &Config{
62 ClientConfig: configgrpc.ClientConfig{
63 Endpoint: endpoint,
64 TLS: configtls.ClientConfig{
65 Insecure: false,
66 },
67 },
68 },
69 },
70 {
71 name: "Keepalive",
72 config: &Config{
73 ClientConfig: configgrpc.ClientConfig{
74 Endpoint: endpoint,
75 Keepalive: configoptional.Some(configgrpc.KeepaliveClientConfig{
76 Time: 30 * time.Second,
77 Timeout: 25 * time.Second,
78 PermitWithoutStream: true,
79 }),
80 },
81 },
82 },
83 {
84 name: "NoneCompression",
85 config: &Config{
86 ClientConfig: configgrpc.ClientConfig{
87 Endpoint: endpoint,
88 Compression: "none",
89 },
90 },
91 },
92 {
93 name: "GzipCompression",
94 config: &Config{
95 ClientConfig: configgrpc.ClientConfig{
96 Endpoint: endpoint,
97 Compression: configcompression.TypeGzip,
98 },
99 },
100 },
101 {
102 name: "SnappyCompression",
103 config: &Config{
104 ClientConfig: configgrpc.ClientConfig{
105 Endpoint: endpoint,
106 Compression: configcompression.TypeSnappy,
107 },
108 },
109 },
110 {
111 name: "ZstdCompression",
112 config: &Config{
113 ClientConfig: configgrpc.ClientConfig{
114 Endpoint: endpoint,
115 Compression: configcompression.TypeZstd,
116 },
117 },
118 },
119 {
120 name: "Headers",
121 config: &Config{
122 ClientConfig: configgrpc.ClientConfig{
123 Endpoint: endpoint,
124 Headers: configopaque.MapList{
125 {Name: "hdr1", Value: "val1"},
126 {Name: "hdr2", Value: "val2"},
127 },
128 },
129 },
130 },
131 {
132 name: "NumConsumers",
133 config: &Config{
134 ClientConfig: configgrpc.ClientConfig{
135 Endpoint: endpoint,
136 },
137 },
138 },
139 {
140 name: "CaCert",
141 config: &Config{
142 ClientConfig: configgrpc.ClientConfig{
143 Endpoint: endpoint,
144 TLS: configtls.ClientConfig{
145 Config: configtls.Config{
146 CAFile: filepath.Join("testdata", "test_cert.pem"),
147 },
148 },
149 },
150 },
151 },
152 {
153 name: "CertPemFileError",
154 config: &Config{
155 ClientConfig: configgrpc.ClientConfig{
156 Endpoint: endpoint,
157 TLS: configtls.ClientConfig{
158 Config: configtls.Config{
159 CAFile: "nosuchfile",
160 },
161 },
162 },
163 },
164 mustFailOnStart: true,
165 },
166 }
167
168 for _, tt := range tests {
169 t.Run(tt.name, func(t *testing.T) {
170 factory := NewFactory()
171 set := exportertest.NewNopSettings(factory.Type())
172 consumer, err := factory.CreateTraces(context.Background(), set, tt.config)
173 require.NoError(t, err)
174 assert.NotNil(t, consumer)
175 err = consumer.Start(context.Background(), componenttest.NewNopHost())
176 if tt.mustFailOnStart {
177 require.Error(t, err)
178 } else {
179 require.NoError(t, err)
180 }
181 // Shutdown is called even when Start fails
182 err = consumer.Shutdown(context.Background())
183 if err != nil {
184 // Since the endpoint of OTLP exporter doesn't actually exist,
185 // exporter may already stop because it cannot connect.
186 assert.Equal(t, "rpc error: code = Canceled desc = grpc: the client connection is closing", err.Error())
187 }
188 })
189 }
190 }
191
192 func TestCreateLogs(t *testing.T) {
193 factory := NewFactory()
194 cfg := factory.CreateDefaultConfig().(*Config)
195 cfg.ClientConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
196
197 set := exportertest.NewNopSettings(factory.Type())
198 oexp, err := factory.CreateLogs(context.Background(), set, cfg)
199 require.NoError(t, err)
200 require.NotNil(t, oexp)
201 }
202
203 func TestCreateProfiles(t *testing.T) {
204 endpoint := testutil.GetAvailableLocalAddress(t)
205 tests := []struct {
206 name string
207 config *Config
208 mustFailOnStart bool
209 }{
210 {
211 name: "UseSecure",
212 config: &Config{
213 ClientConfig: configgrpc.ClientConfig{
214 Endpoint: endpoint,
215 TLS: configtls.ClientConfig{
216 Insecure: false,
217 },
218 },
219 },
220 },
221 {
222 name: "Keepalive",
223 config: &Config{
224 ClientConfig: configgrpc.ClientConfig{
225 Endpoint: endpoint,
226 Keepalive: configoptional.Some(configgrpc.KeepaliveClientConfig{
227 Time: 30 * time.Second,
228 Timeout: 25 * time.Second,
229 PermitWithoutStream: true,
230 }),
231 },
232 },
233 },
234 {
235 name: "NoneCompression",
236 config: &Config{
237 ClientConfig: configgrpc.ClientConfig{
238 Endpoint: endpoint,
239 Compression: "none",
240 },
241 },
242 },
243 {
244 name: "GzipCompression",
245 config: &Config{
246 ClientConfig: configgrpc.ClientConfig{
247 Endpoint: endpoint,
248 Compression: configcompression.TypeGzip,
249 },
250 },
251 },
252 {
253 name: "SnappyCompression",
254 config: &Config{
255 ClientConfig: configgrpc.ClientConfig{
256 Endpoint: endpoint,
257 Compression: configcompression.TypeSnappy,
258 },
259 },
260 },
261 {
262 name: "ZstdCompression",
263 config: &Config{
264 ClientConfig: configgrpc.ClientConfig{
265 Endpoint: endpoint,
266 Compression: configcompression.TypeZstd,
267 },
268 },
269 },
270 {
271 name: "Headers",
272 config: &Config{
273 ClientConfig: configgrpc.ClientConfig{
274 Endpoint: endpoint,
275 Headers: configopaque.MapList{
276 {Name: "hdr1", Value: "val1"},
277 {Name: "hdr2", Value: "val2"},
278 },
279 },
280 },
281 },
282 {
283 name: "NumConsumers",
284 config: &Config{
285 ClientConfig: configgrpc.ClientConfig{
286 Endpoint: endpoint,
287 },
288 },
289 },
290 {
291 name: "CaCert",
292 config: &Config{
293 ClientConfig: configgrpc.ClientConfig{
294 Endpoint: endpoint,
295 TLS: configtls.ClientConfig{
296 Config: configtls.Config{
297 CAFile: filepath.Join("testdata", "test_cert.pem"),
298 },
299 },
300 },
301 },
302 },
303 {
304 name: "CertPemFileError",
305 config: &Config{
306 ClientConfig: configgrpc.ClientConfig{
307 Endpoint: endpoint,
308 TLS: configtls.ClientConfig{
309 Config: configtls.Config{
310 CAFile: "nosuchfile",
311 },
312 },
313 },
314 },
315 mustFailOnStart: true,
316 },
317 }
318
319 for _, tt := range tests {
320 t.Run(tt.name, func(t *testing.T) {
321 factory := NewFactory()
322 set := exportertest.NewNopSettings(factory.Type())
323 consumer, err := factory.(xexporter.Factory).CreateProfiles(context.Background(), set, tt.config)
324 require.NoError(t, err)
325 assert.NotNil(t, consumer)
326 err = consumer.Start(context.Background(), componenttest.NewNopHost())
327 if tt.mustFailOnStart {
328 require.Error(t, err)
329 } else {
330 require.NoError(t, err)
331 }
332 // Shutdown is called even when Start fails
333 err = consumer.Shutdown(context.Background())
334 if err != nil {
335 // Since the endpoint of OTLP exporter doesn't actually exist,
336 // exporter may already stop because it cannot connect.
337 assert.Equal(t, "rpc error: code = Canceled desc = grpc: the client connection is closing", err.Error())
338 }
339 })
340 }
341 }