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 #116 - sub millisecond duration should now be measured by default #117

Merged
merged 5 commits into from Dec 14, 2017
Merged

Fix #116 - sub millisecond duration should now be measured by default #117

merged 5 commits into from Dec 14, 2017

Conversation

domgreen
Copy link
Contributor

@domgreen domgreen commented Dec 6, 2017

Currently issue #116 is raised due to sub ms durations being logged as 0ms we can fix this by casting the duration to a float before we do the division.

This change will mean that if your grpc.time_ms is greater than one it will be represented as a whole number of ms 10 otherwise it will be displayed to 3 decimal places 0.542.

@domgreen domgreen changed the title fixes #116 Fix #116 - sub millisecond duration should now be measured by default Dec 6, 2017
@codecov-io
Copy link

codecov-io commented Dec 6, 2017

Codecov Report

Merging #117 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #117   +/-   ##
=======================================
  Coverage   73.06%   73.06%           
=======================================
  Files          36       36           
  Lines        1277     1277           
=======================================
  Hits          933      933           
  Misses        295      295           
  Partials       49       49
Impacted Files Coverage Δ
logging/logrus/options.go 47.22% <100%> (ø) ⬆️
logging/zap/options.go 47.22% <100%> (ø) ⬆️
logging/logrus/server_interceptors.go 91.54% <100%> (ø) ⬆️
logging/zap/server_interceptors.go 100% <100%> (ø) ⬆️

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 93790b4...0554149. Read the comment docs.

func TestDurationToTimeMillisField(t *testing.T) {
val := DurationToTimeMillisField(time.Microsecond * 100)
assert.Equal(t, val.Type, zapcore.Float32Type, "should be a float type")
assert.Equal(t, math.Float32frombits(uint32(val.Integer)), float32(0.1), "sub millisecond values should be correct")
Copy link
Collaborator

Choose a reason for hiding this comment

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

math.Float32frombits(uint32( feels like it's relying on knowledge of zap internals, is there any other way we can get the processed value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a tricky one to figure out, wasnt sure of it myself tbh but couldn't think of a way to do it without knowing how zapFlags worked.
If we had #114 merged we could probably grab it off the log line in an interceptor test and assert its not 0 therefore not testing this method directly.


func TestDurationToTimeMillisField(t *testing.T) {
_, val := DurationToTimeMillisField(time.Microsecond * 100)
assert.Equal(t, val.(float32), float32(0.1), "sub millisecond values should be correct")

Choose a reason for hiding this comment

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

This check is flaky I believe.

func TestDurationToTimeMillisField(t *testing.T) {
val := DurationToTimeMillisField(time.Microsecond * 100)
assert.Equal(t, val.Type, zapcore.Float32Type, "should be a float type")
assert.Equal(t, math.Float32frombits(uint32(val.Integer)), float32(0.1), "sub millisecond values should be correct")

Choose a reason for hiding this comment

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

This check is flaky as well.

@domgreen
Copy link
Contributor Author

domgreen commented Dec 7, 2017

The alternative for these tests now #114 is merged is to do:

assert.Contains(s.T(), msgs[1], "grpc.time_ms", "interceptor log statement should contain execution time")
	assert.NotZero(s.T(), msgs[1]["grpc.time_ms"].(float64), "duration of call should be greater than zero")

@devnev @artsiukhou @mwitkow what do you think?

@domgreen
Copy link
Contributor Author

domgreen commented Dec 7, 2017

Have updated the PR so that it is inline with the above comments. No longer testing via the DurationToTimeMillisField explicitly and therefore do not have to know about the internals of the zap core encoding of floats.

@mwitkow
Copy link
Member

mwitkow commented Dec 7, 2017

ok, the problem here is that if we do it this way, we will have really long, unreasonable floats.

A float for a millisecond counter is fine, as long as we cap it's precision to /10 or /100 of a millisecond.

@domgreen
Copy link
Contributor Author

domgreen commented Dec 7, 2017

@mwitkow Couple of options I could think of:

  1. leave as is and close bug as not fixing
  2. cast to a string and always have 2 decimal place precision fmt.Sprintf("%.2f", durationToMilliseconds(duration))
  3. cast to string at 2 decimal places only if less than 1 and drop all decimal places if 1 or above.

I do think it's valuable recording less than 0ms as seeing logs with grpc.time_ms: 0 is a little confusing IMO.

@domgreen
Copy link
Contributor Author

domgreen commented Dec 13, 2017

@mwitkow have gone for 2 ... setting all outputs to have 3dp.

Copy link
Member

@mwitkow mwitkow left a comment

Choose a reason for hiding this comment

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

Legit changes, but please double check the precission concerns.

@domgreen
Copy link
Contributor Author

domgreen commented Dec 14, 2017

@mwitkow it is also now inline with httpware have a sample here to show outputs https://play.golang.org/p/SFpG5u9fWI

@mwitkow mwitkow merged commit ae5f700 into grpc-ecosystem:master Dec 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants