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.go and pkg/cli/copilot_agent.go to 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
Estimated Effort
1-2 hours
Related to #5677
AI generated by Plan Command for #5506
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
pkg/logger/error_formatting.goExtractErrorMessage(line string) stringpkg/workflow/metrics.goandpkg/cli/copilot_agent.goto import and use the new functionFiles to Modify
pkg/logger/error_formatting.gopkg/workflow/metrics.go(use new function, remove duplicate)pkg/cli/copilot_agent.go(use new function, remove duplicate)Implementation Details
Use the optimized version from metrics.go with pre-compiled patterns:
Acceptance Criteria
pkg/logger/error_formatting.gofile created with unified functionmake test)make lint)Estimated Effort
1-2 hours
Related to #5677