-
Notifications
You must be signed in to change notification settings - Fork 25
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 bug with observeAndSample function from Histogram #51
Conversation
This commit derives `Show` for all of our *Sample types: - `CounterSample` - `GaugeSample` - `HistogramSample` - `SummarySample`
The Histogram metric has a function `observeAndSample`, which lets you add a new observation, and sample it at the same time. Before this commit, this `observeAndSample` function would return the _previous_ sample, _not_ the sample you just added. This commit changes the behavior of `observeAndSample` so that it returns the sample that was just added. This matches the behavior of `addAndSample` (from `Counter`), and `modifyAndSample` (from `Gauge`), which both return the value of the sample that was just added.
newtype CounterSample = CounterSample {unCounterSample :: Int} | ||
newtype CounterSample = CounterSample {unCounterSample :: Int} deriving Show |
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.
This PR also adds Show
for all of our *Sample
types.
I'm assuming that we want this for ease of debugging. The only problem I can see with this is if we ever want to add a sample type with a value that isn't showable, we might have to figure out what to do in that case. I imagine the likelihood of that is pretty low.
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.
Might we want Eq
as well? Maybe even Ord
. Even though we don't use those typeclasses they seem appropriate for what the data appears to represent.
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 don't really have a strong opinion on this one.
My personal opinion is to derive Show
for everything, since it is helpful for debugging. But only derive other typeclasses as-needed, since it can add additional compile time: https://taylor.fausak.me/2017/08/09/deriving-type-classes-in-haskell-is-slow/
But if we have some sort of coding rule here for what to do, I'd be happy to follow it.
The Histogram metric has a function
observeAndSample
, which lets you add a new observation, and sample it at the same time. Before this commit, thisobserveAndSample
function would return the previous sample, not the sample you just added.This commit changes the behavior of
observeAndSample
so that it returns the sample that was just added. This matches the behavior ofaddAndSample
(fromCounter
), andmodifyAndSample
(fromGauge
), which both return the value of the sample that was just added.Here's an example program that demonstrates this problem:
Running this on
master
gives output like the following:You can see that while the result of
addAndSample
for theCounter
returns 29 (which was the value just added), the result ofobserveAndSample
for theHistogram
returns all zeros (which is the initial state of a histogram).Running this same program in this PR results in:
Now you can see that the resulting
Histogram
sample correctly has a single observation.