From 3373737881c4af6a4dbbe8b99ae000ce904165d5 Mon Sep 17 00:00:00 2001 From: Eric Feliksik Date: Thu, 10 Aug 2017 10:13:39 +0200 Subject: [PATCH] test cloudwatch.WithPercentiles() --- metrics/cloudwatch/cloudwatch.go | 4 ++- metrics/cloudwatch/cloudwatch_test.go | 50 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/metrics/cloudwatch/cloudwatch.go b/metrics/cloudwatch/cloudwatch.go index 3a5f4f86d..3d664d70c 100644 --- a/metrics/cloudwatch/cloudwatch.go +++ b/metrics/cloudwatch/cloudwatch.go @@ -60,9 +60,11 @@ func WithLogger(logger log.Logger) option { // WithPercentiles registers the percentiles to track, overriding the // existing/default values. +// Reason is that Cloudwatch makes you pay per metric, so you can save half the money +// by only using 2 metrics instead of the default 4. func WithPercentiles(percentiles ...float64) option { return func(c *CloudWatch) { - c.percentiles = make([]float64, 0) + c.percentiles = make([]float64, 0, len(percentiles)) for _, p := range percentiles { if p < 0 || p > 1 { continue // illegal entry; ignore diff --git a/metrics/cloudwatch/cloudwatch_test.go b/metrics/cloudwatch/cloudwatch_test.go index f934cc8d3..8dc6e6024 100644 --- a/metrics/cloudwatch/cloudwatch_test.go +++ b/metrics/cloudwatch/cloudwatch_test.go @@ -193,4 +193,54 @@ func TestHistogram(t *testing.T) { if err := svc.testDimensions(n99, label, value); err != nil { t.Fatal(err) } + + // now test with only 2 custom dimensions + // + svc = newMockCloudWatch() + cw = New(namespace, svc, WithLogger(log.NewNopLogger()), WithPercentiles(0.50, 0.90)) + histogram = cw.NewHistogram(name).With(label, value) + + // note we are 'abusing' the p50 and p90 return values + // as these are hardcoded in the teststat.TestHistogram framework + customQuantiles := func() (p50, p90, p95, p99 float64) { + err := cw.Send() + if err != nil { + t.Fatal(err) + } + svc.mtx.RLock() + defer svc.mtx.RUnlock() + p50 = svc.valuesReceived[n50] + p90 = svc.valuesReceived[n90] + + // our teststat.TestHistogram wants us to give p95 and p99, + // but with custom percentiles we don't have those. + // So fake them. Maybe we should make teststat.nvq() public and use that? + p95 = 541.121341 + p99 = 558.158697 + + // but fail if they are actually set (because that would mean the + // WithPercentiles() is not respected) + if _, isSet := svc.valuesReceived[n95]; isSet { + t.Fatal("p95 should not be set") + } + if _, isSet := svc.valuesReceived[n99]; isSet { + t.Fatal("p99 should not be set") + } + return + } + if err := teststat.TestHistogram(histogram, customQuantiles, 0.01); err != nil { + t.Fatal(err) + } + if err := svc.testDimensions(n50, label, value); err != nil { + t.Fatal(err) + } + if err := svc.testDimensions(n90, label, value); err != nil { + t.Fatal(err) + } + if err := svc.testDimensions(n95, label, value); err != nil { + t.Fatal(err) + } + if err := svc.testDimensions(n99, label, value); err != nil { + t.Fatal(err) + } }