opentelemetry-collector icon indicating copy to clipboard operation
opentelemetry-collector copied to clipboard

[service/telemetry] Add Configurable Log Rotation Support Using Lumberjack

Open bvsvas opened this issue 6 months ago • 18 comments

Description

This PR introduces optional log file rotation support to the OpenTelemetry Collector's internal telemetry logging system using the lumberjack log rolling library. The enhancement enables better control over log growth and retention without requiring external tools like logrotate.

Link to tracking issue

Issue# 10768

Fixes #

  • Log Rotation Enabled via Config:
    • New rotation block under service::telemetry::logs allows controlling rotation behavior:
service:
  telemetry:
    logs:
      output_paths: ["collector.log"]
      rotation:
        enabled: true
        max_megabytes: 100   # Max file size in MB before rotating (optional)
        max_backups: 3       # Max number of old files to retain (optional)
        max_age: 28          # Max days to keep old logs (optional)
        compress: true       # Whether to gzip old logs (optional)
  • Integrated via zap.Sink Registration:

    • A unique zap.Sink is registered for each log file using a UUID-prefixed lumberjack scheme.
    • This avoids conflicts during parallel test runs and ensures test isolation.
  • Dynamic Output Path Handling:

    • Applies rotation only if a valid file-based output_paths entry is provided.
    • Ignores console targets like "stdout", "stderr", and "console".

Note: Backward-compatible: If rotation.enabled is false or the block is omitted, log behavior remains unchanged. Only affects file-based logging, no impact on default stderr logging or console environments.

Testing

  • Unit tests added for:
    • newLogger with and without rotation enabled.
    • UUID-prefixed zap.Sink registration per test to avoid global zap sink collision.
    • Log file rotation behavior, file rollover validation.
  • Verified compatibility across platforms (including Windows).

Documentation

Documentation is part of LogsRotationConfig struct (refer to the v0.3.0.go file). Will have to find if there is any README.md should be updated.

bvsvas avatar May 24 '25 06:05 bvsvas