فهرست منبع

feat(usage_metrics): add timer metrics

bergquist 9 سال پیش
والد
کامیت
a8ac37f517
5فایلهای تغییر یافته به همراه125 افزوده شده و 5 حذف شده
  1. 2 0
      pkg/api/dashboard.go
  2. 38 0
      pkg/metrics/metric_ref.go
  3. 8 5
      pkg/metrics/metrics.go
  4. 8 0
      pkg/metrics/report_usage.go
  5. 69 0
      pkg/metrics/timer.go

+ 2 - 0
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 {

+ 38 - 0
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()

+ 8 - 5
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")

+ 8 - 0
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()
+			}
 		}
 	})
 

+ 69 - 0
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
+}