From d563ad26f1244d9dbbdfc2ff2fe4222d8d167edb Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Fri, 9 Jul 2021 02:30:30 -0400 Subject: [PATCH] report statistics about account and device counts --- cmd/apollo-scheduler/main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cmd/apollo-scheduler/main.go b/cmd/apollo-scheduler/main.go index e04959c..7b765a7 100644 --- a/cmd/apollo-scheduler/main.go +++ b/cmd/apollo-scheduler/main.go @@ -106,6 +106,7 @@ func main() { s := gocron.NewScheduler(time.UTC) s.Every(1).Second().Do(func() { enqueueAccounts(ctx, logger, statsd, pool, redisConn, notificationsQueue) }) + s.Every(1).Minute().Do(func() { reportStats(ctx, logger, statsd, pool, redisConn) }) s.StartAsync() signals := make(chan os.Signal, 1) @@ -122,6 +123,30 @@ func main() { s.Stop() } +func reportStats(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, pool *pgxpool.Pool, redisConn *redis.Client) { + var ( + count int64 + + metrics = []struct { + query string + name string + }{ + {"SELECT COUNT(*) FROM accounts", "apollo.registrations.accounts"}, + {"SELECT COUNT(*) FROM devices", "apollo.registrations.devices"}, + } + ) + + for _, metric := range metrics { + pool.QueryRow(ctx, metric.query).Scan(&count) + statsd.Gauge(metric.name, float64(count), []string{}, 1) + + logger.WithFields(logrus.Fields{ + "count": count, + "metric": metric.name, + }).Debug("fetched metrics") + } +} + func enqueueAccounts(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, pool *pgxpool.Pool, redisConn *redis.Client, queue rmq.Queue) { start := time.Now()