Skip to content

Commit

Permalink
Merge pull request #151 from nyaruka/cwatch_utils
Browse files Browse the repository at this point in the history
Add utils funcs for creating metrics data
  • Loading branch information
rowanseymour authored Dec 16, 2024
2 parents 7dc97c1 + 4d64866 commit bb2c8e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 21 additions & 0 deletions aws/cwatch/datum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cwatch

import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
)

// Some utility functions because the standard API is annoyingly verbose

func Dimension(name, value string) types.Dimension {
return types.Dimension{Name: aws.String(name), Value: aws.String(value)}
}

func Datum(metric string, value float64, unit types.StandardUnit, dims ...types.Dimension) types.MetricDatum {
return types.MetricDatum{
MetricName: aws.String(metric),
Dimensions: dims,
Value: aws.Float64(value),
Unit: unit,
}
}
6 changes: 3 additions & 3 deletions aws/cwatch/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func TestService(t *testing.T) {
wg := &sync.WaitGroup{}
svc.StartQueue(wg, time.Millisecond*100)

svc.Queue(types.MetricDatum{MetricName: aws.String("NumGoats"), Value: aws.Float64(10), Unit: types.StandardUnitCount})
svc.Queue(types.MetricDatum{MetricName: aws.String("NumSheep"), Value: aws.Float64(20), Unit: types.StandardUnitCount})
svc.Queue(cwatch.Datum("NumGoats", 10, types.StandardUnitCount, cwatch.Dimension("Host", "foo1")))
svc.Queue(cwatch.Datum("NumSheep", 20, types.StandardUnitCount))
assert.Equal(t, 0, svc.Client.(*cwatch.DevClient).CallCount()) // not sent yet

time.Sleep(time.Millisecond * 200)

assert.Equal(t, 1, svc.Client.(*cwatch.DevClient).CallCount()) // sent as one call

svc.Queue(types.MetricDatum{MetricName: aws.String("SleepTime"), Value: aws.Float64(30), Unit: types.StandardUnitSeconds})
svc.Queue(cwatch.Datum("SleepTime", 30, types.StandardUnitSeconds))

svc.StopQueue()
wg.Wait()
Expand Down

0 comments on commit bb2c8e4

Please sign in to comment.