Skip to content

Commit 9ebcca0

Browse files
committed
Add tests
1 parent aa63ea1 commit 9ebcca0

File tree

5 files changed

+71
-27
lines changed

5 files changed

+71
-27
lines changed

archives/archives.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -967,21 +967,26 @@ func ArchiveActiveOrgs(rt *runtime.Runtime) error {
967967

968968
timeTaken := dates.Now().Sub(start)
969969
slog.Info("archiving of active orgs complete", "time_taken", timeTaken, "num_orgs", len(orgs))
970-
if rt.Config.DeploymentID != "dev" {
971-
972-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("ArchiveElapsed"), Value: aws.Float64(float64(len(orgs)))})
973-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("OrgsArchived"), Value: aws.Float64(float64(len(orgs)))})
974-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsRecordsArchived"), Value: aws.Float64(float64(totalMsgsRecordsArchived))})
975-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsArchivedsCreated"), Value: aws.Float64(float64(totalMsgsArchivesCreated))})
976-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsArchivedsFailed"), Value: aws.Float64(float64(totalMsgsArchivesFailed))})
977-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsRollupsCreated"), Value: aws.Float64(float64(totalMsgsRollupsCreated))})
978-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsRollupsFailed"), Value: aws.Float64(float64(totalMsgsRollupsFailed))})
979-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsRecordsArchived"), Value: aws.Float64(float64(totalRunsRecordsArchived))})
980-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsArchivedsCreated"), Value: aws.Float64(float64(totalRunsArchivesCreated))})
981-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsArchivedsFailed"), Value: aws.Float64(float64(totalRunsArchivesFailed))})
982-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsRollupsCreated"), Value: aws.Float64(float64(totalRunsRollupsCreated))})
983-
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsRollupsFailed"), Value: aws.Float64(float64(totalRunsRollupsFailed))})
970+
971+
metrics := []types.MetricDatum{
972+
{MetricName: aws.String("ArchiveElapsed"), Value: aws.Float64(timeTaken.Seconds()), Unit: types.StandardUnitSeconds},
973+
{MetricName: aws.String("OrgsArchived"), Value: aws.Float64(float64(len(orgs))), Unit: types.StandardUnitCount},
974+
{MetricName: aws.String("MsgsRecordsArchived"), Value: aws.Float64(float64(totalMsgsRecordsArchived)), Unit: types.StandardUnitCount},
975+
{MetricName: aws.String("MsgsArchivedsCreated"), Value: aws.Float64(float64(totalMsgsArchivesCreated)), Unit: types.StandardUnitCount},
976+
{MetricName: aws.String("MsgsArchivedsFailed"), Value: aws.Float64(float64(totalMsgsArchivesFailed)), Unit: types.StandardUnitCount},
977+
{MetricName: aws.String("MsgsRollupsCreated"), Value: aws.Float64(float64(totalMsgsRollupsCreated)), Unit: types.StandardUnitCount},
978+
{MetricName: aws.String("MsgsRollupsFailed"), Value: aws.Float64(float64(totalMsgsRollupsFailed)), Unit: types.StandardUnitCount},
979+
{MetricName: aws.String("RunsRecordsArchived"), Value: aws.Float64(float64(totalRunsRecordsArchived)), Unit: types.StandardUnitCount},
980+
{MetricName: aws.String("RunsArchivedsCreated"), Value: aws.Float64(float64(totalRunsArchivesCreated)), Unit: types.StandardUnitCount},
981+
{MetricName: aws.String("RunsArchivedsFailed"), Value: aws.Float64(float64(totalRunsArchivesFailed)), Unit: types.StandardUnitCount},
982+
{MetricName: aws.String("RunsRollupsCreated"), Value: aws.Float64(float64(totalRunsRollupsCreated)), Unit: types.StandardUnitCount},
983+
{MetricName: aws.String("RunsRollupsFailed"), Value: aws.Float64(float64(totalRunsRollupsFailed)), Unit: types.StandardUnitCount},
984+
}
985+
986+
if _, err := rt.CW.Client.PutMetricData(ctx, rt.CW.Prepare(metrics)); err != nil {
987+
slog.Error("error putting metrics", "error", err)
984988
}
989+
985990
analytics.Gauge("archiver.archive_elapsed", timeTaken.Seconds())
986991
analytics.Gauge("archiver.orgs_archived", float64(len(orgs)))
987992
analytics.Gauge("archiver.msgs_records_archived", float64(totalMsgsRecordsArchived))

archives/archives_test.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/aws/aws-sdk-go-v2/aws"
13+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
1214
"github.com/jmoiron/sqlx"
1315
_ "github.com/lib/pq"
1416
"github.com/nyaruka/gocommon/analytics"
@@ -44,7 +46,7 @@ func setup(t *testing.T) (context.Context, *runtime.Runtime) {
4446

4547
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
4648

47-
CW, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "testing")
49+
CW, err := cwatch.NewService(config.AWSAccessKeyID, config.AWSSecretAccessKey, config.AWSRegion, config.CloudwatchNamespace, config.DeploymentID)
4850
require.NoError(t, err)
4951

