Skip to content

Commit

Permalink
fix comments and append name
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpionknifes committed Jul 29, 2024
1 parent 80690be commit beae842
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 9 additions & 5 deletions bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
// to [log.Int64Value] or [log.StringValue] if the value is too large.
// - [float32], [float64] are transformed to [log.Float64Value].
// - [time.Duration] are transformed to [log.Int64Value] with the nanoseconds.
// - [complex64], [complex128] are transformed to [log.StringValue] with the
// - [complex64], [complex128] are transformed to [log.MapValue] with the keys
// "r" and "i" for the real and imaginary parts. The values are
// [log.Float64Value].
// - [time.Time] are transformed to [log.Int64Value] with the nanoseconds.
// - [[]byte] are transformed to [log.BytesValue].
// - [error] are transformed to [log.StringValue] with the error message.
Expand Down Expand Up @@ -75,8 +77,8 @@ import (
)

const (
// exceptionMessage is the key used for the error message.
exceptionMessage = "exception.message"
// exceptionMessageKey is the key used for the error message.
exceptionMessageKey = "exception.message"
)

type config struct {
Expand Down Expand Up @@ -180,6 +182,7 @@ func WithLevelSeverity(f func(int) log.Severity) Option {
func NewLogSink(name string, options ...Option) *LogSink {
c := newConfig(options)
return &LogSink{
name: name,
newLogger: c.logger,
logger: c.logger(name),
levelSeverity: c.levelSeverity,
Expand All @@ -192,6 +195,7 @@ type LogSink struct {
// Ensure forward compatibility by explicitly making this not comparable.
noCmp [0]func() //nolint: unused // This is indeed used.

name string
newLogger func(name string) log.Logger
logger log.Logger
levelSeverity func(int) log.Severity
Expand All @@ -210,7 +214,7 @@ func (l *LogSink) log(err error, msg string, serverity log.Severity, kvList ...a
if err != nil {
record.AddAttributes(
log.KeyValue{
Key: exceptionMessage,
Key: exceptionMessageKey,
Value: convertValue(err),
},
)
Expand Down Expand Up @@ -258,7 +262,7 @@ func (l *LogSink) Init(info logr.RuntimeInfo) {

// WithName returns a new LogSink with the specified name appended.
func (l LogSink) WithName(name string) logr.LogSink {
l.logger = l.newLogger(name)
l.logger = l.newLogger(l.name + "/" + name)
return &l
}

Expand Down
6 changes: 3 additions & 3 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestNewLogSinkConfiguration(t *testing.T) {
assert.NotNil(t, lsWithName)
assert.NotEqual(t, ls, lsWithName)
assert.Equal(t, ls.newLogger("name"), ls.logger)
assert.Equal(t, ls.newLogger("test"), lsWithName.(*LogSink).logger)
assert.Equal(t, ls.newLogger("name/test"), lsWithName.(*LogSink).logger)
})
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func TestLogSink(t *testing.T) {
Body: log.StringValue("error message"),
Severity: log.SeverityError,
Attributes: []log.KeyValue{
log.String(exceptionMessage, "test error"),
log.String(exceptionMessageKey, "test error"),
},
},
},
Expand All @@ -171,7 +171,7 @@ func TestLogSink(t *testing.T) {
Body: log.StringValue("msg"),
Severity: log.SeverityError,
Attributes: []log.KeyValue{
log.String(exceptionMessage, "test error"),
log.String(exceptionMessageKey, "test error"),
log.String("struct", "{data:1}"),
log.Bool("bool", true),
log.Int64("duration", 60_000_000_000),
Expand Down

0 comments on commit beae842

Please sign in to comment.