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

Fix log adapter in Prometheus receiver #1493

merged 6 commits into from
Aug 5, 2020

Conversation

nilebox
Copy link
Member

@nilebox nilebox commented Aug 5, 2020

Description:
Fixing a bug:

  • deduplicate the "level" field in log messages coming from Prometheus
  • use appropriate zap log level rather than always using Info

Link to tracking Issue: Fixes #1486

Testing: Added unit tests

Documentation: -

@codecov
Copy link

codecov bot commented Aug 5, 2020

Codecov Report

Merging #1493 into master will increase coverage by 0.02%.
The diff coverage is 86.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1493      +/-   ##
==========================================
+ Coverage   90.98%   91.00%   +0.02%     
==========================================
  Files         239      239              
  Lines       16744    16793      +49     
==========================================
+ Hits        15235    15283      +48     
- Misses       1080     1083       +3     
+ Partials      429      427       -2     
Impacted Files Coverage Δ
receiver/prometheusreceiver/internal/logger.go 84.48% <86.00%> (+6.70%) ⬆️
receiver/otlpreceiver/otlp.go 94.44% <0.00%> (+2.77%) ⬆️
translator/internaldata/resource_to_oc.go 87.50% <0.00%> (+4.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8af0a90...427557e. Read the comment docs.

Collector automation moved this from In progress to Reviewer approved Aug 5, 2020
Copy link
Member

@james-bebbington james-bebbington left a comment

Choose a reason for hiding this comment

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

LGTM. A few minor suggestions

// a corresponding zap log level
func matchLogLevel(key interface{}, val interface{}) (*zapcore.Level, bool) {
strKey, ok := key.(string)
if !ok || strKey != "level" {
Copy link
Member

Choose a reason for hiding this comment

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

Move to const?

} else {
// in case something goes wrong
w.l.Info(keyvals...)
}
return nil
}

// extract go-kit log level from key value pairs, convert it to zap log level
// and remove from the list to avoid duplication in log message
Copy link
Member

Choose a reason for hiding this comment

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

Is it idiomatic to start the comment with the function name even for non-exported functions? I've tended to do that anyway, but maybe it's not important

func toZapLevel(value level.Value) zapcore.Level {
// See https://github.com/go-kit/kit/blob/556100560949062d23fe05d9fda4ce173c30c59f/log/level/level.go#L184-L187
switch value.String() {
case "error":
Copy link
Member

Choose a reason for hiding this comment

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

Make these all consts. Also consider just returning the zap logging functions here (could make this a type) so you don't need the levelToFunc function below

case zapcore.ErrorLevel:
return logger.Errorf, nil
}
return nil, fmt.Errorf("unrecognized level: %q", lvl)
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be more consistent with the other code if this just returned Infof

"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.

"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.

@nilebox maybe consider this in a followup PR

@bogdandrutu bogdandrutu merged commit 3751cec into open-telemetry:master Aug 5, 2020
Collector automation moved this from Reviewer approved to Done Aug 5, 2020
@nilebox nilebox deleted the prometheus-logger-fix branch August 5, 2020 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Collector
  
Done
Development

Successfully merging this pull request may close these issues.

Prometheus receiver prints "level" field twice in log messages
3 participants