Skip to content

Commit 787420d

Browse files
author
Semih Serhat Karakaya
committed
[processor/redaction] apply redaction to log.body (open-telemetry#37239)
1 parent a204ecf commit 787420d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

processor/redactionprocessor/processor.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,25 @@ func (s *redaction) processResourceLog(ctx context.Context, rl plog.ResourceLogs
109109
for k := 0; k < ils.LogRecords().Len(); k++ {
110110
log := ils.LogRecords().At(k)
111111
s.processAttrs(ctx, log.Attributes())
112+
s.processLogBody(ctx, log.Body(), log.Attributes())
112113
}
113114
}
114115
}
115116

117+
func (s *redaction) processLogBody(ctx context.Context, body pcommon.Value, attributes pcommon.Map) {
118+
if body.Type() == pcommon.ValueTypeMap {
119+
s.processAttrs(ctx, body.Map())
120+
return
121+
}
122+
123+
maskedBody, redactionCount := s.processString(body.Str())
124+
// If redaction happened, update the body and add to attributes for the summary
125+
if redactionCount > 0 {
126+
body.SetStr(maskedBody)
127+
attributes.PutInt(bodyMaskedCount, int64(redactionCount))
128+
}
129+
}
130+
116131
func (s *redaction) processResourceMetric(ctx context.Context, rm pmetric.ResourceMetrics) {
117132
rsAttrs := rm.Resource().Attributes()
118133

@@ -238,6 +253,21 @@ func (s *redaction) addMetaAttrs(redactedAttrs []string, attributes pcommon.Map,
238253
}
239254
}
240255

256+
// processString applies regex-based redaction to a string and returns the modified string & count.
257+
func (s *redaction) processString(str string) (string, int) {
258+
maskedStr := str
259+
redactionCount := 0
260+
261+
// Apply redaction using regex patterns and count matches
262+
for _, compiledRE := range s.blockRegexList {
263+
matches := compiledRE.FindAllString(str, -1)
264+
redactionCount += len(matches)
265+
maskedStr = compiledRE.ReplaceAllString(maskedStr, "****")
266+
}
267+
268+
return str, redactionCount
269+
}
270+
241271
const (
242272
debug = "debug"
243273
info = "info"
@@ -246,6 +276,7 @@ const (
246276
maskedValues = "redaction.masked.keys"
247277
maskedValueCount = "redaction.masked.count"
248278
ignoredKeyCount = "redaction.ignored.count"
279+
bodyMaskedCount = "redaction.masked.count"
249280
)
250281

251282
// makeAllowList sets up a lookup table of allowed span attribute keys

0 commit comments

Comments
 (0)