Skip to content

Commit

Permalink
upgrade framework and add log level check
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Nov 9, 2020
1 parent ab6c8a6 commit 2cbe8ea
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 78 deletions.
10 changes: 6 additions & 4 deletions agent/job/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ func sendToServer(msg *protocol.MessageRequest, msgRPCServer protocol.MessageCli
return fmt.Errorf("RPC请求失败: %s", err)
}

log.WithFields(log.Fields{
"id": resp.Id,
"body": msg.Data,
}).Debugf("事件同步成功")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"id": resp.Id,
"body": msg.Data,
}).Debugf("事件同步成功")
}

return nil
}
4 changes: 3 additions & 1 deletion agent/job/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func heartbeatJob(cc container.Container, db *ledis.DB, conf *config.Config, hs
return nil
}

log.Debugf("心跳上报成功,服务端版本: %s, 服务端时间戳: %v", pong.ServerVersion, pong.ServerTs)
if log.DebugEnabled() {
log.Debugf("心跳上报成功,服务端版本: %s, 服务端时间戳: %v", pong.ServerVersion, pong.ServerTs)
}
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion api/controller/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func (m *EventController) Events(ctx web.Context, msgRepo repository.EventRepo)
filter["group_ids"] = groupID
}

log.WithFields(log.Fields{"filter": filter}).Debug("events filter")
if log.DebugEnabled() {
log.WithFields(log.Fields{"filter": filter}).Debug("events filter")
}

events, next, err := msgRepo.Paginate(filter, offset, limit)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion api/controller/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func (s *StatisticsController) DailyGroupCounts(ctx web.Context, groupRepo repos
startDate := dailyCounts[0].Datetime
endDate := dailyCounts[len(dailyCounts)-1].Datetime

log.Debugf("%v: %v", startDate, endDate)
if log.DebugEnabled() {
log.Debugf("%v: %v", startDate, endDate)
}

results := make([]MessageGroupByDatetimeCount, 0)

Expand Down
16 changes: 9 additions & 7 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ func main() {
agentID, _ = db.Get([]byte("agent-id"))
}

log.WithFields(log.Fields{
"config": conf,
"agent_id": string(agentID),
}).Debug("configuration")

for _, r := range web.GetAllRoutes(router) {
log.Debugf("route: %s -> %s | %s | %s", r.Name, r.Methods, r.PathTemplate, r.PathRegexp)
if log.DebugEnabled() {
log.WithFields(log.Fields{
"config": conf,
"agent_id": string(agentID),
}).Debug("configuration")

for _, r := range web.GetAllRoutes(router) {
log.Debugf("route: %s -> %s | %s | %s", r.Name, r.Methods, r.PathTemplate, r.PathRegexp)
}
}
})

