Skip to content

fix(metrics): fallback to mock writer on file open failure to prevent panic#596

Open
abhaygoudannavar wants to merge 2 commits intourunc-dev:mainfrom
abhaygoudannavar:one/metrics-file
Open

fix(metrics): fallback to mock writer on file open failure to prevent panic#596
abhaygoudannavar wants to merge 2 commits intourunc-dev:mainfrom
abhaygoudannavar:one/metrics-file

Conversation

@abhaygoudannavar
Copy link
Copy Markdown

Description

This PR fixes a critical nil pointer dereference crash in the metrics initialization process.
When [timestamps] are enabled in the /etc/urunc/config.toml configuration but the target file path is invalid (e.g., the parent directory does not exist or has bad permissions), NewZerologMetrics previously returned nil. Because the caller in main.go assigned this directly to the global metrics variable without a nil check, the very first metrics.Capture() call resulted in a silent crash on startup.

Changes included:

  • Graceful Degradation: NewZerologMetrics now catches the file open error, logs a warning via logrus, and returns a safe, no-op mockWriter instead of nil.
  • Regression Test: Added TestZerologMetricsInvalidFileDoesNotPanic to metrics_test.go to explicitly test the invalid path scenario and ensure Capture() can be called safely without panicking.

Screenshots

  • Before (Crash on invalid file path):
Screenshot 2026-05-01 150215
  • After (Graceful degradation and test passing):
Screenshot 2026-05-01 145812

Fixes: #595

LLM usage

No LLM usage

Checklist

  • Local linter passes (make lint)
  • Local unit tests pass (make unittest)
  • Commits are signed-off (git commit -s)
  • Commit messages follow Conventional Commits formatting

When timestamps are enabled but the target file cannot be opened
(e.g. missing directory), NewZerologMetrics returned nil. This
caused a nil pointer dereference on subsequent metrics.Capture() calls.
This fix gracefully degrades to a no-op mockWriter and logs a warning.

Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com>
Added TestZerologMetricsInvalidFileDoesNotPanic to ensure that if
the metrics log file cannot be opened, it gracefully degrades
instead of causing a nil pointer dereference.

Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented May 1, 2026

Deploy Preview for urunc ready!

Name Link
🔨 Latest commit 51ba5c5
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/69f47543d89a6e0008621cb1
😎 Deploy Preview https://deploy-preview-596--urunc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@abhaygoudannavar
Copy link
Copy Markdown
Author

@cmainas can you take a look on the PR...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(metrics): NewZerologMetrics returns nil on file open failure causing panic

1 participant