Skip to content

Commit

Permalink
Alertmanager: Add alerting logger (#8170)
Browse files Browse the repository at this point in the history
* Alertmanager: Add alerting logger

* make linter ignore unused functions in log.go

* license header
  • Loading branch information
santihernandezc authored May 23, 2024
1 parent 09c7650 commit 383d37a
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
58 changes: 58 additions & 0 deletions pkg/alertmanager/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: AGPL-3.0-only

package alertmanager

import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
alertingLogging "github.com/grafana/alerting/logging"
)

// alertingLogger implements the alertingLogging.Logger interface.
type alertingLogger struct {
kitLogger log.Logger
}

// newLoggerFactory returns a function that implements the alertingLogging.LoggerFactory interface.
//
//lint:ignore U1000 Ignore unused functions for now, they will be used to create the Grafana notifiers.
func newLoggerFactory(logger log.Logger) alertingLogging.LoggerFactory {
return func(loggerName string, ctx ...any) alertingLogging.Logger {
keyvals := append([]any{"logger", loggerName}, ctx...)
return &alertingLogger{kitLogger: log.With(logger, keyvals...)}
}
}

func (l *alertingLogger) New(ctx ...any) alertingLogging.Logger {
return &alertingLogger{log.With(l.kitLogger, ctx...)}
}

func (l *alertingLogger) Log(keyvals ...any) error {
return l.kitLogger.Log(keyvals...)
}

func (l *alertingLogger) Debug(msg string, ctx ...any) {
args := buildKeyvals(msg, ctx)
level.Debug(l.kitLogger).Log(args...)
}

func (l *alertingLogger) Info(msg string, ctx ...any) {
args := buildKeyvals(msg, ctx)
level.Info(l.kitLogger).Log(args...)
}

func (l *alertingLogger) Warn(msg string, ctx ...any) {
args := buildKeyvals(msg, ctx)
level.Warn(l.kitLogger).Log(args...)
}

func (l *alertingLogger) Error(msg string, ctx ...any) {
args := buildKeyvals(msg, ctx)
level.Error(l.kitLogger).Log(args...)
}

// buildKeyvals builds the keyvals for the log message.
// It adds "msg" and the message string as the first two elements.
func buildKeyvals(msg string, ctx []any) []any {
return append([]any{"msg", msg}, ctx...)
}
50 changes: 50 additions & 0 deletions pkg/alertmanager/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: AGPL-3.0-only

package alertmanager

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBuildKeyvals(t *testing.T) {
tests := []struct {
name string
msg string
ctx []any
want []any
}{
{
name: "empty context slice",
msg: "test message",
ctx: []any{},
want: []any{"msg", "test message"},
},
{
name: "nil context slice",
msg: "test message",
ctx: nil,
want: []any{"msg", "test message"},
},
{
name: "context slice with one element",
msg: "test message",
ctx: []any{"key1"},
want: []any{"msg", "test message", "key1"},
},
{
name: "context slice with two elements",
msg: "test message",
ctx: []any{"key1", "value1"},
want: []any{"msg", "test message", "key1", "value1"},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := buildKeyvals(test.msg, test.ctx)
require.Equal(t, test.want, got)
})
}
}

0 comments on commit 383d37a

Please sign in to comment.