Expand Down
12 changes: 7 additions & 5 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ func main() {
app.Main(func(conf *configs.Config, router *mux.Router, em event.Manager) {
rand.Seed(time.Now().Unix())

log.WithFields(log.Fields{
"config": conf,
}).Debug("configuration")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"config": conf,
}).Debug("configuration")

for _, r := range web.GetAllRoutes(router) {
log.Debugf("route: %s -> %s | %s | %s", r.Name, r.Methods, r.PathTemplate, r.PathRegexp)
for _, r := range web.GetAllRoutes(router) {
log.Debugf("route: %s -> %s | %s | %s", r.Name, r.Methods, r.PathTemplate, r.PathRegexp)
}
}

em.Publish(pubsub.SystemUpDownEvent{
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ require (
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/golang/protobuf v1.4.2
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/mux v1.7.4
github.com/gorilla/mux v1.8.0
github.com/gorilla/schema v1.2.0 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
github.com/jeremywohl/flatten v0.0.0-20190921043622-d936035e55cf
github.com/ledisdb/ledisdb v0.0.0-20200510135210-d35789ec47e6
github.com/microcosm-cc/bluemonday v1.0.4
github.com/mylxsw/asteria v0.0.0-20200709071207-26dd334a6e51
github.com/mylxsw/asteria v0.0.0-20201109073030-ef64164c7661
github.com/mylxsw/coll v0.0.0-20190810120926-a7a6f0f4bae8
github.com/mylxsw/container v0.0.0-20200525090619-01208c02b074
github.com/mylxsw/glacier v0.0.0-20200820103600-705c319bcccc
github.com/mylxsw/glacier v0.0.0-20201109074739-c3205f7faac3
github.com/mylxsw/go-toolkit v0.0.0-20191208081907-50a06279f988
github.com/mylxsw/graceful v0.0.0-20200605063420-3c53968cf134
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
Expand Down Expand Up @@ -192,6 +198,8 @@ github.com/mylxsw/asteria v0.0.0-20190730075526-1867e6bc4dbe h1:JNV6IpUt1FnPkGFP
github.com/mylxsw/asteria v0.0.0-20190730075526-1867e6bc4dbe/go.mod h1:yKtYUYKkYe2xOB6qqHW+NnoHd6zBFRk72NS/8V/dgwk=
github.com/mylxsw/asteria v0.0.0-20200709071207-26dd334a6e51 h1:a+1fNIW9RK+9AqXrnNx9kQXarjL48uxIKriD13uZUZA=
github.com/mylxsw/asteria v0.0.0-20200709071207-26dd334a6e51/go.mod h1:v0FN8lOptbljEJBRtBrvompc+NyUpIR7Dcpg2f9823s=
github.com/mylxsw/asteria v0.0.0-20201109073030-ef64164c7661 h1:JZS8euU+/cMmZ7m6GkmuxReZJKSdFyp5p4ItVsemsPk=
github.com/mylxsw/asteria v0.0.0-20201109073030-ef64164c7661/go.mod h1:v0FN8lOptbljEJBRtBrvompc+NyUpIR7Dcpg2f9823s=
github.com/mylxsw/coll v0.0.0-20190810120926-a7a6f0f4bae8 h1:TtxSw54bx34zGgs7Y/VisH/sD5HOlopHbSavxTOzgko=
github.com/mylxsw/coll v0.0.0-20190810120926-a7a6f0f4bae8/go.mod h1:Ugpjgv7bOSn1NXiPNHl92DdCGP2siWk50irFSyI+Hf8=
github.com/mylxsw/container v0.0.0-20191208075953-c8ee6e3238cc h1:xBh4hQSO+fcWa/bFLOjxO4huSgofBltDJ3KXPiZlHGo=
Expand All @@ -200,6 +208,8 @@ github.com/mylxsw/container v0.0.0-20200525090619-01208c02b074 h1:8OLLLh/tG7M5kQ
github.com/mylxsw/container v0.0.0-20200525090619-01208c02b074/go.mod h1:UDbF8EtqT7jB0yDc6g9u4P8ORvw4RbFot1QputpgR5U=
github.com/mylxsw/glacier v0.0.0-20200820103600-705c319bcccc h1:UZ+X4drqKtDR0/OKxuvz5nzHssurkVkJW2vR1qbfVlY=
github.com/mylxsw/glacier v0.0.0-20200820103600-705c319bcccc/go.mod h1:gR/AjRPG59eaf9O94gcktmuE+cR9F04WzIcPCDVSy2M=
github.com/mylxsw/glacier v0.0.0-20201109074739-c3205f7faac3 h1:s9iAbD8YanxG6rn1xTqTDZB8u9PINnxdKglqIQTxJtc=
github.com/mylxsw/glacier v0.0.0-20201109074739-c3205f7faac3/go.mod h1:704aHbgS7VTqwdZCq+RbUu5O3+HKJMjp0RxOV2U+PGw=
github.com/mylxsw/go-toolkit v0.0.0-20191208081907-50a06279f988 h1:eUlVMPyxeWUogeqwAkat6eE+3eY5fjP/UAXyhAf/7Jw=
github.com/mylxsw/go-toolkit v0.0.0-20191208081907-50a06279f988/go.mod h1:aJAUawotu1IMtEPXInIm6qwiAEIMSGRAMH6otakYCWw=
github.com/mylxsw/graceful v0.0.0-20200605063420-3c53968cf134 h1:uYT1HZC8kyKGtAR/Los/azrD8uROgkM8HHuhN2bzPmI=
Expand Down
10 changes: 6 additions & 4 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ func (q *QueueAction) Handle(rule repository.Rule, trigger repository.Trigger, g
return err
}

log.WithFields(log.Fields{
"action": q.action,
"id": id,
}).Debug("enqueue a action to queue")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"action": q.action,
"id": id,
}).Debug("enqueue a action to queue")
}

return nil
})
Expand Down
12 changes: 7 additions & 5 deletions internal/action/dingding.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ func (d DingdingAction) Handle(rule repository.Rule, trigger repository.Trigger,
return err
}

