Skip to content

Commit

Permalink
Merge pull request #822 from nyaruka/metrics_without_ctype
Browse files Browse the repository at this point in the history
Send metrics without channel type as well
  • Loading branch information
rowanseymour authored Dec 19, 2024
2 parents afccf3b + 847cde8 commit 117d949
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
41 changes: 29 additions & 12 deletions backends/rapidpro/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions backends/rapidpro/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 117d949

Please sign in to comment.