From 847cde8aa7580a6507b38b22fa59499b7dfbcc1e Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 19 Dec 2024 16:35:51 -0500 Subject: [PATCH] Send metrics without channel type as well --- backends/rapidpro/stats.go | 41 +++++++++++++++++++++++---------- backends/rapidpro/stats_test.go | 19 +++++++++++++-- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/backends/rapidpro/stats.go b/backends/rapidpro/stats.go index 4c412470..10c71282 100644 --- a/backends/rapidpro/stats.go +++ b/backends/rapidpro/stats.go @@ -11,17 +11,42 @@ import ( type CountByType map[courier.ChannelType]int -// converts per channel counts into cloudwatch metrics with type as a dimension +// converts per channel counts into a set of cloudwatch metrics with type as a dimension, and a total count without type func (c CountByType) metrics(name string) []types.MetricDatum { - m := make([]types.MetricDatum, 0, len(c)) + m := make([]types.MetricDatum, 0, len(c)+1) + total := 0 for typ, count := range c { m = append(m, cwatch.Datum(name, float64(count), types.StandardUnitCount, cwatch.Dimension("ChannelType", string(typ)))) + + total += count } + + m = append(m, cwatch.Datum(name, float64(total), types.StandardUnitCount)) return m } type DurationByType map[courier.ChannelType]time.Duration +func (c DurationByType) metrics(name string, avgDenom func(courier.ChannelType) int) []types.MetricDatum { + m := make([]types.MetricDatum, 0, len(c)+1) + totalDuration := time.Duration(0) + totalDenom := 0 + for typ, d := range c { // convert to averages + denom := avgDenom(typ) + avgTime := d / time.Duration(denom) + m = append(m, cwatch.Datum(name, float64(avgTime)/float64(time.Second), types.StandardUnitSeconds, cwatch.Dimension("ChannelType", string(typ)))) + + totalDuration += d + totalDenom += denom + } + + if totalDenom > 0 { + overallAvg := float64(totalDuration) / float64(totalDenom) + m = append(m, cwatch.Datum(name, overallAvg/float64(time.Second), types.StandardUnitSeconds)) + } + return m +} + type Stats struct { IncomingRequests CountByType // number of handler requests IncomingMessages CountByType // number of messages received @@ -61,19 +86,11 @@ func (s *Stats) ToMetrics() []types.MetricDatum { metrics = append(metrics, s.IncomingStatuses.metrics("IncomingStatuses")...) metrics = append(metrics, s.IncomingEvents.metrics("IncomingEvents")...) metrics = append(metrics, s.IncomingIgnored.metrics("IncomingIgnored")...) - - for typ, d := range s.IncomingDuration { // convert to averages - avgTime := d / time.Duration(s.IncomingRequests[typ]) - metrics = append(metrics, cwatch.Datum("IncomingDuration", float64(avgTime)/float64(time.Second), types.StandardUnitSeconds, cwatch.Dimension("ChannelType", string(typ)))) - } + metrics = append(metrics, s.IncomingDuration.metrics("IncomingDuration", func(typ courier.ChannelType) int { return s.IncomingRequests[typ] })...) metrics = append(metrics, s.OutgoingSends.metrics("OutgoingSends")...) metrics = append(metrics, s.OutgoingErrors.metrics("OutgoingErrors")...) - - for typ, d := range s.OutgoingDuration { // convert to averages - avgTime := d / time.Duration(s.OutgoingSends[typ]+s.OutgoingErrors[typ]) - metrics = append(metrics, cwatch.Datum("OutgoingDuration", float64(avgTime)/float64(time.Second), types.StandardUnitSeconds, cwatch.Dimension("ChannelType", string(typ)))) - } + metrics = append(metrics, s.OutgoingDuration.metrics("OutgoingDuration", func(typ courier.ChannelType) int { return s.OutgoingSends[typ] + s.OutgoingErrors[typ] })...) metrics = append(metrics, cwatch.Datum("ContactsCreated", float64(s.ContactsCreated), types.StandardUnitCount)) return metrics diff --git a/backends/rapidpro/stats_test.go b/backends/rapidpro/stats_test.go index caf92fd0..12d5477f 100644 --- a/backends/rapidpro/stats_test.go +++ b/backends/rapidpro/stats_test.go @@ -4,8 +4,10 @@ import ( "testing" "time" + "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" "github.com/nyaruka/courier" "github.com/nyaruka/courier/backends/rapidpro" + "github.com/nyaruka/gocommon/aws/cwatch" "github.com/stretchr/testify/assert" ) @@ -33,7 +35,7 @@ func TestStats(t *testing.T) { assert.Equal(t, rapidpro.DurationByType{"T": time.Second * 2, "FBA": time.Second * 3}, stats.OutgoingDuration) metrics := stats.ToMetrics() - assert.Len(t, metrics, 8) + assert.Len(t, metrics, 17) sc.RecordOutgoing("FBA", true, time.Second) sc.RecordOutgoing("FBA", true, time.Second) @@ -51,5 +53,18 @@ func TestStats(t *testing.T) { assert.Equal(t, rapidpro.DurationByType{"FBA": time.Second * 2}, stats.OutgoingDuration) metrics = stats.ToMetrics() - assert.Len(t, metrics, 3) + assert.Len(t, metrics, 11) + assert.Equal(t, []types.MetricDatum{ + cwatch.Datum("IncomingRequests", 0, "Count"), + cwatch.Datum("IncomingMessages", 0, "Count"), + cwatch.Datum("IncomingStatuses", 0, "Count"), + cwatch.Datum("IncomingEvents", 0, "Count"), + cwatch.Datum("IncomingIgnored", 0, "Count"), + cwatch.Datum("OutgoingSends", 2, "Count", cwatch.Dimension("ChannelType", "FBA")), + cwatch.Datum("OutgoingSends", 2, "Count"), + cwatch.Datum("OutgoingErrors", 0, "Count"), + cwatch.Datum("OutgoingDuration", 1, "Seconds", cwatch.Dimension("ChannelType", "FBA")), + cwatch.Datum("OutgoingDuration", 1, "Seconds"), + cwatch.Datum("ContactsCreated", 0, "Count"), + }, metrics) }