From 3eb9f0c6bcb894fe291a4cef8abd753f11271c9a Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 26 May 2022 14:43:42 -0500 Subject: [PATCH] Use analytics package from gocommon instead of librato directly --- core/runner/runner.go | 12 ++++++------ core/tasks/analytics/cron.go | 18 +++++++++--------- core/tasks/campaigns/cron.go | 6 +++--- core/tasks/handler/worker.go | 6 +++--- go.mod | 4 ++-- go.sum | 4 ++-- mailroom.go | 9 +++++---- 7 files changed, 30 insertions(+), 29 deletions(-) diff --git a/core/runner/runner.go b/core/runner/runner.go index 29705605e..0d0666348 100644 --- a/core/runner/runner.go +++ b/core/runner/runner.go @@ -6,11 +6,11 @@ import ( "github.com/gomodule/redigo/redis" "github.com/jmoiron/sqlx" + "github.com/nyaruka/gocommon/analytics" "github.com/nyaruka/goflow/assets" "github.com/nyaruka/goflow/excellent/types" "github.com/nyaruka/goflow/flows" "github.com/nyaruka/goflow/flows/triggers" - "github.com/nyaruka/librato" "github.com/nyaruka/mailroom/core/goflow" "github.com/nyaruka/mailroom/core/models" "github.com/nyaruka/mailroom/core/queue" @@ -243,8 +243,8 @@ func StartFlowBatch( } // log both our total and average - librato.Gauge("mr.flow_batch_start_elapsed", float64(time.Since(start))/float64(time.Second)) - librato.Gauge("mr.flow_batch_start_count", float64(len(sessions))) + analytics.Gauge("mr.flow_batch_start_elapsed", float64(time.Since(start))/float64(time.Second)) + analytics.Gauge("mr.flow_batch_start_count", float64(len(sessions))) return sessions, nil } @@ -393,8 +393,8 @@ func FireCampaignEvents( } // log both our total and average - librato.Gauge("mr.campaign_event_elapsed", float64(time.Since(start))/float64(time.Second)) - librato.Gauge("mr.campaign_event_count", float64(len(sessions))) + analytics.Gauge("mr.campaign_event_elapsed", float64(time.Since(start))/float64(time.Second)) + analytics.Gauge("mr.campaign_event_count", float64(len(sessions))) // build the list of contacts actually started startedContacts := make([]models.ContactID, len(sessions)) @@ -559,7 +559,7 @@ func StartFlowForContacts( continue } log.WithField("elapsed", time.Since(start)).Info("flow engine start") - librato.Gauge("mr.flow_start_elapsed", float64(time.Since(start))) + analytics.Gauge("mr.flow_start_elapsed", float64(time.Since(start))) sessions = append(sessions, session) sprints = append(sprints, sprint) diff --git a/core/tasks/analytics/cron.go b/core/tasks/analytics/cron.go index 8fc72af04..a12647993 100644 --- a/core/tasks/analytics/cron.go +++ b/core/tasks/analytics/cron.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/nyaruka/librato" + "github.com/nyaruka/gocommon/analytics" "github.com/nyaruka/mailroom" "github.com/nyaruka/mailroom/core/queue" "github.com/nyaruka/mailroom/runtime" @@ -60,14 +60,14 @@ func reportAnalytics(ctx context.Context, rt *runtime.Runtime) error { redisWaitDuration = redisStats.WaitDuration redisWaitCount = redisStats.WaitCount - librato.Gauge("mr.db_busy", float64(dbStats.InUse)) - librato.Gauge("mr.db_idle", float64(dbStats.Idle)) - librato.Gauge("mr.db_wait_ms", float64(dbWaitDurationInPeriod/time.Millisecond)) - librato.Gauge("mr.db_wait_count", float64(dbWaitCountInPeriod)) - librato.Gauge("mr.redis_wait_ms", float64(redisWaitDurationInPeriod/time.Millisecond)) - librato.Gauge("mr.redis_wait_count", float64(redisWaitCountInPeriod)) - librato.Gauge("mr.handler_queue", float64(handlerSize)) - librato.Gauge("mr.batch_queue", float64(batchSize)) + analytics.Gauge("mr.db_busy", float64(dbStats.InUse)) + analytics.Gauge("mr.db_idle", float64(dbStats.Idle)) + analytics.Gauge("mr.db_wait_ms", float64(dbWaitDurationInPeriod/time.Millisecond)) + analytics.Gauge("mr.db_wait_count", float64(dbWaitCountInPeriod)) + analytics.Gauge("mr.redis_wait_ms", float64(redisWaitDurationInPeriod/time.Millisecond)) + analytics.Gauge("mr.redis_wait_count", float64(redisWaitCountInPeriod)) + analytics.Gauge("mr.handler_queue", float64(handlerSize)) + analytics.Gauge("mr.batch_queue", float64(batchSize)) logrus.WithFields(logrus.Fields{ "db_busy": dbStats.InUse, diff --git a/core/tasks/campaigns/cron.go b/core/tasks/campaigns/cron.go index 7522aeee0..c6bdd0008 100644 --- a/core/tasks/campaigns/cron.go +++ b/core/tasks/campaigns/cron.go @@ -6,8 +6,8 @@ import ( "time" "github.com/gomodule/redigo/redis" + "github.com/nyaruka/gocommon/analytics" "github.com/nyaruka/goflow/assets" - "github.com/nyaruka/librato" "github.com/nyaruka/mailroom" "github.com/nyaruka/mailroom/core/models" "github.com/nyaruka/mailroom/core/queue" @@ -107,8 +107,8 @@ func QueueEventFires(ctx context.Context, rt *runtime.Runtime) error { numTasks++ } - librato.Gauge("mr.campaign_event_cron_elapsed", float64(time.Since(start))/float64(time.Second)) - librato.Gauge("mr.campaign_event_cron_count", float64(numFires)) + analytics.Gauge("mr.campaign_event_cron_elapsed", float64(time.Since(start))/float64(time.Second)) + analytics.Gauge("mr.campaign_event_cron_count", float64(numFires)) log.WithFields(logrus.Fields{ "elapsed": time.Since(start), "fires": numFires, diff --git a/core/tasks/handler/worker.go b/core/tasks/handler/worker.go index 166f943cd..4f3528a2c 100644 --- a/core/tasks/handler/worker.go +++ b/core/tasks/handler/worker.go @@ -8,6 +8,7 @@ import ( "github.com/gomodule/redigo/redis" "github.com/jmoiron/sqlx" + "github.com/nyaruka/gocommon/analytics" "github.com/nyaruka/gocommon/dbutil" "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/goflow/excellent/types" @@ -17,7 +18,6 @@ import ( "github.com/nyaruka/goflow/flows/resumes" "github.com/nyaruka/goflow/flows/triggers" "github.com/nyaruka/goflow/utils" - "github.com/nyaruka/librato" "github.com/nyaruka/mailroom" "github.com/nyaruka/mailroom/core/models" "github.com/nyaruka/mailroom/core/queue" @@ -159,10 +159,10 @@ func handleContactEvent(ctx context.Context, rt *runtime.Runtime, task *queue.Ta } // log our processing time to librato - librato.Gauge(fmt.Sprintf("mr.%s_elapsed", contactEvent.Type), float64(time.Since(start))/float64(time.Second)) + analytics.Gauge(fmt.Sprintf("mr.%s_elapsed", contactEvent.Type), float64(time.Since(start))/float64(time.Second)) // and total latency for this task since it was queued - librato.Gauge(fmt.Sprintf("mr.%s_latency", contactEvent.Type), float64(time.Since(task.QueuedOn))/float64(time.Second)) + analytics.Gauge(fmt.Sprintf("mr.%s_latency", contactEvent.Type), float64(time.Since(task.QueuedOn))/float64(time.Second)) // if we get an error processing an event, requeue it for later and return our error if err != nil { diff --git a/go.mod b/go.mod index f1d0420cf..de6d37de9 100644 --- a/go.mod +++ b/go.mod @@ -16,9 +16,8 @@ require ( github.com/jmoiron/sqlx v1.3.4 github.com/lib/pq v1.10.4 github.com/nyaruka/ezconf v0.2.1 - github.com/nyaruka/gocommon v1.20.0 + github.com/nyaruka/gocommon v1.21.0 github.com/nyaruka/goflow v0.159.2 - github.com/nyaruka/librato v1.0.0 github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d github.com/nyaruka/null v1.2.0 github.com/nyaruka/redisx v0.2.1 @@ -54,6 +53,7 @@ require ( github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/naoina/go-stringutil v0.1.0 // indirect github.com/naoina/toml v0.1.1 // indirect + github.com/nyaruka/librato v1.0.0 // indirect github.com/nyaruka/phonenumbers v1.0.75 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sergi/go-diff v1.2.0 // indirect diff --git a/go.sum b/go.sum index 91c2410cd..81020dab4 100644 --- a/go.sum +++ b/go.sum @@ -119,8 +119,8 @@ github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8= github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0= github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw= -github.com/nyaruka/gocommon v1.20.0 h1:qbxRsBBPvpfGbuBq08jlQGxa5t+UZX/YGV7+kR+/moM= -github.com/nyaruka/gocommon v1.20.0/go.mod h1:JrQSLAPo9ezSy1AzsJ1zDr1HW0/eu+aipICJkN/+kpg= +github.com/nyaruka/gocommon v1.21.0 h1:nu7M2cdSPrkqUPdGsEeWX047+neo69H4x+4g/OKpoLM= +github.com/nyaruka/gocommon v1.21.0/go.mod h1:cv9r6amof1gSktfPZROClZhLFzdSIH/N9KbW6Nny4g8= github.com/nyaruka/goflow v0.159.2 h1:tyAILWt7DGQ5nfd2/ZN4BNlVxPbm/Omoz/CHS6hs+UM= github.com/nyaruka/goflow v0.159.2/go.mod h1:iLYZOLLggFRR8e5dVgFEmB95W4FPrIKbz3w3C6Ia/0g= github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0= diff --git a/mailroom.go b/mailroom.go index e42345748..d5bbc2c73 100644 --- a/mailroom.go +++ b/mailroom.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/nyaruka/gocommon/analytics" "github.com/nyaruka/gocommon/storage" "github.com/nyaruka/mailroom/core/queue" "github.com/nyaruka/mailroom/runtime" @@ -16,7 +17,6 @@ import ( "github.com/gomodule/redigo/redis" "github.com/jmoiron/sqlx" - "github.com/nyaruka/librato" "github.com/olivere/elastic/v7" "github.com/sirupsen/logrus" ) @@ -172,10 +172,11 @@ func (mr *Mailroom) Start() error { // if we have a librato token, configure it if c.LibratoToken != "" { - librato.Configure(c.LibratoUsername, c.LibratoToken, c.InstanceName, time.Second, mr.wg) - librato.Start() + analytics.RegisterBackend(analytics.NewLibrato(c.LibratoUsername, c.LibratoToken, c.InstanceName, time.Second, mr.wg)) } + analytics.Start() + // init our foremen and start it mr.batchForeman.Start() mr.handlerForeman.Start() @@ -194,7 +195,7 @@ func (mr *Mailroom) Stop() error { logrus.Info("mailroom stopping") mr.batchForeman.Stop() mr.handlerForeman.Stop() - librato.Stop() + analytics.Stop() close(mr.quit) mr.cancel()