log.WithFields(log.Fields{
"title": rule.Name,
"content": notifyContent,
"mobiles": mobiles,
}).Debug("send message to dingding succeed")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"title": rule.Name,
"content": notifyContent,
"mobiles": mobiles,
}).Debug("send message to dingding succeed")
}

return nil
})
Expand Down
10 changes: 6 additions & 4 deletions internal/action/phone_call_aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ func (w AliyunVoiceCallAction) Handle(rule repository.Rule, trigger repository.T
}).Errorf("send message to aliyun voice failed: %v", err)
}

log.WithFields(log.Fields{
"title": title,
"mobiles": mobiles,
}).Debug("send message to aliyun voice succeed")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"title": title,
"mobiles": mobiles,
}).Debug("send message to aliyun voice succeed")
}

return nil
})
Expand Down
20 changes: 12 additions & 8 deletions internal/job/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ func (a *AggregationJob) groupingEvents(eventRepo repository.EventRepo, groupRep
).(repository.EventStatus)
}

log.WithFields(log.Fields{
"msg_id": msg.ID.Hex(),
"status": msg.Status,
}).Debug("change message status")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"msg_id": msg.ID.Hex(),
"status": msg.Status,
}).Debug("change message status")
}

return eventRepo.UpdateID(msg.ID, msg)
})
Expand Down Expand Up @@ -169,10 +171,12 @@ func (a *AggregationJob) pendingEventGroup(groupRepo repository.EventGroupRepo,
grp.MessageCount = msgCount
grp.Status = repository.EventGroupStatusPending

log.WithFields(log.Fields{
"grp_id": grp.ID.Hex(),
"status": grp.Status,
}).Debug("change group status")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"grp_id": grp.ID.Hex(),
"status": grp.Status,
}).Debug("change group status")
}

err = groupRepo.UpdateID(grp.ID, grp)

Expand Down
8 changes: 6 additions & 2 deletions internal/job/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (d *DistributeLockManager) lock() error {
d.lockID = lock.LockID
d.locked = true

log.Debugf("got distribute lock, owner=%s", d.owner)
if log.DebugEnabled() {
log.Debugf("got distribute lock, owner=%s", d.owner)
}

return nil
}
Expand All @@ -110,7 +112,9 @@ func (d *DistributeLockManager) TryUnLock() error {
d.locked = false
d.lockID = primitive.NilObjectID

log.Debugf("distribute lock has been released")
if log.DebugEnabled() {
log.Debugf("distribute lock has been released")
}

return nil
}
Expand Down
23 changes: 14 additions & 9 deletions internal/job/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ func (a TriggerJob) processEventGroups(groupRepo repository.EventGroupRepo, even
grp.Status = repository.EventGroupStatusOK
}

log.WithFields(log.Fields{
"grp_id": grp.ID,
"status": grp.Status,
}).Debug("change group status for matchedTriggers")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"grp_id": grp.ID,
"status": grp.Status,
}).Debug("change group status for matchedTriggers")
}

grp.Actions = mergeActions(grp.Actions, matchedTriggers)
return groupRepo.UpdateID(grp.ID, grp)
Expand All @@ -145,11 +147,14 @@ func (a TriggerJob) matchedTriggerAction(grp repository.EventGroup, manager acti
maxFailedCount = trigger.FailedCount
}

