From a8ac37f5173699adf6965b94d68923dad04b8866 Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 1 Jun 2016 15:04:58 +0200 Subject: [PATCH] feat(usage_metrics): add timer metrics --- pkg/api/dashboard.go | 2 ++ pkg/metrics/metric_ref.go | 38 ++++++++++++++++++++ pkg/metrics/metrics.go | 13 ++++--- pkg/metrics/report_usage.go | 8 +++++ pkg/metrics/timer.go | 69 +++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 pkg/metrics/timer.go diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index cad9da19e82..fda5297912e 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -77,6 +77,8 @@ func GetDashboard(c *middleware.Context) { } c.JSON(200, dto) + + metrics.M_Api_Dashboard_Get_Timer.AddTiming(123333) } func getUserLogin(userId int64) string { diff --git a/pkg/metrics/metric_ref.go b/pkg/metrics/metric_ref.go index f9e5d693d4c..6419c68a74c 100644 --- a/pkg/metrics/metric_ref.go +++ b/pkg/metrics/metric_ref.go @@ -5,6 +5,11 @@ type comboCounterRef struct { metricCounter Counter } +type comboTimerRef struct { + usageTimer Timer + metricTimer Timer +} + func NewComboCounterRef(name string) Counter { cr := &comboCounterRef{} cr.usageCounter = UsageStats.GetOrRegister(name, NewCounter).(Counter) @@ -12,6 +17,39 @@ func NewComboCounterRef(name string) Counter { return cr } +func NewComboTimerRef(name string) Timer { + tr := &comboTimerRef{} + tr.usageTimer = UsageStats.GetOrRegister(name, NewTimer).(Timer) + tr.metricTimer = MetricStats.GetOrRegister(name, NewTimer).(Timer) + return tr +} + +func (t comboTimerRef) Clear() { + t.metricTimer.Clear() + t.usageTimer.Clear() +} + +func (t comboTimerRef) Avg() int64 { + panic("Avg called on combotimer ref") +} + +func (t comboTimerRef) Min() int64 { + panic("Avg called on combotimer ref") +} + +func (t comboTimerRef) Max() int64 { + panic("Avg called on combotimer ref") +} + +func (t comboTimerRef) Total() int64 { + panic("Avg called on combotimer ref") +} + +func (t comboTimerRef) AddTiming(timing int64) { + t.metricTimer.AddTiming(timing) + t.usageTimer.AddTiming(timing) +} + func (c comboCounterRef) Clear() { c.usageCounter.Clear() c.metricCounter.Clear() diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 8e10b2428b4..2556fa78420 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -17,11 +17,14 @@ var ( M_Api_User_SignUpCompleted = NewComboCounterRef("api.user.signup_completed") M_Api_User_SignUpInvite = NewComboCounterRef("api.user.signup_invite") M_Api_Dashboard_Get = NewComboCounterRef("api.dashboard.get") - M_Api_Dashboard_Post = NewComboCounterRef("api.dashboard.post") - M_Api_Admin_User_Create = NewComboCounterRef("api.admin.user_create") - M_Api_Login_Post = NewComboCounterRef("api.login.post") - M_Api_Login_OAuth = NewComboCounterRef("api.login.oauth") - M_Api_Org_Create = NewComboCounterRef("api.org.create") + + M_Api_Dashboard_Get_Timer = NewComboTimerRef("api.dashboard_load") + + M_Api_Dashboard_Post = NewComboCounterRef("api.dashboard.post") + M_Api_Admin_User_Create = NewComboCounterRef("api.admin.user_create") + M_Api_Login_Post = NewComboCounterRef("api.login.post") + M_Api_Login_OAuth = NewComboCounterRef("api.login.oauth") + M_Api_Org_Create = NewComboCounterRef("api.org.create") M_Api_Dashboard_Snapshot_Create = NewComboCounterRef("api.dashboard_snapshot.create") M_Api_Dashboard_Snapshot_External = NewComboCounterRef("api.dashboard_snapshot.external") diff --git a/pkg/metrics/report_usage.go b/pkg/metrics/report_usage.go index 6f1a6ca521e..02d5bdc6a48 100644 --- a/pkg/metrics/report_usage.go +++ b/pkg/metrics/report_usage.go @@ -52,6 +52,14 @@ func sendMetricUsage(sender MetricSender) { metrics[name+".count"] = metric.Count() metric.Clear() } + case Timer: + if metric.Total() > 0 { + metrics[name+".avg"] = metric.Avg() + metrics[name+".min"] = metric.Min() + metrics[name+".max"] = metric.Max() + metrics[name+".total"] = metric.Total() + metric.Clear() + } } }) diff --git a/pkg/metrics/timer.go b/pkg/metrics/timer.go new file mode 100644 index 00000000000..eff1e7e88cf --- /dev/null +++ b/pkg/metrics/timer.go @@ -0,0 +1,69 @@ +package metrics + +//import "sync/atomic" + +type Timer interface { + AddTiming(int64) + Clear() + Avg() int64 + Min() int64 + Max() int64 + Total() int64 +} + +func NewTimer() Timer { + return &StandardTimer{ + avg: 0, + min: 0, + max: 0, + total: 0, + count: 0, + } +} + +func (this *StandardTimer) AddTiming(time int64) { + if this.min > time { + this.min = time + } + + if this.max < time { + this.max = time + } + + this.total += time + this.count++ + + this.avg = this.total / this.count +} + +func (this *StandardTimer) Clear() { + this.avg = 0 + this.min = 0 + this.max = 0 + this.total = 0 + this.count = 0 +} + +func (this *StandardTimer) Avg() int64 { + return this.avg +} + +func (this *StandardTimer) Min() int64 { + return this.min +} + +func (this *StandardTimer) Max() int64 { + return this.max +} + +func (this *StandardTimer) Total() int64 { + return this.total +} + +type StandardTimer struct { + total int64 + count int64 + avg int64 + min int64 + max int64 +}