Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log adapter in Prometheus receiver #1493

Merged
merged 6 commits into from
Aug 5, 2020
Merged
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
98 changes: 96 additions & 2 deletions receiver/prometheusreceiver/internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ package internal

import (
gokitLog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"go.uber.org/zap"
)

const (
levelKey = "level"
msgKey = "msg"
)

// NewZapToGokitLogAdapter create an adapter for zap.Logger to gokitLog.Logger
func NewZapToGokitLogAdapter(logger *zap.Logger) gokitLog.Logger {
// need to skip two levels in order to get the correct caller
Expand All @@ -31,15 +37,103 @@ type zapToGokitLogAdapter struct {
l *zap.SugaredLogger
}

type logData struct {
level level.Value
msg string
otherFields []interface{}
}

func (w *zapToGokitLogAdapter) Log(keyvals ...interface{}) error {
// expecting key value pairs, the number of items need to be even
if len(keyvals)%2 == 0 {
// expecting key value pairs, the number of items need to be even
w.l.Infow("", keyvals...)
// Extract log level and message and log them using corresponding zap function
ld := extractLogData(keyvals)
logFunc, err := levelToFunc(w.l, ld.level)
if err != nil {
return err
}
logFunc(ld.msg, ld.otherFields...)
} else {
// in case something goes wrong
w.l.Info(keyvals...)
}
return nil
}

func extractLogData(keyvals []interface{}) *logData {
lvl := level.InfoValue() // default
msg := ""

other := make([]interface{}, 0, len(keyvals))
for i := 0; i < len(keyvals); i = i + 2 {
key := keyvals[i]
val := keyvals[i+1]

if l, ok := matchLogLevel(key, val); ok {
lvl = l
continue
}

if m, ok := matchLogMessage(key, val); ok {
msg = m
continue
}

other = append(other, key, val)
}

return &logData{
level: lvl,
msg: msg,
otherFields: other,
}
}

// check if a given key-value pair represents go-kit log message and return it
func matchLogMessage(key interface{}, val interface{}) (string, bool) {
strKey, ok := key.(string)
if !ok || strKey != msgKey {
return "", false
}

msg, ok := val.(string)
if !ok {
return "", false
}

return msg, true
}

// check if a given key-value pair represents go-kit log level and return it
func matchLogLevel(key interface{}, val interface{}) (level.Value, bool) {
strKey, ok := key.(string)
if !ok || strKey != levelKey {
return nil, false
}

levelVal, ok := val.(level.Value)
if !ok {
return nil, false
}

return levelVal, true
}

// find a matching zap logging function to be used for a given level
func levelToFunc(logger *zap.SugaredLogger, lvl level.Value) (func(string, ...interface{}), error) {
switch lvl {
case level.DebugValue():
return logger.Debugw, nil
case level.InfoValue():
return logger.Infow, nil
case level.WarnValue():
return logger.Warnw, nil
case level.ErrorValue():
return logger.Errorw, nil
}

// default
return logger.Infof, nil
}

var _ gokitLog.Logger = (*zapToGokitLogAdapter)(nil)
197 changes: 197 additions & 0 deletions receiver/prometheusreceiver/internal/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package internal

import (
"testing"

"github.com/go-kit/kit/log/level"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestLog(t *testing.T) {
tcs := []struct {
name string
input []interface{}
wantLevel zapcore.Level
wantMessage string
}{
{
name: "Starting provider",
input: []interface{}{
"level",
level.DebugValue(),
"msg",
"Starting provider",
"provider",
"string/0",
"subs",
"[target1]",
},
wantLevel: zapcore.DebugLevel,
wantMessage: "Starting provider",
},
{
name: "Scrape failed",
input: []interface{}{
"level",
level.ErrorValue(),
"scrape_pool",
"target1",
"msg",
"Scrape failed",
"err",
"server returned HTTP status 500 Internal Server Error",
},
wantLevel: zapcore.ErrorLevel,
wantMessage: "Scrape failed",
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
conf := zap.NewProductionConfig()
conf.Level.SetLevel(zapcore.DebugLevel)

// capture zap log entry
var entry zapcore.Entry
h := func(e zapcore.Entry) error {
entry = e
return nil
}

logger, err := conf.Build(zap.Hooks(h))
require.NoError(t, err)

adapter := NewZapToGokitLogAdapter(logger)
err = adapter.Log(tc.input...)
require.NoError(t, err)

assert.Equal(t, tc.wantLevel, entry.Level)
assert.Equal(t, tc.wantMessage, entry.Message)
})
}
}

func TestExtractLogData(t *testing.T) {
tcs := []struct {
name string
input []interface{}
wantLevel level.Value
wantMessage string
wantOutput []interface{}
}{
{
name: "nil fields",
input: nil,
wantLevel: level.InfoValue(), // Default
wantMessage: "",
wantOutput: []interface{}{},
},
{
name: "empty fields",
input: []interface{}{},
wantLevel: level.InfoValue(), // Default
wantMessage: "",
wantOutput: []interface{}{},
},
{
name: "info level",
input: []interface{}{
"level",
level.InfoValue(),
},
wantLevel: level.InfoValue(),
wantMessage: "",
wantOutput: []interface{}{},
},
{
name: "warn level",
input: []interface{}{
"level",
level.WarnValue(),
},
wantLevel: level.WarnValue(),
wantMessage: "",
wantOutput: []interface{}{},
},
{
name: "error level",
input: []interface{}{
"level",
level.ErrorValue(),
},
wantLevel: level.ErrorValue(),
wantMessage: "",
wantOutput: []interface{}{},
},
{
name: "debug level + extra fields",
input: []interface{}{
"timestamp",
1596604719,
"level",
level.DebugValue(),
"msg",
"http client error",
},
wantLevel: level.DebugValue(),
wantMessage: "http client error",
wantOutput: []interface{}{
"timestamp",
1596604719,
},
},
{
name: "missing level field",
input: []interface{}{
"timestamp",
1596604719,
"msg",
"http client error",
},
wantLevel: level.InfoValue(), // Default
wantMessage: "http client error",
wantOutput: []interface{}{
"timestamp",
1596604719,
},
},
{
name: "invalid level type",
input: []interface{}{
"level",
"warn", // String is not recognized
},
wantLevel: level.InfoValue(), // Default
wantOutput: []interface{}{
"level",
"warn", // Field is preserved
},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to test the Log function directly imo. You could hook into zap logs and test what is actually logged. That would also get your coverage up 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nilebox maybe consider this in a followup PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already added a test with zap hooks actually:

I kept the original tests as well, because zap hook entries don't have custom fields so I can't assert them there unfortunately.

}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ld := extractLogData(tc.input)
assert.Equal(t, tc.wantLevel, ld.level)
assert.Equal(t, tc.wantMessage, ld.msg)
assert.Equal(t, tc.wantOutput, ld.otherFields)
})
}
}