Codebase list golang-github-go-kit-kit / f583a20
Cleaner/easier way for user to specify Cloudwatch metric percentiles. Eric Feliksik 6 years ago
1 changed file(s) with 23 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
1313 "github.com/go-kit/kit/metrics"
1414 "github.com/go-kit/kit/metrics/generic"
1515 "github.com/go-kit/kit/metrics/internal/lv"
16 "strconv"
1617 )
1718
1819 const (
3738 counters *lv.Space
3839 gauges *lv.Space
3940 histograms *lv.Space
40 percentiles Percentiles
41 percentiles []float64 // percentiles to track
4142 logger log.Logger
4243 numConcurrentRequests int
4344 }
5657 }
5758 }
5859
59 func WithPercentiles(p Percentiles) option {
60 // WithPercentiles registers the percentiles to track, overriding the
61 // existing/default values.
62 func WithPercentiles(percentiles ...float64) option {
6063 return func(c *CloudWatch) {
61 validated := Percentiles{}
62 for _, entry := range p {
63 if entry.f < 0 || entry.f > 1 {
64 continue // illegal entry
64 c.percentiles = make([]float64, 0)
65 for _, p := range percentiles {
66 if p < 0 || p > 1 {
67 continue // illegal entry; ignore
6568 }
66 validated = append(validated, entry)
67 }
68 c.percentiles = validated
69 c.percentiles = append(c.percentiles, p)
70 }
6971 }
7072 }
7173
9294 histograms: lv.NewSpace(),
9395 numConcurrentRequests: 10,
9496 logger: log.NewLogfmtLogger(os.Stderr),
95 percentiles: Percentiles{
96 {"50", 0.50},
97 {"90", 0.90},
98 {"95", 0.95},
99 {"99", 0.99},
100 },
97 percentiles: []float64{0.50, 0.90, 0.95, 0.99},
10198 }
10299
103100 for _, optFunc := range options {
178175 return true
179176 })
180177
178 // format a [0,1]-float value to a percentile value, with minimum nr of decimals
179 // 0.90 -> "90"
180 // 0.95 -> "95"
181 // 0.999 -> "99.9"
182 formatPerc := func(p float64) string {
183 return strconv.FormatFloat(p*100, 'f', -1, 64)
184 }
185
181186 cw.histograms.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
182187 histogram := generic.NewHistogram(name, 50)
183188
185190 histogram.Observe(v)
186191 }
187192
188 for _, p := range cw.percentiles {
189 value := histogram.Quantile(p.f)
193 for _, perc := range cw.percentiles {
194 value := histogram.Quantile(perc)
190195 datums = append(datums, &cloudwatch.MetricDatum{
191 MetricName: aws.String(fmt.Sprintf("%s_%s", name, p.s)),
196 MetricName: aws.String(fmt.Sprintf("%s_p%s", name, formatPerc(perc))),
192197 Dimensions: makeDimensions(lvs...),
193198 Value: aws.Float64(value),
194199 Timestamp: aws.Time(now),