@@ -35,9 +35,10 @@ func TestRedactUnknownAttributes(t *testing.T) {
35
35
redacted := map [string ]pcommon.Value {
36
36
"credit_card" : pcommon .NewValueStr ("4111111111111111" ),
37
37
}
38
+ logBody := pcommon .NewValueStr ("first-batch-first-logEntry" )
38
39
39
40
outTraces := runTest (t , allowed , redacted , nil , nil , ignored , config )
40
- outLogs := runLogsTest (t , allowed , redacted , nil , nil , ignored , config )
41
+ outLogs := runLogsTest (t , allowed , redacted , nil , nil , ignored , logBody , config )
41
42
outMetricsGauge := runMetricsTest (t , allowed , redacted , nil , nil , ignored , config , pmetric .MetricTypeGauge )
42
43
outMetricsSum := runMetricsTest (t , allowed , redacted , nil , nil , ignored , config , pmetric .MetricTypeSum )
43
44
outMetricsHistogram := runMetricsTest (t , allowed , redacted , nil , nil , ignored , config , pmetric .MetricTypeHistogram )
@@ -80,9 +81,10 @@ func TestAllowAllKeys(t *testing.T) {
80
81
"id" : pcommon .NewValueInt (5 ),
81
82
"name" : pcommon .NewValueStr ("placeholder" ),
82
83
}
84
+ logBody := pcommon .NewValueStr ("first-batch-first-logEntry" )
83
85
84
86
outTraces := runTest (t , allowed , nil , nil , nil , nil , config )
85
- outLogs := runLogsTest (t , allowed , nil , nil , nil , nil , config )
87
+ outLogs := runLogsTest (t , allowed , nil , nil , nil , nil , logBody , config )
86
88
outMetricsGauge := runMetricsTest (t , allowed , nil , nil , nil , nil , config , pmetric .MetricTypeGauge )
87
89
outMetricsSum := runMetricsTest (t , allowed , nil , nil , nil , nil , config , pmetric .MetricTypeSum )
88
90
outMetricsHistogram := runMetricsTest (t , allowed , nil , nil , nil , nil , config , pmetric .MetricTypeHistogram )
@@ -131,9 +133,10 @@ func TestAllowAllKeysMaskValuesAllowValues(t *testing.T) {
131
133
"credit_card2" : pcommon .NewValueStr ("placeholder 4111111111111112" ),
132
134
"email" :
pcommon .
NewValueStr (
"[email protected] " ),
133
135
}
136
+ logBody := pcommon .NewValueStr ("first-batch-first-logEntry" )
134
137
135
138
outTraces := runTest (t , allowed , nil , masked , allowedValues , nil , config )
136
- outLogs := runLogsTest (t , allowed , nil , masked , allowedValues , nil , config )
139
+ outLogs := runLogsTest (t , allowed , nil , masked , allowedValues , nil , logBody , config )
137
140
outMetricsGauge := runMetricsTest (t , allowed , nil , nil , masked , allowedValues , config , pmetric .MetricTypeGauge )
138
141
outMetricsSum := runMetricsTest (t , allowed , nil , masked , allowedValues , nil , config , pmetric .MetricTypeSum )
139
142
outMetricsHistogram := runMetricsTest (t , allowed , nil , masked , allowedValues , nil , config , pmetric .MetricTypeHistogram )
@@ -197,9 +200,10 @@ func TestRedactSummaryDebug(t *testing.T) {
197
200
allowedValues := map [string ]pcommon.Value {
198
201
"email" :
pcommon .
NewValueStr (
"[email protected] " ),
199
202
}
203
+ logBody := pcommon .NewValueStr ("placeholder 4111111111111111" )
200
204
201
205
outTraces := runTest (t , allowed , redacted , masked , allowedValues , ignored , config )
202
- outLogs := runLogsTest (t , allowed , redacted , masked , allowedValues , ignored , config )
206
+ outLogs := runLogsTest (t , allowed , redacted , masked , allowedValues , ignored , logBody , config )
203
207
outMetricsGauge := runMetricsTest (t , allowed , redacted , masked , allowedValues , ignored , config , pmetric .MetricTypeGauge )
204
208
outMetricsSum := runMetricsTest (t , allowed , redacted , masked , allowedValues , ignored , config , pmetric .MetricTypeSum )
205
209
outMetricsHistogram := runMetricsTest (t , allowed , redacted , masked , allowedValues , ignored , config , pmetric .MetricTypeHistogram )
@@ -251,6 +255,12 @@ func TestRedactSummaryDebug(t *testing.T) {
251
255
value , _ = attr .Get ("email" )
252
256
assert .
Equal (
t ,
"[email protected] " ,
value .
Str ())
253
257
}
258
+
259
+ outLog := outLogs .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 )
260
+ assert .Equal (t , "placeholder ****" , outLog .Body ().Str ())
261
+ outLogBodyMaskedCount , ok := outLog .Attributes ().Get (bodyMaskedCount )
262
+ assert .True (t , ok )
263
+ assert .Equal (t , int64 (1 ), outLogBodyMaskedCount .Int ())
254
264
}
255
265
256
266
// TestRedactSummaryInfo validates that the processor writes a verbose summary
@@ -280,9 +290,10 @@ func TestRedactSummaryInfo(t *testing.T) {
280
290
allowedValues := map [string ]pcommon.Value {
281
291
"email" :
pcommon .
NewValueStr (
"[email protected] " ),
282
292
}
293
+ logBody := pcommon .NewValueStr ("placeholder 4111111111111111" )
283
294
284
295
outTraces := runTest (t , allowed , redacted , masked , allowedValues , ignored , config )
285
- outLogs := runLogsTest (t , allowed , redacted , masked , allowedValues , ignored , config )
296
+ outLogs := runLogsTest (t , allowed , redacted , masked , allowedValues , ignored , logBody , config )
286
297
outMetricsGauge := runMetricsTest (t , allowed , redacted , masked , allowedValues , ignored , config , pmetric .MetricTypeGauge )
287
298
outMetricsSum := runMetricsTest (t , allowed , redacted , masked , allowedValues , ignored , config , pmetric .MetricTypeSum )
288
299
outMetricsHistogram := runMetricsTest (t , allowed , redacted , masked , allowedValues , ignored , config , pmetric .MetricTypeHistogram )
@@ -332,6 +343,12 @@ func TestRedactSummaryInfo(t *testing.T) {
332
343
value , _ = attr .Get ("safe_attribute" )
333
344
assert .Equal (t , "harmless but suspicious 4111111111111141" , value .Str ())
334
345
}
346
+
347
+ outLog := outLogs .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 )
348
+ assert .Equal (t , "placeholder ****" , outLog .Body ().Str ())
349
+ outLogBodyMaskedCount , ok := outLog .Attributes ().Get (bodyMaskedCount )
350
+ assert .True (t , ok )
351
+ assert .Equal (t , int64 (1 ), outLogBodyMaskedCount .Int ())
335
352
}
336
353
337
354
// TestRedactSummarySilent validates that the processor does not create the
@@ -351,9 +368,10 @@ func TestRedactSummarySilent(t *testing.T) {
351
368
redacted := map [string ]pcommon.Value {
352
369
"credit_card" : pcommon .NewValueStr ("4111111111111111" ),
353
370
}
371
+ logBody := pcommon .NewValueStr ("placeholder 4111111111111111" )
354
372
355
- outTraces := runTest (t , allowed , redacted , masked , nil , nil , config )
356
- outLogs := runLogsTest (t , allowed , redacted , masked , nil , nil , config )
373
+ outTraces := runTest (t , allowed , nil , masked , nil , nil , config )
374
+ outLogs := runLogsTest (t , allowed , nil , masked , nil , nil , logBody , config )
357
375
outMetricsGauge := runMetricsTest (t , allowed , redacted , nil , masked , nil , config , pmetric .MetricTypeGauge )
358
376
outMetricsSum := runMetricsTest (t , allowed , redacted , masked , nil , nil , config , pmetric .MetricTypeSum )
359
377
outMetricsHistogram := runMetricsTest (t , allowed , redacted , masked , nil , nil , config , pmetric .MetricTypeHistogram )
@@ -386,12 +404,20 @@ func TestRedactSummarySilent(t *testing.T) {
386
404
value , _ := attr .Get ("name" )
387
405
assert .Equal (t , "placeholder ****" , value .Str ())
388
406
}
407
+
408
+ outLog := outLogs .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 )
409
+ assert .Equal (t , "placeholder ****" , outLog .Body ().Str ())
410
+ _ , ok := outLog .Attributes ().Get (bodyMaskedCount )
411
+ assert .False (t , ok )
389
412
}
390
413
391
414
// TestRedactSummaryDefault validates that the processor does not create the
392
415
// summary attributes by default
393
416
func TestRedactSummaryDefault (t * testing.T ) {
394
- config := & Config {AllowedKeys : []string {"id" , "name" , "group" }}
417
+ config := & Config {
418
+ AllowedKeys : []string {"id" , "name" , "group" },
419
+ BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" },
420
+ }
395
421
allowed := map [string ]pcommon.Value {
396
422
"id" : pcommon .NewValueInt (5 ),
397
423
}
@@ -401,9 +427,10 @@ func TestRedactSummaryDefault(t *testing.T) {
401
427
masked := map [string ]pcommon.Value {
402
428
"name" : pcommon .NewValueStr ("placeholder 4111111111111111" ),
403
429
}
430
+ logBody := pcommon .NewValueStr ("placeholder 4111111111111111" )
404
431
405
- outTraces := runTest (t , allowed , nil , masked , nil , ignored , config )
406
- outLogs := runLogsTest (t , allowed , nil , masked , nil , ignored , config )
432
+ outTraces := runTest (t , allowed , nil , masked , nil , nil , config )
433
+ outLogs := runLogsTest (t , allowed , nil , masked , nil , nil , logBody , config )
407
434
outMetricsGauge := runMetricsTest (t , allowed , nil , masked , nil , ignored , config , pmetric .MetricTypeGauge )
408
435
outMetricsSum := runMetricsTest (t , allowed , nil , masked , nil , ignored , config , pmetric .MetricTypeSum )
409
436
outMetricsHistogram := runMetricsTest (t , allowed , nil , masked , nil , ignored , config , pmetric .MetricTypeHistogram )
@@ -431,7 +458,14 @@ func TestRedactSummaryDefault(t *testing.T) {
431
458
assert .False (t , ok )
432
459
_ , ok = attr .Get (ignoredKeyCount )
433
460
assert .False (t , ok )
461
+ value , _ := attr .Get ("name" )
462
+ assert .Equal (t , "placeholder ****" , value .Str ())
434
463
}
464
+
465
+ outLog := outLogs .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 )
466
+ assert .Equal (t , "placeholder ****" , outLog .Body ().Str ())
467
+ _ , ok := outLog .Attributes ().Get (bodyMaskedCount )
468
+ assert .False (t , ok )
435
469
}
436
470
437
471
// TestMultipleBlockValues validates that the processor can block multiple
@@ -452,9 +486,10 @@ func TestMultipleBlockValues(t *testing.T) {
452
486
redacted := map [string ]pcommon.Value {
453
487
"credit_card" : pcommon .NewValueStr ("4111111111111111" ),
454
488
}
489
+ logBody := pcommon .NewValueStr ("placeholder 4111111111111111 52000" )
455
490
456
491
outTraces := runTest (t , allowed , redacted , masked , nil , nil , config )
457
- outLogs := runLogsTest (t , allowed , redacted , masked , nil , nil , config )
492
+ outLogs := runLogsTest (t , allowed , redacted , masked , nil , nil , logBody , config )
458
493
outMetricsGauge := runMetricsTest (t , allowed , redacted , masked , nil , nil , config , pmetric .MetricTypeGauge )
459
494
outMetricsSum := runMetricsTest (t , allowed , redacted , masked , nil , nil , config , pmetric .MetricTypeSum )
460
495
outMetricsHistogram := runMetricsTest (t , allowed , redacted , masked , nil , nil , config , pmetric .MetricTypeHistogram )
@@ -500,6 +535,12 @@ func TestMultipleBlockValues(t *testing.T) {
500
535
assert .Equal (t , "placeholder **** ****" , nameValue .Str ())
501
536
assert .Equal (t , "mystery ****" , mysteryValue .Str ())
502
537
}
538
+
539
+ outLog := outLogs .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 )
540
+ assert .Equal (t , "placeholder **** ****" , outLog .Body ().Str ())
541
+ outLogBodyMaskedCount , ok := outLog .Attributes ().Get (bodyMaskedCount )
542
+ assert .True (t , ok )
543
+ assert .Equal (t , int64 (2 ), outLogBodyMaskedCount .Int ())
503
544
}
504
545
505
546
// TestProcessAttrsAppliedTwice validates a use case when data is coming through redaction processor more than once.
@@ -601,6 +642,7 @@ func runLogsTest(
601
642
masked map [string ]pcommon.Value ,
602
643
allowedValues map [string ]pcommon.Value ,
603
644
ignored map [string ]pcommon.Value ,
645
+ body pcommon.Value ,
604
646
config * Config ,
605
647
) plog.Logs {
606
648
inBatch := plog .NewLogs ()
@@ -611,7 +653,7 @@ func runLogsTest(
611
653
library := ils .Scope ()
612
654
library .SetName ("first-library" )
613
655
logEntry := ils .LogRecords ().AppendEmpty ()
614
- logEntry .Body (). SetStr ( "first-batch-first-logEntry" )
656
+ body . CopyTo ( logEntry .Body ())
615
657
logEntry .SetTraceID ([16 ]byte {1 , 2 , 3 , 4 })
616
658
617
659
length := len (allowed ) + len (masked ) + len (redacted ) + len (ignored ) + len (allowedValues )
0 commit comments