@@ -235,7 +235,7 @@ func TestMonitoringConfigMetricsInterval(t *testing.T) {
235
235
// check the streams created for the input, should be a list of objects
236
236
if assert .Contains (t , input , "streams" , "input %q does not contain any stream" , inputID ) &&
237
237
assert .IsTypef (t , []any {}, input ["streams" ], "streams for input %q are not a list of objects" , inputID ) {
238
- // loop over streams and cast to map[string]any to access keys
238
+ // loop over streams and access keys
239
239
for _ , rawStream := range input ["streams" ].([]any ) {
240
240
if assert .IsTypef (t , map [string ]any {}, rawStream , "stream %v for input %q is not a map" , rawStream , inputID ) {
241
241
stream := rawStream .(map [string ]any )
@@ -258,6 +258,210 @@ func TestMonitoringConfigMetricsInterval(t *testing.T) {
258
258
}
259
259
}
260
260
261
+ func TestMonitoringConfigMetricsFailureThreshold (t * testing.T ) {
262
+
263
+ agentInfo , err := info .NewAgentInfo (context .Background (), false )
264
+ require .NoError (t , err , "Error creating agent info" )
265
+
266
+ sampleFiveErrorsStreamThreshold := uint (5 )
267
+ sampleTenErrorsStreamThreshold := uint (10 )
268
+
269
+ tcs := []struct {
270
+ name string
271
+ monitoringCfg * monitoringConfig
272
+ policy map [string ]any
273
+ expectedThreshold uint
274
+ }{
275
+ {
276
+ name : "default failure threshold" ,
277
+ monitoringCfg : & monitoringConfig {
278
+ C : & monitoringcfg.MonitoringConfig {
279
+ Enabled : true ,
280
+ MonitorMetrics : true ,
281
+ HTTP : & monitoringcfg.MonitoringHTTPConfig {
282
+ Enabled : false ,
283
+ },
284
+ },
285
+ },
286
+ policy : map [string ]any {
287
+ "agent" : map [string ]any {
288
+ "monitoring" : map [string ]any {
289
+ "metrics" : true ,
290
+ "http" : map [string ]any {
291
+ "enabled" : false ,
292
+ },
293
+ },
294
+ },
295
+ "outputs" : map [string ]any {
296
+ "default" : map [string ]any {},
297
+ },
298
+ },
299
+ expectedThreshold : defaultMetricsStreamFailureThreshold ,
300
+ },
301
+ {
302
+ name : "agent config failure threshold" ,
303
+ monitoringCfg : & monitoringConfig {
304
+ C : & monitoringcfg.MonitoringConfig {
305
+ Enabled : true ,
306
+ MonitorMetrics : true ,
307
+ HTTP : & monitoringcfg.MonitoringHTTPConfig {
308
+ Enabled : false ,
309
+ },
310
+ FailureThreshold : & sampleFiveErrorsStreamThreshold ,
311
+ },
312
+ },
313
+ policy : map [string ]any {
314
+ "agent" : map [string ]any {
315
+ "monitoring" : map [string ]any {
316
+ "metrics" : true ,
317
+ "http" : map [string ]any {
318
+ "enabled" : false ,
319
+ },
320
+ },
321
+ },
322
+ "outputs" : map [string ]any {
323
+ "default" : map [string ]any {},
324
+ },
325
+ },
326
+ expectedThreshold : sampleFiveErrorsStreamThreshold ,
327
+ },
328
+ {
329
+ name : "policy failure threshold uint" ,
330
+ monitoringCfg : & monitoringConfig {
331
+ C : & monitoringcfg.MonitoringConfig {
332
+ Enabled : true ,
333
+ MonitorMetrics : true ,
334
+ HTTP : & monitoringcfg.MonitoringHTTPConfig {
335
+ Enabled : false ,
336
+ },
337
+ FailureThreshold : & sampleFiveErrorsStreamThreshold ,
338
+ },
339
+ },
340
+ policy : map [string ]any {
341
+ "agent" : map [string ]any {
342
+ "monitoring" : map [string ]any {
343
+ "metrics" : true ,
344
+ "http" : map [string ]any {
345
+ "enabled" : false ,
346
+ },
347
+ failureThresholdKey : sampleTenErrorsStreamThreshold ,
348
+ },
349
+ },
350
+ "outputs" : map [string ]any {
351
+ "default" : map [string ]any {},
352
+ },
353
+ },
354
+ expectedThreshold : sampleTenErrorsStreamThreshold ,
355
+ },
356
+ {
357
+ name : "policy failure threshold int" ,
358
+ monitoringCfg : & monitoringConfig {
359
+ C : & monitoringcfg.MonitoringConfig {
360
+ Enabled : true ,
361
+ MonitorMetrics : true ,
362
+ HTTP : & monitoringcfg.MonitoringHTTPConfig {
363
+ Enabled : false ,
364
+ },
365
+ FailureThreshold : & sampleFiveErrorsStreamThreshold ,
366
+ },
367
+ },
368
+ policy : map [string ]any {
369
+ "agent" : map [string ]any {
370
+ "monitoring" : map [string ]any {
371
+ "metrics" : true ,
372
+ "http" : map [string ]any {
373
+ "enabled" : false ,
374
+ },
375
+ failureThresholdKey : 10 ,
376
+ },
377
+ },
378
+ "outputs" : map [string ]any {
379
+ "default" : map [string ]any {},
380
+ },
381
+ },
382
+ expectedThreshold : sampleTenErrorsStreamThreshold ,
383
+ },
384
+ {
385
+ name : "policy failure threshold string" ,
386
+ monitoringCfg : & monitoringConfig {
387
+ C : & monitoringcfg.MonitoringConfig {
388
+ Enabled : true ,
389
+ MonitorMetrics : true ,
390
+ HTTP : & monitoringcfg.MonitoringHTTPConfig {
391
+ Enabled : false ,
392
+ },
393
+ FailureThreshold : & sampleFiveErrorsStreamThreshold ,
394
+ },
395
+ },
396
+ policy : map [string ]any {
397
+ "agent" : map [string ]any {
398
+ "monitoring" : map [string ]any {
399
+ "metrics" : true ,
400
+ "http" : map [string ]any {
401
+ "enabled" : false ,
402
+ },
403
+ failureThresholdKey : "10" ,
404
+ },
405
+ },
406
+ "outputs" : map [string ]any {
407
+ "default" : map [string ]any {},
408
+ },
409
+ },
410
+ expectedThreshold : sampleTenErrorsStreamThreshold ,
411
+ },
412
+ }
413
+
414
+ for _ , tc := range tcs {
415
+
416
+ t .Run (tc .name , func (t * testing.T ) {
417
+ b := & BeatsMonitor {
418
+ enabled : true ,
419
+ config : tc .monitoringCfg ,
420
+ operatingSystem : runtime .GOOS ,
421
+ agentInfo : agentInfo ,
422
+ }
423
+ got , err := b .MonitoringConfig (tc .policy , nil , map [string ]string {"foobeat" : "filebeat" }, map [string ]uint64 {}) // put a componentID/binary mapping to have something in the beats monitoring input
424
+ assert .NoError (t , err )
425
+
426
+ rawInputs , ok := got ["inputs" ]
427
+ require .True (t , ok , "monitoring config contains no input" )
428
+ inputs , ok := rawInputs .([]any )
429
+ require .True (t , ok , "monitoring inputs are not a list" )
430
+ marshaledInputs , err := yaml .Marshal (inputs )
431
+ if assert .NoError (t , err , "error marshaling monitoring inputs" ) {
432
+ t .Logf ("marshaled monitoring inputs:\n %s\n " , marshaledInputs )
433
+ }
434
+
435
+ // loop over the created inputs
436
+ for _ , i := range inputs {
437
+ input , ok := i .(map [string ]any )
438
+ if assert .Truef (t , ok , "input is not represented as a map: %v" , i ) {
439
+ inputID := input ["id" ]
440
+ t .Logf ("input %q" , inputID )
441
+ // check the streams created for the input, should be a list of objects
442
+ if assert .Contains (t , input , "streams" , "input %q does not contain any stream" , inputID ) &&
443
+ assert .IsTypef (t , []any {}, input ["streams" ], "streams for input %q are not a list of objects" , inputID ) {
444
+
445
+ // loop over streams and cast to map[string]any to access keys
446
+ for _ , rawStream := range input ["streams" ].([]any ) {
447
+ if assert .IsTypef (t , map [string ]any {}, rawStream , "stream %v for input %q is not a map" , rawStream , inputID ) {
448
+ stream := rawStream .(map [string ]any )
449
+ // check period and assert its value
450
+ streamID := stream ["id" ]
451
+ if assert .Containsf (t , stream , failureThresholdKey , "stream %q for input %q does not contain a failureThreshold" , streamID , inputID ) &&
452
+ assert .IsType (t , uint (0 ), stream [failureThresholdKey ], "period for stream %q of input %q is not represented as a string" , streamID , inputID ) {
453
+ actualFailureThreshold := stream [failureThresholdKey ].(uint )
454
+ assert .Equalf (t , actualFailureThreshold , tc .expectedThreshold , "unexpected failure threshold for stream %q of input %q" , streamID , inputID )
455
+ }
456
+ }
457
+ }
458
+ }
459
+ }
460
+ }
461
+ })
462
+ }
463
+ }
464
+
261
465
func TestMonitoringConfigComponentFields (t * testing.T ) {
262
466
agentInfo , err := info .NewAgentInfo (context .Background (), false )
263
467
require .NoError (t , err , "Error creating agent info" )
0 commit comments