Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVPROD-11650 Don't recompute redactions for every Send invocation during test ingestion #8796

Merged
merged 5 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions agent/internal/redactor/redacting_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type redactingSender struct {
expansions *util.DynamicExpansions
expansionsToRedact []string
internalRedactions *util.DynamicExpansions
allRedacted []util.RedactInfo

send.Sender
}
Expand All @@ -35,6 +36,13 @@ type RedactionOptions struct {
// secrets). All values in InternalRedactions are assumed to be
// sensitive and are replaced by their key.
InternalRedactions *util.DynamicExpansions
// PreloadRedactions indicates whether the redacting sender will need to compute a
// full list of redacted key-value pairs on initialization, or on every
// sender.Send invocation. This should not be set if the redacting sender
// is logging during task runtime, because expansions can change over the course
// of task runtime. It can only be safely set if we're confident that expansions
// will no longer be changing.
PreloadRedactions bool
}

func (r *redactingSender) Send(m message.Composer) {
Expand All @@ -43,21 +51,14 @@ func (r *redactingSender) Send(m message.Composer) {
}

msg := m.String()
allRedacted := r.expansions.GetRedacted()
for _, expansion := range r.expansionsToRedact {
if val := r.expansions.Get(expansion); val != "" {
allRedacted = append(allRedacted, util.RedactInfo{Key: expansion, Value: val})
}

var allRedacted []util.RedactInfo
if len(r.allRedacted) > 0 {
allRedacted = r.allRedacted
} else {
allRedacted = getAllRedacted(r.expansions, r.internalRedactions, r.expansionsToRedact)
}
r.internalRedactions.Range(func(k, v string) bool {
allRedacted = append(allRedacted, util.RedactInfo{Key: k, Value: v})
return true
})

// Sort redacted info based on value length to ensure we're redacting longer values first.
sort.Slice(allRedacted, func(i, j int) bool {
return len(allRedacted[i].Value) > len(allRedacted[j].Value)
})
for _, info := range allRedacted {
msg = strings.ReplaceAll(msg, info.Value, fmt.Sprintf(redactedVariableTemplate, info.Key))
}
Expand All @@ -74,10 +75,34 @@ func NewRedactingSender(sender send.Sender, opts RedactionOptions) send.Sender {
if opts.InternalRedactions == nil {
opts.InternalRedactions = &util.DynamicExpansions{}
}
return &redactingSender{

redacter := &redactingSender{
expansions: opts.Expansions,
expansionsToRedact: append(opts.Redacted, globals.ExpansionsToRedact...),
internalRedactions: opts.InternalRedactions,
Sender: sender,
}
if opts.PreloadRedactions {
redacter.allRedacted = getAllRedacted(redacter.expansions, redacter.internalRedactions, redacter.expansionsToRedact)
}
return redacter
}

func getAllRedacted(expansions, internalRedactions *util.DynamicExpansions, expansionsToRedact []string) []util.RedactInfo {
allRedacted := expansions.GetRedacted()
for _, expansion := range expansionsToRedact {
if val := expansions.Get(expansion); val != "" {
allRedacted = append(allRedacted, util.RedactInfo{Key: expansion, Value: val})
}
}
internalRedactions.Range(func(k, v string) bool {
allRedacted = append(allRedacted, util.RedactInfo{Key: k, Value: v})
return true
})

// Sort redacted info based on value length to ensure we're redacting longer values first.
sort.Slice(allRedacted, func(i, j int) bool {
return len(allRedacted[i].Value) > len(allRedacted[j].Value)
})
return allRedacted
}
2 changes: 2 additions & 0 deletions agent/internal/taskoutput/test_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func newTestLogDirectoryHandler(dir string, output *taskoutput.TaskOutput, taskO
if err != nil {
return nil, errors.Wrap(err, "making test log sender")
}
// This flag exists to improve the performance of test log ingestion.
redactionOpts.PreloadRedactions = true
return redactor.NewRedactingSender(evgSender, redactionOpts), nil
}

Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (

// Agent version to control agent rollover. The format is the calendar date
// (YYYY-MM-DD).
AgentVersion = "2025-03-11-a"
AgentVersion = "2025-03-11-b"
)

const (
Expand Down
Loading