-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alertmanager: Add alerting logger (#8170)
* Alertmanager: Add alerting logger * make linter ignore unused functions in log.go * license header
- Loading branch information
1 parent
09c7650
commit 383d37a
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |