Skip to content

Commit bade8ef

Browse files
committed
Add tests
1 parent aa63ea1 commit bade8ef

File tree

5 files changed

+76
-27
lines changed

5 files changed

+76
-27
lines changed

archives/archives.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -967,21 +967,20 @@ 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))})
984-
}
970+
971+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("ArchiveElapsed"), Value: aws.Float64(timeTaken.Seconds()), Unit: types.StandardUnitSeconds})
972+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("OrgsArchived"), Value: aws.Float64(float64(len(orgs))), Unit: types.StandardUnitCount})
973+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsRecordsArchived"), Value: aws.Float64(float64(totalMsgsRecordsArchived)), Unit: types.StandardUnitCount})
974+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsArchivedsCreated"), Value: aws.Float64(float64(totalMsgsArchivesCreated)), Unit: types.StandardUnitCount})
975+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsArchivedsFailed"), Value: aws.Float64(float64(totalMsgsArchivesFailed)), Unit: types.StandardUnitCount})
976+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsRollupsCreated"), Value: aws.Float64(float64(totalMsgsRollupsCreated)), Unit: types.StandardUnitCount})
977+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("MsgsRollupsFailed"), Value: aws.Float64(float64(totalMsgsRollupsFailed)), Unit: types.StandardUnitCount})
978+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsRecordsArchived"), Value: aws.Float64(float64(totalRunsRecordsArchived)), Unit: types.StandardUnitCount})
979+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsArchivedsCreated"), Value: aws.Float64(float64(totalRunsArchivesCreated)), Unit: types.StandardUnitCount})
980+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsArchivedsFailed"), Value: aws.Float64(float64(totalRunsArchivesFailed)), Unit: types.StandardUnitCount})
981+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsRollupsCreated"), Value: aws.Float64(float64(totalRunsRollupsCreated)), Unit: types.StandardUnitCount})
982+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("RunsRollupsFailed"), Value: aws.Float64(float64(totalRunsRollupsFailed)), Unit: types.StandardUnitCount})
983+
985984
analytics.Gauge("archiver.archive_elapsed", timeTaken.Seconds())
986985
analytics.Gauge("archiver.orgs_archived", float64(len(orgs)))
987986
analytics.Gauge("archiver.msgs_records_archived", float64(totalMsgsRecordsArchived))

archives/archives_test.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import (
66
"io"
77
"log/slog"
88
"os"
9+
"sync"
910
"testing"
1011
"time"
1112

13+
"github.com/aws/aws-sdk-go-v2/aws"
14+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
1215
"github.com/jmoiron/sqlx"
1316
_ "github.com/lib/pq"
1417
"github.com/nyaruka/gocommon/analytics"
@@ -44,7 +47,7 @@ func setup(t *testing.T) (context.Context, *runtime.Runtime) {
4447

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

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

5053
return ctx, &runtime.Runtime{Config: config, DB: db, S3: s3Client, CW: CW}
@@ -495,7 +498,24 @@ func TestArchiveOrgRuns(t *testing.T) {
495498
}
496499

497500
func TestArchiveActiveOrgs(t *testing.T) {
498-
_, rt := setup(t)
501+
ctx, rt := setup(t)
502+
wg := &sync.WaitGroup{}
503+
504+
rt.CW.StartQueue(wg, time.Millisecond*100)
505+
assert.Equal(t, 0, rt.CW.Client.(*cwatch.DevClient).CallCount())
506+
507+
_, err := rt.CW.Client.PutMetricData(ctx, rt.CW.Prepare([]types.MetricDatum{
508+
{MetricName: aws.String("NumGoats"), Value: aws.Float64(10), Unit: types.StandardUnitCount},
509+
{MetricName: aws.String("NumSheep"), Dimensions: []types.Dimension{{Name: aws.String("Host"), Value: aws.String("foo1")}}, Value: aws.Float64(20), Unit: types.StandardUnitCount},
510+
}))
511+
512+
assert.NoError(t, err)
513+
assert.Equal(t, 1, rt.CW.Client.(*cwatch.DevClient).CallCount())
514+
515+
// test queuing metrics to be sent by batching process
516+
rt.CW.Queue(types.MetricDatum{MetricName: aws.String("SleepTime"), Value: aws.Float64(30), Unit: types.StandardUnitSeconds})
517+
518+
time.Sleep(time.Millisecond * 300)
499519

500520
mockAnalytics := analytics.NewMock()
501521
analytics.RegisterBackend(mockAnalytics)
@@ -504,7 +524,7 @@ func TestArchiveActiveOrgs(t *testing.T) {
504524
dates.SetNowFunc(dates.NewSequentialNow(time.Date(2018, 1, 8, 12, 30, 0, 0, time.UTC), time.Second))
505525
defer dates.SetNowFunc(time.Now)
506526

507-
err := ArchiveActiveOrgs(rt)
527+
err = ArchiveActiveOrgs(rt)
508528
assert.NoError(t, err)
509529

510530
assert.Equal(t, map[string][]float64{
@@ -523,5 +543,9 @@ func TestArchiveActiveOrgs(t *testing.T) {
523543
}, mockAnalytics.Gauges)
524544

525545
analytics.Stop()
546+
rt.CW.StopQueue()
547+
548+
// check the queued metric was sent
549+
assert.Equal(t, 2, rt.CW.Client.(*cwatch.DevClient).CallCount())
526550

527551
}

cmd/rp-archiver/main.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,15 @@ 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)
138+
rt.CW.StartQueue(wg, time.Second*3)
141139

142140
if config.Once {
143141
doArchival(rt)

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)