-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathreceiver_test.go
More file actions
457 lines (430 loc) · 12.4 KB
/
Copy pathreceiver_test.go
File metadata and controls
457 lines (430 loc) · 12.4 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
// 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.
package mbreceiver
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand/v2"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/elastic/beats/v7/x-pack/otel/oteltest"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentstatus"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
func TestNewReceiver(t *testing.T) {
monitorSocket := genSocketPath()
var monitorHost string
if runtime.GOOS == "windows" {
monitorHost = "npipe:///" + filepath.Base(monitorSocket)
} else {
monitorHost = "unix://" + monitorSocket
}
config := Config{
Beatconfig: map[string]any{
"metricbeat": map[string]any{
"modules": []map[string]any{
{
"module": "system",
"enabled": true,
"period": "1s",
"processes": []string{".*"},
"metricsets": []string{"cpu"},
},
},
},
"logging": map[string]any{
"level": "debug",
"selectors": []string{
"*",
},
},
"path.home": t.TempDir(),
"http.enabled": true,
"http.host": monitorHost,
"management.otel.enabled": true,
},
}
oteltest.CheckReceivers(oteltest.CheckReceiversParams{
T: t,
Receivers: []oteltest.ReceiverConfig{
{
Name: "r1",
Beat: "metricbeat",
Config: &config,
Factory: NewFactory(),
},
},
AssertFunc: func(c *assert.CollectT, logs map[string][]mapstr.M, zapLogs *observer.ObservedLogs) {
_ = zapLogs
require.Conditionf(c, func() bool {
return len(logs["r1"]) > 0
}, "expected at least one ingest log, got logs: %v", logs["r1"])
assert.Equal(c, "metricbeat", logs["r1"][0].Flatten()["agent.type"], "expected agent.type field in to be 'metricbeat'")
var lastError strings.Builder
assert.Conditionf(c, func() bool {
return getFromSocket(t, &lastError, monitorSocket, "stats")
}, "failed to connect to monitoring socket stats endpoint, last error was: %s", &lastError)
assert.Conditionf(c, func() bool {
return getFromSocket(t, &lastError, monitorSocket, "inputs")
}, "failed to connect to monitoring socket inputs endpoint, last error was: %s", &lastError)
assert.Condition(c, func() bool {
processorsLoaded := zapLogs.FilterMessageSnippet("Generated new processors").
FilterMessageSnippet("add_host_metadata").
FilterMessageSnippet("add_cloud_metadata").
FilterMessageSnippet("add_docker_metadata").
FilterMessageSnippet("add_kubernetes_metadata").
Len() == 1
assert.True(c, processorsLoaded, "processors not loaded")
// Check that add_host_metadata works, other processors are not guaranteed to add fields in all environments
return assert.Contains(c, logs["r1"][0].Flatten(), "host.architecture")
}, "failed to check processors loaded")
},
})
}
func TestMultipleReceivers(t *testing.T) {
monitorSocket1 := genSocketPath()
var monitorHost1 string
if runtime.GOOS == "windows" {
monitorHost1 = "npipe:///" + filepath.Base(monitorSocket1)
} else {
monitorHost1 = "unix://" + monitorSocket1
}
monitorSocket2 := genSocketPath()
var monitorHost2 string
if runtime.GOOS == "windows" {
monitorHost2 = "npipe:///" + filepath.Base(monitorSocket2)
} else {
monitorHost2 = "unix://" + monitorSocket2
}
config1 := Config{
Beatconfig: map[string]any{
"metricbeat": map[string]any{
"modules": []map[string]any{
{
"module": "system",
"enabled": true,
"period": "1s",
"processes": []string{".*"},
"metricsets": []string{"cpu"},
},
},
},
"logging": map[string]any{
"level": "debug",
"selectors": []string{
"*",
},
},
"path.home": t.TempDir(),
"http.enabled": true,
"http.host": monitorHost1,
},
}
config2 := Config{
Beatconfig: map[string]any{
"metricbeat": map[string]any{
"modules": []map[string]any{
{
"module": "system",
"enabled": true,
"period": "1s",
"processes": []string{".*"},
"metricsets": []string{"cpu"},
},
},
},
"logging": map[string]any{
"level": "debug",
"selectors": []string{
"*",
},
},
"path.home": t.TempDir(),
"http.enabled": true,
"http.host": monitorHost2,
},
}
factory := NewFactory()
oteltest.CheckReceivers(oteltest.CheckReceiversParams{
T: t,
Receivers: []oteltest.ReceiverConfig{
{
Name: "r1",
Beat: "metricbeat",
Config: &config1,
Factory: factory,
},
{
Name: "r2",
Beat: "metricbeat",
Config: &config2,
Factory: factory,
},
},
AssertFunc: func(c *assert.CollectT, logs map[string][]mapstr.M, zapLogs *observer.ObservedLogs) {
_ = zapLogs
require.Conditionf(c, func() bool {
return len(logs["r1"]) > 0 && len(logs["r2"]) > 0
}, "expected at least one ingest log for each receiver, got logs: %v", logs)
assert.Equal(c, "metricbeat", logs["r1"][0].Flatten()["agent.type"], "expected agent.type field to be 'metricbeat' in r1")
assert.Equal(c, "metricbeat", logs["r2"][0].Flatten()["agent.type"], "expected agent.type field to be 'metricbeat' in r2")
// Make sure that each receiver has a separate logger
// instance and does not interfere with others. Previously, the
// logger in Beats was global, causing logger fields to be
// overwritten when multiple receivers started in the same process.
r1StartLogs := zapLogs.FilterMessageSnippet("Beat ID").FilterField(zap.String("otelcol.component.id", "metricbeatreceiver/r1"))
assert.Equal(c, 1, r1StartLogs.Len(), "r1 should have a single start log")
r2StartLogs := zapLogs.FilterMessageSnippet("Beat ID").FilterField(zap.String("otelcol.component.id", "metricbeatreceiver/r2"))
assert.Equal(c, 1, r2StartLogs.Len(), "r2 should have a single start log")
var lastError strings.Builder
assert.Conditionf(c, func() bool {
tests := []string{monitorSocket1, monitorSocket2}
for _, tc := range tests {
if ret := getFromSocket(t, &lastError, tc, "stats"); ret == false {
return false
}
if ret := getFromSocket(t, &lastError, tc, "inputs"); ret == false {
return false
}
}
return true
}, "failed to connect to monitoring socket, last error was: %s", &lastError)
},
})
}
func genSocketPath() string {
randData := make([]byte, 16)
for i := range len(randData) {
randData[i] = uint8(rand.UintN(255)) //nolint:gosec // 0-255 fits in a uint8
}
socketName := base64.URLEncoding.EncodeToString(randData) + ".sock"
socketDir := os.TempDir()
return filepath.Join(socketDir, socketName)
}
func getFromSocket(t *testing.T, sb *strings.Builder, socketPath string, endpoint string) bool {
// skip windows for now
if runtime.GOOS == "windows" {
return true
}
client := http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
}
defer client.CloseIdleConnections()
url, err := url.JoinPath("http://unix", endpoint)
if err != nil {
sb.Reset()
fmt.Fprintf(sb, "JoinPath failed: %s", err)
return false
}
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, url, nil)
if err != nil {
sb.Reset()
fmt.Fprintf(sb, "error creating request: %s", err)
return false
}
resp, err := client.Do(req)
if err != nil {
sb.Reset()
fmt.Fprintf(sb, "client.Get failed: %s", err)
return false
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
sb.Reset()
fmt.Fprintf(sb, "io.ReadAll of body failed: %s", err)
return false
}
if len(body) <= 0 {
sb.Reset()
sb.WriteString("body too short")
return false
}
if endpoint == "inputs" {
var data []any
if err := json.Unmarshal(body, &data); err != nil {
sb.Reset()
fmt.Fprintf(sb, "json unmarshal of body failed: %s\n", err)
fmt.Fprintf(sb, "body was %v\n", body)
return false
}
if len(data) <= 0 {
sb.Reset()
sb.WriteString("json array didn't have any entries")
return false
}
} else {
var data map[string]any
if err := json.Unmarshal(body, &data); err != nil {
sb.Reset()
fmt.Fprintf(sb, "json unmarshal of body failed: %s\n", err)
fmt.Fprintf(sb, "body was %v\n", body)
return false
}
if len(data) <= 0 {
sb.Reset()
sb.WriteString("json didn't have any keys")
return false
}
}
return true
}
func BenchmarkFactory(b *testing.B) {
tmpDir := b.TempDir()
cfg := &Config{
Beatconfig: map[string]interface{}{
"metricbeat": map[string]any{
"modules": []map[string]any{
{
"module": "system",
"enabled": true,
"period": "1s",
"processes": []string{".*"},
"metricsets": []string{"cpu"},
},
},
},
"logging": map[string]any{
"level": "info",
"selectors": []string{
"*",
},
},
"path.home": tmpDir,
},
}
var zapLogs bytes.Buffer
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.Lock(zapcore.AddSync(&zapLogs)),
zapcore.InfoLevel)
factory := NewFactory()
receiverSettings := receiver.Settings{}
receiverSettings.Logger = zap.New(core)
receiverSettings.ID = component.NewIDWithName(factory.Type(), "r1")
b.ResetTimer()
for b.Loop() {
_, err := factory.CreateLogs(b.Context(), receiverSettings, cfg, nil)
require.NoError(b, err)
}
}
func TestReceiverStatus(t *testing.T) {
benchmarkInputId := "benchmark-id"
inputStatusAttributes := func(state string, msg string) pcommon.Map {
eventAttributes := pcommon.NewMap()
inputStatuses := eventAttributes.PutEmptyMap("inputs")
benchmarkStatus := inputStatuses.PutEmptyMap(benchmarkInputId)
benchmarkStatus.PutStr("status", state)
benchmarkStatus.PutStr("error", msg)
return eventAttributes
}
expectedDegradedErrorMessage := "Error fetching data for metricset benchmark.info: benchmark input degraded"
testCases := []struct {
name string
status *componentstatus.Event
degrade bool
}{
{
name: "degraded input",
status: componentstatus.NewEvent(
componentstatus.StatusRecoverableError,
componentstatus.WithError(errors.New(expectedDegradedErrorMessage)),
componentstatus.WithAttributes(inputStatusAttributes(
componentstatus.StatusRecoverableError.String(), expectedDegradedErrorMessage)),
),
degrade: true,
},
{
name: "running input",
status: componentstatus.NewEvent(componentstatus.StatusOK,
componentstatus.WithAttributes(inputStatusAttributes(
componentstatus.StatusOK.String(), ""))),
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
config := Config{
Beatconfig: map[string]any{
"metricbeat": map[string]any{
"max_start_delay": "0s",
"modules": []map[string]any{
{
"id": benchmarkInputId,
"module": "benchmark",
"enabled": true,
"period": "1s",
"degrade": test.degrade,
"metricsets": []string{"info"},
},
},
},
"path.home": t.TempDir(),
},
}
oteltest.CheckReceivers(oteltest.CheckReceiversParams{
T: t,
Receivers: []oteltest.ReceiverConfig{
{
Name: "r1",
Beat: "metricbeat",
Config: &config,
Factory: NewFactory(),
},
},
Status: test.status,
})
})
}
}
func TestReceiverHook(t *testing.T) {
cfg := Config{
Beatconfig: map[string]any{
"metricbeat": map[string]any{
"max_start_delay": "0s",
"modules": []map[string]any{
{
"module": "benchmark",
"enabled": true,
"period": "1s",
"metricsets": []string{"info"},
},
},
},
"management.otel.enabled": true,
"path.home": t.TempDir(),
},
}
receiverSettings := receiver.Settings{
ID: component.MustNewID(Name),
TelemetrySettings: component.TelemetrySettings{
Logger: zap.NewNop(),
},
}
// For metricbeatreceiver, we expect 2 hooks to be registered:
// one for beat metrics and one for input metrics.
oteltest.TestReceiverHook(t, &cfg, NewFactory(), receiverSettings, 2)
}