-
Notifications
You must be signed in to change notification settings - Fork 13
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
Remove in memory sink from fanout and add latency sampling methods #28
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package telemetry | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/armon/go-metrics" | ||
|
@@ -19,6 +20,18 @@ func NewLabel(name, value string) metrics.Label { | |
return metrics.Label{Name: name, Value: value} | ||
} | ||
|
||
// ModuleMeasureSinceWithSampling samples latency metrics given the sample rate. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update comment to state sampleRate should be between |
||
// This is intended to be used in hot code paths. | ||
func ModuleMeasureSinceWithSampling(module string, start time.Time, sampleRate float64, keys ...string) { | ||
if rand.Float64() < sampleRate { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a lot of issues with sampling based upon a probabilistic rate since:
Sampling per N units of time makes a lot more sense. For example only allow this metric to be sampled once every second. It would also be good to scale the sample based upon how many times it has occurred correctly but that seems like it would require changing the If you still want to go down this path then it would make sense to use a faster random number generator like https://github.com/flyingmutant/rand and also ensure that you perform the sampling before you compute the time which would require the sampling to happen in the callers method (which is annoying without macros). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed on slack, we will go as is. We should swap to use float32 as we don't need the extra precision that float64 provides and it takes less effort to RNG 32 bits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it actually takes more effort because Float32 internally calls Float64 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is terrible. Likely why other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to move the sampling check outside of the method so that we don't have to pay the cost of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah maybe we should just do it that way. let me revert |
||
metrics.MeasureSinceWithLabels( | ||
keys, | ||
start.UTC(), | ||
append([]metrics.Label{NewLabel(MetricLabelNameModule, module)}, globalLabels...), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a label that states the sampling rate? |
||
) | ||
} | ||
} | ||
|
||
// ModuleMeasureSince provides a short hand method for emitting a time measure | ||
// metric for a module with a given set of keys. If any global labels are defined, | ||
// they will be added to the module label. | ||
|
@@ -70,3 +83,11 @@ func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) { | |
func MeasureSince(start time.Time, keys ...string) { | ||
metrics.MeasureSinceWithLabels(keys, start.UTC(), globalLabels) | ||
} | ||
|
||
// MeasureSinceWithSampling provides a wrapper functionality for emitting a a time measure | ||
// metric with sampling. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto on comment. |
||
func MeasureSinceWithSampling(start time.Time, sampleRate float64, keys ...string) { | ||
if rand.Float64() < sampleRate { | ||
metrics.MeasureSinceWithLabels(keys, start.UTC(), globalLabels) | ||
} | ||
} |
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.
Don't create the fanout sink, you should be able to use a
MetricSink
object and either assignpromSink
ormemSink
to it.