Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"

"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
)

type Writer interface {
Expand Down Expand Up @@ -57,7 +58,8 @@ func NewZerologMetrics(enabled bool, target string, containerID string) Writer {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixNano
file, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return nil
logrus.Warnf("Failed to open metrics file %s: %v. Metrics will be disabled.", target, err)
return &mockWriter{}
}
logger := zerolog.New(file).Level(zerolog.InfoLevel).With().Timestamp().Logger()
return &zerologMetrics{
Expand Down
12 changes: 12 additions & 0 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ func TestZerologMetricsMetadata(t *testing.T) {
t.Errorf("timestampOrder = %v, want 0", got)
}
}

func TestZerologMetricsInvalidFileDoesNotPanic(t *testing.T) {
// Provide a path to a directory that definitely does not exist
invalidPath := "/does/not/exist/timestamps.log"

// Create the metrics writer with timestamps enabled but an invalid path
writer := NewZerologMetrics(true, invalidPath, "container-test")

// This call would panic with a nil pointer dereference without the fix.
Comment thread
abhaygoudannavar marked this conversation as resolved.
// With the fix, it will safely execute the mockWriter's no-op Capture().
writer.Capture(TS00)
}