-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathotel_test.go
More file actions
555 lines (497 loc) · 16.5 KB
/
Copy pathotel_test.go
File metadata and controls
555 lines (497 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
//go:build integration && !agentbeat
package integration
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"sync"
"testing"
"text/template"
"time"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"github.com/gofrs/uuid/v5"
libbeattesting "github.com/elastic/beats/v7/libbeat/testing"
"github.com/elastic/beats/v7/libbeat/tests/integration"
"github.com/elastic/beats/v7/x-pack/otel/oteltestcol"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/testing/estools"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/mock-es/pkg/api"
)
func TestFilebeatOTelE2E(t *testing.T) {
integration.EnsureESIsRunning(t)
numEvents := 1
namespace := strings.ReplaceAll(uuid.Must(uuid.NewV4()).String(), "-", "")
fbOtelIndex := "logs-integration-" + namespace
fbIndex := "logs-filebeat-" + namespace
otelMonitoringPort := int(libbeattesting.MustAvailableTCP4Port(t))
filebeatMonitoringPort := int(libbeattesting.MustAvailableTCP4Port(t))
beatsCfgFile := `
filebeat.inputs:
- type: filestream
id: filestream-input-id
enabled: true
file_identity.native: ~
prospector.scanner.fingerprint.enabled: false
paths:
- %s
output:
elasticsearch:
hosts:
- localhost:9200
protocol: http
username: admin
password: testing
index: %s
queue.mem.flush.timeout: 0s
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
http.enabled: true
http.host: localhost
http.port: %d
`
otelCfgFile := `receivers:
filebeatreceiver:
filebeat:
inputs:
- type: filestream
id: filestream-input-id
enabled: true
paths:
- %s
prospector.scanner.fingerprint.enabled: false
file_identity.native: ~
output:
otelconsumer:
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
logging:
level: info
selectors:
- '*'
queue.mem.flush.timeout: 0s
setup.template.enabled: false
path.home: %s
http.enabled: true
http.host: localhost
http.port: %d
exporters:
debug:
use_internal_logger: false
verbosity: detailed
elasticsearch/log:
endpoints:
- http://localhost:9200
compression: none
user: admin
password: testing
logs_index: %s
batcher:
enabled: true
flush_timeout: 1s
mapping:
mode: bodymap
service:
pipelines:
logs:
receivers:
- filebeatreceiver
exporters:
- elasticsearch/log
- debug
`
tmpdir := t.TempDir()
logFilePath := filepath.Join(tmpdir, "log.log")
writeEventsToLogFile(t, logFilePath, numEvents)
oteltestcol.New(t, fmt.Sprintf(otelCfgFile, logFilePath, tmpdir, otelMonitoringPort, fbOtelIndex))
// start filebeat
filebeat := integration.NewBeat(
t,
"filebeat",
"../../filebeat.test",
)
logFilePath = filepath.Join(filebeat.TempDir(), "log.log")
writeEventsToLogFile(t, logFilePath, numEvents)
s := fmt.Sprintf(beatsCfgFile, logFilePath, fbIndex, filebeatMonitoringPort)
s = s + `
setup.template.name: logs-filebeat-default
setup.template.pattern: logs-filebeat-default
`
filebeat.WriteConfigFile(s)
filebeat.Start()
// prepare to query ES
esCfg := elasticsearch.Config{
Addresses: []string{"http://localhost:9200"},
Username: "admin",
Password: "testing",
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, //nolint:gosec // this is only for testing
},
},
}
es, err := elasticsearch.NewClient(esCfg)
require.NoError(t, err)
var filebeatDocs estools.Documents
var otelDocs estools.Documents
// wait for logs to be published
require.EventuallyWithTf(t,
func(ct *assert.CollectT) {
findCtx, findCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer findCancel()
otelDocs, err = estools.GetAllLogsForIndexWithContext(findCtx, es, ".ds-"+fbOtelIndex+"*")
assert.NoError(ct, err)
filebeatDocs, err = estools.GetAllLogsForIndexWithContext(findCtx, es, ".ds-"+fbIndex+"*")
assert.NoError(ct, err)
assert.GreaterOrEqual(ct, otelDocs.Hits.Total.Value, numEvents, "expected at least %d otel events, got %d", numEvents, otelDocs.Hits.Total.Value)
assert.GreaterOrEqual(ct, filebeatDocs.Hits.Total.Value, numEvents, "expected at least %d filebeat events, got %d", numEvents, filebeatDocs.Hits.Total.Value)
},
2*time.Minute, 1*time.Second, "expected at least %d events for both filebeat and otel", numEvents)
var filebeatDoc, otelDoc mapstr.M
filebeatDoc = filebeatDocs.Hits.Hits[0].Source
otelDoc = otelDocs.Hits.Hits[0].Source
ignoredFields := []string{
// Expected to change between agentDocs and OtelDocs
"@timestamp",
"agent.ephemeral_id",
"agent.id",
"log.file.inode",
"log.file.path",
"log.file.device_id", // changes value between filebeat and otel receiver
"container.id", // only present in filebeat
}
assertMapsEqual(t, filebeatDoc, otelDoc, ignoredFields, "expected documents to be equal")
assert.Equal(t, "filebeat", otelDoc.Flatten()["agent.type"], "expected agent.type field to be 'filebeat' in otel docs")
assert.Equal(t, "filebeat", filebeatDoc.Flatten()["agent.type"], "expected agent.type field to be 'filebeat' in filebeat docs")
assertMonitoring(t, otelMonitoringPort)
}
func writeEventsToLogFile(t *testing.T, filename string, numEvents int) {
t.Helper()
logFile, err := os.Create(filename)
if err != nil {
t.Fatalf("could not create file '%s': %s", filename, err)
}
// write events to log file
for i := 0; i < numEvents; i++ {
msg := fmt.Sprintf("Line %d", i)
_, err = logFile.Write([]byte(msg + "\n"))
require.NoErrorf(t, err, "failed to write line %d to temp file", i)
}
if err := logFile.Sync(); err != nil {
t.Fatalf("could not sync log file '%s': %s", filename, err)
}
if err := logFile.Close(); err != nil {
t.Fatalf("could not close log file '%s': %s", filename, err)
}
}
func assertMapsEqual(t *testing.T, m1, m2 mapstr.M, ignoredFields []string, msg string) {
t.Helper()
flatM1 := m1.Flatten()
flatM2 := m2.Flatten()
for _, f := range ignoredFields {
hasKeyM1, _ := flatM1.HasKey(f)
hasKeyM2, _ := flatM2.HasKey(f)
if !hasKeyM1 && !hasKeyM2 {
assert.Failf(t, msg, "ignored field %q does not exist in either map, please remove it from the ignored fields", f)
}
flatM1.Delete(f)
flatM2.Delete(f)
}
require.Equal(t, "", cmp.Diff(flatM1, flatM2), "expected maps to be equal")
}
func assertMonitoring(t *testing.T, port int) {
address := fmt.Sprintf("http://localhost:%d", port)
r, err := http.Get(address) //nolint:noctx,bodyclose,gosec // fine for tests
require.NoError(t, err)
require.Equal(t, http.StatusOK, r.StatusCode, "incorrect status code")
r, err = http.Get(address + "/stats") //nolint:noctx,bodyclose // fine for tests
require.NoError(t, err)
require.Equal(t, http.StatusOK, r.StatusCode, "incorrect status code")
r, err = http.Get(address + "/not-exist") //nolint:noctx,bodyclose // fine for tests
require.NoError(t, err)
require.Equal(t, http.StatusNotFound, r.StatusCode, "incorrect status code")
}
func TestFilebeatOTelDocumentLevelRetries(t *testing.T) {
tests := []struct {
name string
maxRetries int
failuresPerEvent int
bulkErrorCode string
eventIDsToFail []int
expectedIngestedEventIDs []int
}{
{
name: "bulk 429 with retries",
maxRetries: 3,
failuresPerEvent: 2, // Fail 2 times, succeed on 3rd attempt
bulkErrorCode: "429", // retryable error
eventIDsToFail: []int{1, 3, 5, 7},
expectedIngestedEventIDs: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, // All events should eventually be ingested
},
{
name: "bulk exhausts retries",
maxRetries: 3,
failuresPerEvent: 5, // Fail more than max_retries
bulkErrorCode: "429",
eventIDsToFail: []int{2, 4, 6, 8},
expectedIngestedEventIDs: []int{0, 1, 3, 5, 7, 9}, // Only non-failing events should be ingested
},
{
name: "bulk with permanent mapping errors",
maxRetries: 3,
failuresPerEvent: 0, // always fail
bulkErrorCode: "400", // never retried
eventIDsToFail: []int{1, 4, 8}, // Only specific events fail
expectedIngestedEventIDs: []int{0, 2, 3, 5, 6, 7, 9}, // Only non-failing events should be ingested
},
}
const numTestEvents = 10
reEventLine := regexp.MustCompile(`"message":"Line (\d+)"`)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var ingestedTestEvents []string
var mu sync.Mutex
eventFailureCounts := make(map[string]int)
deterministicHandler := func(action api.Action, event []byte) int {
// Handle non-bulk requests
if action.Action != "create" {
return http.StatusOK
}
// Extract event ID from the event data
if matches := reEventLine.FindSubmatch(event); len(matches) > 1 {
eventIDStr := string(matches[1])
eventID, err := strconv.Atoi(eventIDStr)
if err != nil {
return http.StatusInternalServerError
}
eventKey := "Line " + eventIDStr
mu.Lock()
defer mu.Unlock()
isFailingEvent := slices.Contains(tt.eventIDsToFail, eventID)
var shouldFail bool
if isFailingEvent {
// This event is configured to fail
failureCount := eventFailureCounts[eventKey]
switch tt.bulkErrorCode {
case "400":
// Permanent errors always fail
shouldFail = true
case "429":
// Temporary errors fail until failuresPerEvent threshold
shouldFail = failureCount < tt.failuresPerEvent
}
} else {
// Events not in the fail list always succeed
shouldFail = false
}
if shouldFail {
eventFailureCounts[eventKey] = eventFailureCounts[eventKey] + 1
if tt.bulkErrorCode == "429" {
return http.StatusTooManyRequests
} else {
return http.StatusBadRequest
}
}
// track ingested event
found := false
for _, existing := range ingestedTestEvents {
if existing == eventKey {
found = true
break
}
}
if !found {
ingestedTestEvents = append(ingestedTestEvents, eventKey)
}
return http.StatusOK
}
return http.StatusOK
}
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
mux := http.NewServeMux()
mux.Handle("/", api.NewDeterministicAPIHandler(
uuid.Must(uuid.NewV4()),
"",
provider,
time.Now().Add(24*time.Hour),
0,
0,
deterministicHandler,
))
server := httptest.NewServer(mux)
defer server.Close()
namespace := strings.ReplaceAll(uuid.Must(uuid.NewV4()).String(), "-", "")
index := "logs-integration-" + namespace
beatsConfig := struct {
Index string
InputFile string
ESEndpoint string
MaxRetries int
MonitoringPort int
}{
Index: index,
InputFile: filepath.Join(t.TempDir(), "log.log"),
ESEndpoint: server.URL,
MaxRetries: tt.maxRetries,
MonitoringPort: int(libbeattesting.MustAvailableTCP4Port(t)),
}
cfg := `receivers:
filebeatreceiver:
filebeat:
inputs:
- type: filestream
id: filestream-input-id
enabled: true
paths:
- {{.InputFile}}
prospector.scanner.fingerprint.enabled: false
file_identity.native: ~
output:
otelconsumer:
logging:
level: debug
queue.mem.flush.timeout: 0s
setup.template.enabled: false
http.enabled: true
http.host: localhost
http.port: {{.MonitoringPort}}
exporters:
elasticsearch:
auth:
authenticator: beatsauth
compression: none
endpoints:
- {{.ESEndpoint}}
logs_index: {{.Index}}
mapping:
mode: bodymap
max_conns_per_host: 1
password: testing
retry:
enabled: true
initial_interval: 1s
max_interval: 1m0s
max_retries: {{.MaxRetries}}
sending_queue:
batch:
flush_timeout: 10s
max_size: 1600
min_size: 0
sizer: items
block_on_overflow: true
enabled: true
num_consumers: 1
queue_size: 3200
wait_for_result: true
user: admin
extensions:
beatsauth:
idle_connection_timeout: 3s
proxy_disable: false
timeout: 1m30s
service:
extensions:
- beatsauth
pipelines:
logs:
receivers:
- filebeatreceiver
exporters:
- elasticsearch
telemetry:
logs:
level: DEBUG
metrics:
level: none
`
var configBuffer bytes.Buffer
require.NoError(t,
template.Must(template.New("config").Parse(cfg)).Execute(&configBuffer, beatsConfig))
collector := oteltestcol.New(t, configBuffer.String())
writeEventsToLogFile(t, beatsConfig.InputFile, numTestEvents)
// Wait for file input to be fully read
require.Eventually(t, func() bool {
return collector.ObservedLogs().FilterMessageSnippet(fmt.Sprintf("End of file reached: %s; Backoff now.", beatsConfig.InputFile)).Len() == 1
}, 30*time.Second, 100*time.Millisecond, "timed out waiting for file input to be fully read")
// Wait for expected events to be ingested
require.EventuallyWithT(t, func(ct *assert.CollectT) {
mu.Lock()
defer mu.Unlock()
// collect mock-es metrics
rm := metricdata.ResourceMetrics{}
err := reader.Collect(context.Background(), &rm)
assert.NoError(ct, err, "failed to collect metrics from mock-es")
metrics := make(map[string]int64)
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
if sum, ok := m.Data.(metricdata.Sum[int64]); ok {
var total int64
for _, dp := range sum.DataPoints {
total += dp.Value
}
metrics[m.Name] = total
}
}
}
assert.Equal(ct, int64(len(tt.expectedIngestedEventIDs)), metrics["bulk.create.ok"], "expected bulk.create.ok metric to match ingested events")
// If we have the right count, validate the specific events
// Verify we have the correct events ingested
for _, expectedID := range tt.expectedIngestedEventIDs {
expectedEventKey := fmt.Sprintf("Line %d", expectedID)
found := false
for _, ingested := range ingestedTestEvents {
if ingested == expectedEventKey {
found = true
break
}
}
assert.True(ct, found, "expected _bulk event %s to be ingested", expectedEventKey)
}
// Verify we have valid line content for all ingested events
for _, ingested := range ingestedTestEvents {
assert.Regexp(ct, `^Line \d+$`, ingested, "unexpected ingested event format: %s", ingested)
}
}, 30*time.Second, 1*time.Second, "timed out waiting for expected event processing")
// Confirm filebeat agreed with our accounting of ingested events
require.EventuallyWithT(t, func(ct *assert.CollectT) {
address := fmt.Sprintf("http://localhost:%d", beatsConfig.MonitoringPort)
r, err := http.Get(address + "/stats") //nolint:noctx,bodyclose // fine for tests
assert.NoError(ct, err)
assert.Equal(ct, http.StatusOK, r.StatusCode, "incorrect status code")
var m mapstr.M
err = json.NewDecoder(r.Body).Decode(&m)
assert.NoError(ct, err)
m = m.Flatten()
// Currently, otelconsumer either ACKs or fails the entire batch and has no visibility into individual event failures within the exporter.
// From otelconsumer's perspective, the whole batch is considered successful as long as ConsumeLogs returns no error.
assert.Equal(ct, float64(numTestEvents), m["libbeat.output.events.total"], "expected total events sent to output to match")
assert.Equal(ct, float64(numTestEvents), m["libbeat.output.events.acked"], "expected total events acked to match")
assert.Equal(ct, float64(0), m["libbeat.output.events.dropped"], "expected total events dropped to match")
}, 10*time.Second, 100*time.Millisecond, "expected output stats to be available in monitoring endpoint")
})
}
}