-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
bogdandrutu
merged 6 commits into
open-telemetry:master
from
nilebox:prometheus-logger-fix
Aug 5, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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 | ||
}, | ||
}, | ||
} | ||
|
||
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) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😄There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
opentelemetry-collector/receiver/prometheusreceiver/internal/logger_test.go
Line 78 in 8b55cb6
I kept the original tests as well, because zap hook entries don't have custom fields so I can't assert them there unfortunately.