5052
return ctx, &runtime.Runtime{Config: config, DB: db, S3: s3Client, CW: CW}
@@ -495,7 +497,17 @@ func TestArchiveOrgRuns(t *testing.T) {
495497
}
496498

497499
func TestArchiveActiveOrgs(t *testing.T) {
498-
_, rt := setup(t)
500+
ctx, rt := setup(t)
501+
502+
assert.Equal(t, 0, rt.CW.Client.(*cwatch.DevClient).CallCount())
503+
504+
_, err := rt.CW.Client.PutMetricData(ctx, rt.CW.Prepare([]types.MetricDatum{
505+
{MetricName: aws.String("NumGoats"), Value: aws.Float64(10), Unit: types.StandardUnitCount},
506+
{MetricName: aws.String("NumSheep"), Dimensions: []types.Dimension{{Name: aws.String("Host"), Value: aws.String("foo1")}}, Value: aws.Float64(20), Unit: types.StandardUnitCount},
507+
}))
508+
509+
assert.NoError(t, err)
510+
assert.Equal(t, 1, rt.CW.Client.(*cwatch.DevClient).CallCount())
499511

500512
mockAnalytics := analytics.NewMock()
501513
analytics.RegisterBackend(mockAnalytics)
@@ -504,7 +516,7 @@ func TestArchiveActiveOrgs(t *testing.T) {
504516
dates.SetNowFunc(dates.NewSequentialNow(time.Date(2018, 1, 8, 12, 30, 0, 0, time.UTC), time.Second))
505517
defer dates.SetNowFunc(time.Now)
506518

507-
err := ArchiveActiveOrgs(rt)
519+
err = ArchiveActiveOrgs(rt)
508520
assert.NoError(t, err)
509521

510522
assert.Equal(t, map[string][]float64{
@@ -524,4 +536,7 @@ func TestArchiveActiveOrgs(t *testing.T) {
524536

525537
analytics.Stop()
526538

539+
// check the queued metric was sent
540+
assert.Equal(t, 2, rt.CW.Client.(*cwatch.DevClient).CallCount())
541+
527542
}

cmd/rp-archiver/main.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,14 @@ func main() {
127127
analytics.RegisterBackend(analytics.NewLibrato(config.LibratoUsername, config.LibratoToken, config.InstanceName, time.Second, wg))
128128
}
129129

130-
if rt.Config.DeploymentID != "dev" {
131-
rt.CW, err = cwatch.NewService(config.AWSAccessKeyID, config.AWSSecretAccessKey, config.AWSRegion, "Temba", config.DeploymentID)
132-
if err != nil {
133-
logger.Error("unable to create cloudwatch service", "error", err)
134-
} else {
135-
logger.Info("cloudwatch service ok", "state", "starting")
136-
}
130+
rt.CW, err = cwatch.NewService(config.AWSAccessKeyID, config.AWSSecretAccessKey, config.AWSRegion, config.CloudwatchNamespace, config.DeploymentID)
131+
if err != nil {
132+
logger.Error("unable to create cloudwatch service", "error", err)
133+
} else {
134+
logger.Info("cloudwatch service ok", "state", "starting")
137135
}
138136

139137
analytics.Start()
140-
rt.CW.StartQueue(wg)
141138

142139
if config.Once {
143140
doArchival(rt)
@@ -154,7 +151,6 @@ func main() {
154151
}
155152

156153
analytics.Stop()
157-
rt.CW.StopQueue()
158154
wg.Wait()
159155
}
160156

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/jmoiron/sqlx v1.4.0
1414
github.com/lib/pq v1.10.9
1515
github.com/nyaruka/ezconf v0.3.0
16-
github.com/nyaruka/gocommon v1.60.1
16+
github.com/nyaruka/gocommon v1.60.2
1717
github.com/samber/slog-multi v1.2.0
1818
github.com/samber/slog-sentry v1.2.2
1919
github.com/stretchr/testify v1.10.0

go.sum

+28
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ github.com/nyaruka/gocommon v1.60.0 h1:VjkimXF1dlJltWPYRFnqEIizoXXqrsmCuRBt/t1h4
8484
github.com/nyaruka/gocommon v1.60.0/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
8585
github.com/nyaruka/gocommon v1.60.1 h1:m/BXoBQ1KVzbpmTJ5vuQrv084mWyQ6gtuX6cOeva+lM=
8686
github.com/nyaruka/gocommon v1.60.1/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
87+
github.com/nyaruka/gocommon v1.60.2-0.20241213101040-31d318de7516 h1:W2nGSNyLCdtj+FuJe6gC6wxskQHnc1b4guKJbvWuFJ4=
88+
github.com/nyaruka/gocommon v1.60.2-0.20241213101040-31d318de7516/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
89+
github.com/nyaruka/gocommon v1.60.2-0.20241213101632-7af3f01d418c h1:MWhp6Or3C/S0CRAAEuUFsNanmeBsml9vhIo1rGMAupw=
90+
github.com/nyaruka/gocommon v1.60.2-0.20241213101632-7af3f01d418c/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
91+
github.com/nyaruka/gocommon v1.60.2-0.20241213101955-a34cc2b9e7a6 h1:yU1kOmeG4jC11R7gqsP0Akyy0XYjzKFTfmGcS+PsLxU=
92+
github.com/nyaruka/gocommon v1.60.2-0.20241213101955-a34cc2b9e7a6/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
93+
github.com/nyaruka/gocommon v1.60.2-0.20241213102159-aca359588626 h1:mrtup7cuBEVmIQAVjPkvzw/5+lHnfpAjjWooWRa7c38=
94+
github.com/nyaruka/gocommon v1.60.2-0.20241213102159-aca359588626/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
95+
github.com/nyaruka/gocommon v1.60.2-0.20241213102535-f6608972de8a h1:9jHgS60UIMleaPcfXS7wfSeE5VZohLapsbZD5wIhQQw=
96+
github.com/nyaruka/gocommon v1.60.2-0.20241213102535-f6608972de8a/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
97+
github.com/nyaruka/gocommon v1.60.2-0.20241213104214-ab724df76615 h1:xU3bWjxh2AGm7YnvpMpJZ/r880AIeKfnnR5iIBL8L4w=
98+
github.com/nyaruka/gocommon v1.60.2-0.20241213104214-ab724df76615/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
99+
github.com/nyaruka/gocommon v1.60.2-0.20241213110004-7421f1ac47a6 h1:mz5mynR3qWyHgV2il/z5dN+WA0IZQrpIR+wGngZixvg=
100+
github.com/nyaruka/gocommon v1.60.2-0.20241213110004-7421f1ac47a6/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
101+
github.com/nyaruka/gocommon v1.60.2-0.20241213110203-0444041bd051 h1:yAH0gdA3l/ZcuHe0uuXygbjkiEkI75F7HhvkdR5Iibk=
102+
github.com/nyaruka/gocommon v1.60.2-0.20241213110203-0444041bd051/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
103+
github.com/nyaruka/gocommon v1.60.2-0.20241213110951-fb183c8bb4a5 h1:PP8mo3GwgKvKPKW4KAKhD4Fk2nzrM2NR1wNxdLPGePw=
104+
github.com/nyaruka/gocommon v1.60.2-0.20241213110951-fb183c8bb4a5/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
105+
github.com/nyaruka/gocommon v1.60.2-0.20241213111058-b7ec4abe84e0 h1:yZojcEs0ji+H5xzeSTvdX5+RorbtpvQBpGkPSKStuiU=
106+
github.com/nyaruka/gocommon v1.60.2-0.20241213111058-b7ec4abe84e0/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
107+
github.com/nyaruka/gocommon v1.60.2-0.20241213112709-d4ad3229b2d2 h1:AJTdPSYJb4bjqn8flO0/NfZzTPkAJBhgTWpG8RjQ01w=
108+
github.com/nyaruka/gocommon v1.60.2-0.20241213112709-d4ad3229b2d2/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
109+
github.com/nyaruka/gocommon v1.60.2-0.20241213113120-30b1b04a98e8 h1:wK38pZqJ6yJHOVzNRgDI0MxOp1fxEuXozHZIVrTlJCM=
110+
github.com/nyaruka/gocommon v1.60.2-0.20241213113120-30b1b04a98e8/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
111+
github.com/nyaruka/gocommon v1.60.2-0.20241213113333-112a0bc4c3ee h1:MH7kxmPa53XrbDROcK8vzYYw+Im0NMwGL/knR8TsSqE=
112+
github.com/nyaruka/gocommon v1.60.2-0.20241213113333-112a0bc4c3ee/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
113+
github.com/nyaruka/gocommon v1.60.2 h1:AvvSSAV70SV49ocNtvjpdb9NlcdiA2OQAL4NYVUcuV0=
114+
github.com/nyaruka/gocommon v1.60.2/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
87115
github.com/nyaruka/librato v1.1.1 h1:0nTYtJLl3Sn7lX3CuHsLf+nXy1k/tGV0OjVxLy3Et4s=
88116
github.com/nyaruka/librato v1.1.1/go.mod h1:fme1Fu1PT2qvkaBZyw8WW+SrnFe2qeeCWpvqmAaKAKE=
89117
github.com/nyaruka/null/v2 v2.0.3 h1:rdmMRQyVzrOF3Jff/gpU/7BDR9mQX0lcLl4yImsA3kw=

0 commit comments

Comments
 (0)