Skip to content

[plan] Consolidate extractErrorMessage() duplicate implementations #5678

@github-actions

Description

@github-actions

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

  1. Create a new shared utility file: pkg/logger/error_formatting.go
  2. Implement unified function using pre-compiled regex patterns (metrics.go version)
  3. Export the function as ExtractErrorMessage(line string) string
  4. Update both pkg/workflow/metrics.go and pkg/cli/copilot_agent.go to import and use the new function
  5. 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.go file 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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions