Skip to content

Commit d7e5001

Browse files
authored
sdk/log: Fix ExampleProcessor_redact to clone the record (#5559)
Towards #5065 Follow our own docs. From `Processor.Enabled` docs: > Before modifying a Record, the implementation must use Record.Clone to create a copy that shares no state with the original.
1 parent 4987a1d commit d7e5001

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

sdk/log/example_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ type RedactTokensProcessor struct {
3434

3535
// OnEmit redacts values from attributes containing "token" in the key
3636
// by replacing them with a REDACTED value.
37-
func (s *RedactTokensProcessor) OnEmit(ctx context.Context, record logsdk.Record) error {
37+
func (p *RedactTokensProcessor) OnEmit(ctx context.Context, record logsdk.Record) error {
38+
cloned := false
3839
record.WalkAttributes(func(kv log.KeyValue) bool {
3940
if strings.Contains(strings.ToLower(kv.Key), "token") {
41+
if !cloned {
42+
record = record.Clone()
43+
cloned = true
44+
}
4045
record.AddAttributes(log.String(kv.Key, "REDACTED"))
4146
}
4247
return true
4348
})
44-
return s.Processor.OnEmit(ctx, record)
49+
return p.Processor.OnEmit(ctx, record)
4550
}

0 commit comments

Comments
 (0)