forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use same approach to metrics as courier - record events in stats, con…
…vert to metrics and send every minute
- Loading branch information
1 parent
c3a1701
commit 13d6fa1
Showing
7 changed files
with
105 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package runtime | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" | ||
"github.com/nyaruka/gocommon/aws/cwatch" | ||
) | ||
|
||
type Stats struct { | ||
HandlerTaskCount int // number of contact tasks handled | ||
HandlerTaskDuration time.Duration // total time spent handling contact tasks | ||
HandlerTaskLatency time.Duration // total time spent queuing and handling contact tasks | ||
|
||
CronTaskCount map[string]int // number of cron tasks run by type | ||
CronTaskDuration map[string]time.Duration // total time spent running cron tasks | ||
} | ||
|
||
func newStats() *Stats { | ||
return &Stats{} | ||
} | ||
|
||
func (s *Stats) ToMetrics() []types.MetricDatum { | ||
metrics := make([]types.MetricDatum, 0, 20) | ||
|
||
// convert handler task timings to averages | ||
avgHandlerTaskDuration, avgHandlerTaskLatency := time.Duration(0), time.Duration(0) | ||
if s.HandlerTaskCount > 0 { | ||
avgHandlerTaskDuration = s.HandlerTaskDuration / time.Duration(s.HandlerTaskCount) | ||
avgHandlerTaskLatency = s.HandlerTaskLatency / time.Duration(s.HandlerTaskCount) | ||
} | ||
|
||
metrics = append(metrics, | ||
cwatch.Datum("HandlerTaskCount", float64(s.HandlerTaskCount), types.StandardUnitCount), | ||
cwatch.Datum("HandlerTaskDuration", float64(avgHandlerTaskDuration/time.Second), types.StandardUnitCount), | ||
cwatch.Datum("HandlerTaskLatency", float64(avgHandlerTaskLatency/time.Second), types.StandardUnitCount), | ||
) | ||
|
||
for name, count := range s.CronTaskCount { | ||
avgTime := s.CronTaskDuration[name] / time.Duration(count) | ||
|
||
metrics = append(metrics, | ||
cwatch.Datum("CronTaskCount", float64(count), types.StandardUnitCount, cwatch.Dimension("TaskName", name)), | ||
cwatch.Datum("CronTaskDuration", float64(avgTime/time.Second), types.StandardUnitSeconds, cwatch.Dimension("TaskName", name)), | ||
) | ||
} | ||
|
||
return metrics | ||
} | ||
|
||
// StatsCollector provides threadsafe stats collection | ||
type StatsCollector struct { | ||
mutex sync.Mutex | ||
stats *Stats | ||
} | ||
|
||
// NewStatsCollector creates a new stats collector | ||
func NewStatsCollector() *StatsCollector { | ||
return &StatsCollector{stats: newStats()} | ||
} | ||
|
||
func (c *StatsCollector) RecordHandlerTask(d, l time.Duration) { | ||
c.mutex.Lock() | ||
c.stats.HandlerTaskCount++ | ||
c.stats.HandlerTaskDuration += d | ||
c.stats.HandlerTaskLatency += l | ||
c.mutex.Unlock() | ||
} | ||
|
||
func (c *StatsCollector) RecordCronTask(name string, d time.Duration) { | ||
c.mutex.Lock() | ||
c.stats.CronTaskCount[name]++ | ||
c.stats.CronTaskDuration[name] += d | ||
c.mutex.Unlock() | ||
} | ||
|
||
// Extract returns the stats for the period since the last call | ||
func (c *StatsCollector) Extract() *Stats { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
s := c.stats | ||
c.stats = newStats() | ||
return s | ||
} |