-
Notifications
You must be signed in to change notification settings - Fork 296
Closed
Description
Objective
Unify two nearly identical extractErrorMessage() functions into a single, optimized implementation.
Problem
Two duplicate implementations exist with different performance characteristics:
Location 1: pkg/workflow/metrics.go:462 (optimized - uses pre-compiled regex)
Location 2: pkg/cli/copilot_agent.go:297 (inefficient - compiles regex inline)
Approach
- Create a new shared utility file:
pkg/logger/error_formatting.go - Implement unified function using pre-compiled regex patterns (metrics.go version)
- Export the function as
ExtractErrorMessage(line string) string - Update both
pkg/workflow/metrics.goandpkg/cli/copilot_agent.goto import and use the new function - Remove the duplicate local implementations
Files to Modify
- Create:
pkg/logger/error_formatting.go - Update:
pkg/workflow/metrics.go(use new function, remove duplicate) - Update:
pkg/cli/copilot_agent.go(use new function, remove duplicate)
Implementation Details
Use the optimized version from metrics.go with pre-compiled patterns:
var (
timestampPattern1 = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}[T\s]\d{2}:\d{2}:\d{2}(\.\d+)?([+-]\d{2}:\d{2}|Z)?\s*`)
timestampPattern2 = regexp.MustCompile(`^\[\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\]\s*`)
timestampPattern3 = regexp.MustCompile(`^\d{2}:\d{2}:\d{2}\s*`)
logLevelPattern = regexp.MustCompile(`^(ERROR|WARN|WARNING|INFO|DEBUG):\s*`)
)
func ExtractErrorMessage(line string) string {
cleaned := timestampPattern1.ReplaceAllString(line, "")
cleaned = timestampPattern2.ReplaceAllString(cleaned, "")
cleaned = timestampPattern3.ReplaceAllString(cleaned, "")
cleaned = logLevelPattern.ReplaceAllString(cleaned, "")
return strings.TrimSpace(cleaned)
}Acceptance Criteria
- New
pkg/logger/error_formatting.gofile created with unified function - Both calling sites updated to use new function
- Duplicate implementations removed
- All tests pass (
make test) - Linting passes (
make lint) - No functionality changes (behavior identical to before)
Estimated Effort
1-2 hours
Related to #5677
AI generated by Plan Command for #5506
Reactions are currently unavailable