log.WithFields(log.Fields{
"trigger_id": trigger.ID,
"status": trigger.Status,
"grp_id": grp.ID,
}).Debug("change trigger status")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"trigger_id": trigger.ID,
"status": trigger.Status,
"grp_id": grp.ID,
}).Debug("change trigger status")
}

return hasError, matchedTriggers, maxFailedCount
}

Expand Down
16 changes: 10 additions & 6 deletions internal/matcher/trigger_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ func (tc *TriggerContext) TriggeredTimesInPeriod(periodInMinutes int, triggerSta
triggeredTimes = n
})

log.WithFields(log.Fields{
"times": triggeredTimes,
}).Debugf("TriggeredTimesInPeriod")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"times": triggeredTimes,
}).Debugf("TriggeredTimesInPeriod")
}

return triggeredTimes
}
Expand All @@ -178,9 +180,11 @@ func (tc *TriggerContext) LastTriggeredGroup(triggerStatus string) repository.Ev
}
})

log.WithFields(log.Fields{
"group": lastTriggeredGroup,
}).Debugf("LastTriggeredGroup")
if log.DebugEnabled() {
log.WithFields(log.Fields{
"group": lastTriggeredGroup,
}).Debugf("LastTriggeredGroup")
}

return lastTriggeredGroup
}
Expand Down
9 changes: 7 additions & 2 deletions internal/queue/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,18 @@ func (manager *queueManager) StartWorker(ctx context.Context, workID string) {
manager.info.WorkerNum++
manager.lock.Unlock()

log.Debugf("queue worker [%s] started", workID)
if log.DebugEnabled() {
log.Debugf("queue worker [%s] started", workID)
}

defer func() {
manager.lock.Lock()
manager.info.WorkerNum--
manager.lock.Unlock()

log.Debugf("queue worker [%s] stopped", workID)
if log.DebugEnabled() {
log.Debugf("queue worker [%s] stopped", workID)
}
}()

ticker := time.NewTicker(2 * time.Second)
Expand Down
4 changes: 3 additions & 1 deletion internal/repository/impl/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func NewLockRepo(db *mongo.Database) repository.LockRepo {
if err != nil {
log.Errorf("create unique index for lock collection failed: %v", err)
} else {
log.Debugf("ensure unique index (%s) for lock collection", name)
if log.DebugEnabled() {
log.Debugf("ensure unique index (%s) for lock collection", name)
}
}

return &LockRepo{col: col}
Expand Down
8 changes: 5 additions & 3 deletions migrate/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ func initPredefinedTemplates(conf *configs.Config, repo repository.TemplateRepo)
continue
}

log.WithFields(log.Fields{
"temp": t,
}).Debugf("add predefined template %s with id %s", t.Name, id.Hex())
if log.DebugEnabled() {
log.WithFields(log.Fields{
"temp": t,
}).Debugf("add predefined template %s with id %s", t.Name, id.Hex())
}
} else if err != nil {
log.WithFields(log.Fields{
"temp": t,
Expand Down
12 changes: 8 additions & 4 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ func Send(ctx context.Context, servers []string, token string, meta map[string]i
func sendEventToServer(ctx context.Context, evt extension.CommonEvent, data []byte, adanosServer, adanosToken string) error {
reqURL := fmt.Sprintf("%s/api/events/", strings.TrimRight(adanosServer, "/"))

log.WithFields(log.Fields{
"event": evt,
}).Debugf("request: %v", reqURL)
if log.DebugEnabled() {
log.WithFields(log.Fields{
"event": evt,
}).Debugf("request: %v", reqURL)
}

client := &http.Client{}
req, err := http.NewRequestWithContext(ctx, "POST", reqURL, bytes.NewReader(data))
Expand All @@ -136,6 +138,8 @@ func sendEventToServer(ctx context.Context, evt extension.CommonEvent, data []by
return errors.Wrap(err, "read response body failed")
}

log.Debugf("response: %v", string(respBody))
if log.DebugEnabled() {
log.Debugf("response: %v", string(respBody))
}
return nil
}

0 comments on commit 2cbe8ea

Please sign in to comment.