Package list golang-github-go-kit-kit / 0be9fba
test cloudwatch.WithPercentiles() Eric Feliksik 4 years ago
2 changed file(s) with 52 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
5959
6060 // WithPercentiles registers the percentiles to track, overriding the
6161 // existing/default values.
62 // Reason is that Cloudwatch makes you pay per metric, so you can save half the money
63 // by only using 2 metrics instead of the default 4.
6264 func WithPercentiles(percentiles ...float64) option {
6365 return func(c *CloudWatch) {
64 c.percentiles = make([]float64, 0)
66 c.percentiles = make([]float64, 0, len(percentiles))
6567 for _, p := range percentiles {
6668 if p < 0 || p > 1 {
6769 continue // illegal entry; ignore
192192 if err := svc.testDimensions(n99, label, value); err != nil {
193193 t.Fatal(err)
194194 }
195 }
195
196 // now test with only 2 custom percentiles
197 //
198 svc = newMockCloudWatch()
199 cw = New(namespace, svc, WithLogger(log.NewNopLogger()), WithPercentiles(0.50, 0.90))
200 histogram = cw.NewHistogram(name).With(label, value)
201
202 customQuantiles := func() (p50, p90, p95, p99 float64) {
203 err := cw.Send()
204 if err != nil {
205 t.Fatal(err)
206 }
207 svc.mtx.RLock()
208 defer svc.mtx.RUnlock()
209 p50 = svc.valuesReceived[n50]
210 p90 = svc.valuesReceived[n90]
211
212 // our teststat.TestHistogram wants us to give p95 and p99,
213 // but with custom percentiles we don't have those.
214 // So fake them. Maybe we should make teststat.nvq() public and use that?
215 p95 = 541.121341
216 p99 = 558.158697
217
218 // but fail if they are actually set (because that would mean the
219 // WithPercentiles() is not respected)
220 if _, isSet := svc.valuesReceived[n95]; isSet {
221 t.Fatal("p95 should not be set")
222 }
223 if _, isSet := svc.valuesReceived[n99]; isSet {
224 t.Fatal("p99 should not be set")
225 }
226 return
227 }
228 if err := teststat.TestHistogram(histogram, customQuantiles, 0.01); err != nil {
229 t.Fatal(err)
230 }
231 if err := svc.testDimensions(n50, label, value); err != nil {
232 t.Fatal(err)
233 }
234 if err := svc.testDimensions(n90, label, value); err != nil {
235 t.Fatal(err)
236 }
237 if err := svc.testDimensions(n95, label, value); err != nil {
238 t.Fatal(err)
239 }
240 if err := svc.testDimensions(n99, label, value); err != nil {
241 t.Fatal(err)
242 }
243 }