acr-cli icon indicating copy to clipboard operation
acr-cli copied to clipboard

Migrate to Structured Logging with zerolog and Improve Logging for Manifest Purge Logic

Open Copilot opened this issue 7 months ago • 1 comments

This PR migrates the ACR CLI from fmt.Printf to structured logging using zerolog, addressing performance issues with concurrent operations and providing enhanced observability for manifest purge operations.

Changes Made

🚀 Core Infrastructure

  • Added zerolog dependency with centralized logger setup in internal/logger
  • New CLI flags:
    • --log-level (debug, info, warn, error) - defaults to info
    • --log-format (console, json) - defaults to console for CLI usability

🔍 Enhanced Manifest Purge Logging

The manifest purge logic now includes detailed structured logging explaining decision points:

Debug Level - Shows why each manifest is excluded:

{"level":"debug","repository":"myrepo","manifest":"sha256:abc...","reason":"has_tags","tag_count":3,"message":"Manifest excluded from purge - has remaining tags"}
{"level":"debug","repository":"myrepo","manifest":"sha256:def...","reason":"delete_disabled","message":"Manifest excluded from purge - deletion disabled by attributes"}

Info Level - Operation summaries:

{"level":"info","repository":"myrepo","candidate_count":15,"message":"Found candidate manifests for deletion"}
{"level":"info","repository":"myrepo","deleted_count":5,"attempted_count":7,"message":"Successfully completed manifest purge operation"}

Warn Level - 404 errors and skipped operations:

{"level":"warn","repository":"myrepo","manifest":"sha256:ghi...","status_code":404,"message":"Manifest not found during deletion, assuming already deleted"}

📊 Structured Context

All log entries include relevant structured fields:

  • repository: Repository being processed
  • manifest/tag: Artifact identification
  • reason: Decision justification (e.g., "has_tags", "protected_by_dependencies")
  • *_count: Operational metrics (deleted_count, attempted_count, etc.)
  • status_code: HTTP response tracking
  • dry_run: Operation mode indication

⚡ Performance & Compatibility

  • Zero-allocation logging: Minimal overhead using zerolog's efficient design
  • Backward compatible: All existing user output preserved (fmt.Printf kept for user-facing messages)
  • Non-blocking: Structured logging doesn't impact concurrent manifest operations
  • Test suite: All 56 existing tests continue to pass

Usage Examples

# Debug level for troubleshooting manifest purge decisions
acr --log-level debug purge -r registry --filter "repo:.*" --ago 7d --untagged

# JSON format for log aggregation systems
acr --log-level info --log-format json purge -r registry --filter "repo:.*" --ago 7d

# Default behavior unchanged (info level, console format)
acr purge -r registry --filter "repo:.*" --ago 7d

Files Modified

  • cmd/acr/root.go - Added logging flags and setup
  • cmd/acr/purge.go - Enhanced manifest purge logging
  • cmd/common/image_functions.go - Detailed manifest evaluation logging
  • internal/worker/purger.go - Structured deletion operations
  • internal/worker/annotator.go - Annotation operations
  • cmd/acr/annotate.go - Annotation workflow
  • cmd/acr/manifest.go - Manifest operations
  • internal/cssc/cssc.go - Filter results
  • docs/structured-logging.md - Comprehensive documentation

Benefits

  1. Improved debugging - Understand exactly why manifests are excluded from purge
  2. Better monitoring - Structured logs work with ELK, Splunk, Azure Monitor
  3. Enhanced performance - Non-locking concurrent-safe logging
  4. Operational visibility - Clear context for all registry operations

Fixes #470.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot avatar Jul 09 '25 00:07 Copilot