Improve monitoring management module
Showing
4 changed files
with
57 additions
and
12 deletions
| ... | @@ -2,22 +2,56 @@ package beego | ... | @@ -2,22 +2,56 @@ package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | "github.com/astaxie/beego/admin" | ||
| 5 | "net/http" | 6 | "net/http" |
| 7 | "time" | ||
| 6 | ) | 8 | ) |
| 7 | 9 | ||
| 8 | var BeeAdminApp *AdminApp | 10 | var BeeAdminApp *AdminApp |
| 9 | 11 | ||
| 12 | //func MyFilterMonitor(method, requestPath string, t time.Duration) bool { | ||
| 13 | // if method == "POST" { | ||
| 14 | // return false | ||
| 15 | // } | ||
| 16 | // if t.Nanoseconds() < 100 { | ||
| 17 | // return false | ||
| 18 | // } | ||
| 19 | // if strings.HasPrefix(requestPath, "/astaxie") { | ||
| 20 | // return false | ||
| 21 | // } | ||
| 22 | // return true | ||
| 23 | //} | ||
| 24 | |||
| 25 | //beego.FilterMonitorFunc = MyFilterMonitor | ||
| 26 | var FilterMonitorFunc func(string, string, time.Duration) bool | ||
| 27 | |||
| 10 | func init() { | 28 | func init() { |
| 11 | BeeAdminApp = &AdminApp{ | 29 | BeeAdminApp = &AdminApp{ |
| 12 | routers: make(map[string]http.HandlerFunc), | 30 | routers: make(map[string]http.HandlerFunc), |
| 13 | } | 31 | } |
| 14 | BeeAdminApp.Route("/", AdminIndex) | 32 | BeeAdminApp.Route("/", AdminIndex) |
| 33 | BeeAdminApp.Route("/qps", QpsIndex) | ||
| 34 | BeeAdminApp.Route("/prof", ProfIndex) | ||
| 35 | FilterMonitorFunc = func(string, string, time.Duration) bool { return true } | ||
| 15 | } | 36 | } |
| 16 | 37 | ||
| 17 | func AdminIndex(rw http.ResponseWriter, r *http.Request) { | 38 | func AdminIndex(rw http.ResponseWriter, r *http.Request) { |
| 18 | rw.Write([]byte("Welcome to Admin Dashboard")) | 39 | rw.Write([]byte("Welcome to Admin Dashboard")) |
| 19 | } | 40 | } |
| 20 | 41 | ||
| 42 | func QpsIndex(rw http.ResponseWriter, r *http.Request) { | ||
| 43 | info := admin.UrlMap.GetMap(rw) | ||
| 44 | } | ||
| 45 | func ProfIndex(rw http.ResponseWriter, r *http.Request) { | ||
| 46 | r.ParseForm() | ||
| 47 | command := r.Form.Get("command") | ||
| 48 | if command != "" { | ||
| 49 | admin.ProcessInput(command) | ||
| 50 | } else { | ||
| 51 | rw.Write([]byte("request url like '/prof?command=lookup goroutine'")) | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 21 | type AdminApp struct { | 55 | type AdminApp struct { |
| 22 | routers map[string]http.HandlerFunc | 56 | routers map[string]http.HandlerFunc |
| 23 | } | 57 | } | ... | ... |
| 1 | package admin | 1 | package admin |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "io" |
| 5 | "strconv" | ||
| 5 | "sync" | 6 | "sync" |
| 6 | "time" | 7 | "time" |
| 7 | ) | 8 | ) |
| ... | @@ -60,14 +61,21 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri | ... | @@ -60,14 +61,21 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri |
| 60 | } | 61 | } |
| 61 | } | 62 | } |
| 62 | 63 | ||
| 63 | func (m *UrlMap) GetMap() []byte { | 64 | func (m *UrlMap) GetMap(rw io.Writer) { |
| 64 | m.lock.RLock() | 65 | m.lock.RLock() |
| 65 | defer m.lock.RUnlock() | 66 | defer m.lock.RUnlock() |
| 66 | r, err := json.Marshal(m.urlmap) | 67 | rw.Write([]byte("requestURL avgTime")) |
| 67 | if err != nil { | 68 | for k, v := range m.urlmap { |
| 68 | return []byte("") | 69 | rw.Write([]byte(k + "")) |
| 70 | for kk, vv := range v { | ||
| 71 | rw.Write([]byte(kk)) | ||
| 72 | rw.Write([]byte(strconv.FormatInt(vv.RequestNum, 10))) | ||
| 73 | rw.Write([]byte(strconv.FormatInt(int64(vv.TotalTime), 10))) | ||
| 74 | rw.Write([]byte(strconv.FormatInt(int64(vv.MaxTime), 10))) | ||
| 75 | rw.Write([]byte(strconv.FormatInt(int64(vv.MinTime), 10))) | ||
| 76 | rw.Write([]byte(strconv.FormatInt(int64(vv.TotalTime)/vv.RequestNum, 10))) | ||
| 77 | } | ||
| 69 | } | 78 | } |
| 70 | return r | ||
| 71 | } | 79 | } |
| 72 | 80 | ||
| 73 | var StatisticsMap *UrlMap | 81 | var StatisticsMap *UrlMap | ... | ... |
| 1 | package admin | 1 | package admin |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "os" | ||
| 4 | "testing" | 5 | "testing" |
| 5 | "time" | 6 | "time" |
| 6 | ) | 7 | ) |
| ... | @@ -13,5 +14,5 @@ func TestStatics(t *testing.T) { | ... | @@ -13,5 +14,5 @@ func TestStatics(t *testing.T) { |
| 13 | StatisticsMap.AddStatistics("POST", "/api/user/astaxie", "&admin.user", time.Duration(1200000)) | 14 | StatisticsMap.AddStatistics("POST", "/api/user/astaxie", "&admin.user", time.Duration(1200000)) |
| 14 | StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(1300000)) | 15 | StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(1300000)) |
| 15 | StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400000)) | 16 | StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400000)) |
| 16 | s := StatisticsMap.GetMap() | 17 | StatisticsMap.GetMap(os.Stdout) |
| 17 | } | 18 | } | ... | ... |
| ... | @@ -846,12 +846,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -846,12 +846,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 846 | Admin: | 846 | Admin: |
| 847 | //admin module record QPS | 847 | //admin module record QPS |
| 848 | if EnableAdmin { | 848 | if EnableAdmin { |
| 849 | if runrouter != nil { | 849 | timeend := time.Since(starttime) |
| 850 | go admin.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.controllerType.Name(), time.Since(starttime)) | 850 | if FilterMonitorFunc(r.Method, requestPath, timeend) { |
| 851 | } else { | 851 | if runrouter != nil { |
| 852 | go admin.StatisticsMap.AddStatistics(r.Method, requestPath, "", time.Since(starttime)) | 852 | go admin.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.controllerType.Name(), timeend) |
| 853 | } else { | ||
| 854 | go admin.StatisticsMap.AddStatistics(r.Method, requestPath, "", timeend) | ||
| 855 | } | ||
| 853 | } | 856 | } |
| 854 | |||
| 855 | } | 857 | } |
| 856 | } | 858 | } |
| 857 | 859 | ... | ... |
-
Please register or sign in to post a comment