Skip to content

Commit

Permalink
sdk/log: Fix ExampleProcessor_redact to clone the record (#5559)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pellared authored Jul 1, 2024
1 parent 4987a1d commit d7e5001
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions sdk/log/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ type RedactTokensProcessor struct {

// OnEmit redacts values from attributes containing "token" in the key
// by replacing them with a REDACTED value.
func (s *RedactTokensProcessor) OnEmit(ctx context.Context, record logsdk.Record) error {
func (p *RedactTokensProcessor) OnEmit(ctx context.Context, record logsdk.Record) error {
cloned := false
record.WalkAttributes(func(kv log.KeyValue) bool {
if strings.Contains(strings.ToLower(kv.Key), "token") {
if !cloned {
record = record.Clone()
cloned = true
}
record.AddAttributes(log.String(kv.Key, "REDACTED"))
}
return true
})
return s.Processor.OnEmit(ctx, record)
return p.Processor.OnEmit(ctx, record)
}

0 comments on commit d7e5001

Please sign in to comment.