Skip to content

Commit

Permalink
test cloudwatch.WithPercentiles()
Browse files Browse the repository at this point in the history
  • Loading branch information
feliksik committed Aug 10, 2017
1 parent c961428 commit 3373737
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion metrics/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions metrics/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 3373737

Please sign in to comment.