This is an automated email from the ASF dual-hosted git repository. littlecui pushed a commit to branch alarm in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
commit ff2740f4f2649ee0fec6d2341139b780eee91c1b Author: little-cui <[email protected]> AuthorDate: Thu Nov 29 18:38:42 2018 +0800 SCB-1049 Add alarm model --- server/alarm/alarm.go | 61 ++++++++++++++++++++++++++++++++++++- server/alarm/{alarm.go => field.go} | 32 ++++++++++++++----- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/server/alarm/alarm.go b/server/alarm/alarm.go index fc6feef..64281b9 100644 --- a/server/alarm/alarm.go +++ b/server/alarm/alarm.go @@ -15,11 +15,70 @@ package alarm +import ( + nf "github.com/apache/servicecomb-service-center/server/service/notification" + "sync/atomic" +) + +type ID int32 + const ( - CodeBackendConnectionRefuse = iota + CodeBackendConnectionRefuse ID = iota CodeServerOverload CodeServiceQuotaLimit CodeInstanceQuotaLimit CodeDiagnoseFailure CodeInternalError + typeEnd ) + +const Subject = "__ALARM_CENTER__" + +var latestId = int32(typeEnd) + +type AlarmEvent struct { + nf.Event + Id ID + fields map[string]interface{} +} + +func (ae *AlarmEvent) FieldBool(key string) bool { + v, _ := ae.fields[key].(bool) + return v +} + +func (ae *AlarmEvent) FieldString(key string) string { + v, _ := ae.fields[key].(string) + return v +} + +func (ae *AlarmEvent) FieldInt64(key string) int64 { + v, _ := ae.fields[key].(int64) + return v +} + +func (ae *AlarmEvent) FieldInt(key string) int { + v, _ := ae.fields[key].(int) + return v +} + +func (ae *AlarmEvent) FieldFloat64(key string) float64 { + v, _ := ae.fields[key].(float64) + return v +} + +func RegisterAlarmSource() ID { + return ID(atomic.AddInt32(&latestId, 1)) +} + +func Alarm(id ID, fields ...Field) error { + ae := &AlarmEvent{ + Event: nf.NewEvent(nf.NOTIFTY, Subject, ""), + Id: id, + fields: make(map[string]interface{}, len(fields)), + } + for _, f := range fields { + ae.fields[f.Key] = f.Value + } + return nf.GetNotifyService().Publish(ae) +} diff --git a/server/alarm/alarm.go b/server/alarm/field.go similarity index 63% copy from server/alarm/alarm.go copy to server/alarm/field.go index fc6feef..33e6aff 100644 --- a/server/alarm/alarm.go +++ b/server/alarm/field.go @@ -15,11 +15,27 @@ package alarm -const ( - CodeBackendConnectionRefuse = iota - CodeServerOverload - CodeServiceQuotaLimit - CodeInstanceQuotaLimit - CodeDiagnoseFailure - CodeInternalError -) +type Field struct { + Key string + Value interface{} +} + +func FieldBool(key string, v bool) Field { + return Field{Key: key, Value: v} +} + +func FieldString(key string, v string) Field { + return Field{Key: key, Value: v} +} + +func FieldInt64(key string, v int64) Field { + return Field{Key: key, Value: v} +} + +func FieldInt(key string, v int) Field { + return Field{Key: key, Value: v} +} + +func FieldFloat64(key string, v float64) Field { + return Field{Key: key, Value: v} +}
