From 4d648669df31d2e6375d6ef1eee231e00b6bae05 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 16 Dec 2024 14:18:18 -0500 Subject: [PATCH] Add utils funcs for creating metrics data --- aws/cwatch/datum.go | 21 +++++++++++++++++++++ aws/cwatch/service_test.go | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 aws/cwatch/datum.go diff --git a/aws/cwatch/datum.go b/aws/cwatch/datum.go new file mode 100644 index 0000000..3a12afe --- /dev/null +++ b/aws/cwatch/datum.go @@ -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, + } +} diff --git a/aws/cwatch/service_test.go b/aws/cwatch/service_test.go index 083f013..291875d 100644 --- a/aws/cwatch/service_test.go +++ b/aws/cwatch/service_test.go @@ -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()