Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
097bcb3b
Commit
097bcb3b
authored
Nov 15, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve monitoring management module
parent
18335194
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
12 deletions
+57
-12
admin.go
admin.go
+34
-0
statistics.go
admin/statistics.go
+14
-6
statistics_test.go
admin/statistics_test.go
+2
-1
router.go
router.go
+7
-5
No files found.
admin.go
View file @
097bcb3b
...
...
@@ -2,22 +2,56 @@ package beego
import
(
"fmt"
"github.com/astaxie/beego/admin"
"net/http"
"time"
)
var
BeeAdminApp
*
AdminApp
//func MyFilterMonitor(method, requestPath string, t time.Duration) bool {
// if method == "POST" {
// return false
// }
// if t.Nanoseconds() < 100 {
// return false
// }
// if strings.HasPrefix(requestPath, "/astaxie") {
// return false
// }
// return true
//}
//beego.FilterMonitorFunc = MyFilterMonitor
var
FilterMonitorFunc
func
(
string
,
string
,
time
.
Duration
)
bool
func
init
()
{
BeeAdminApp
=
&
AdminApp
{
routers
:
make
(
map
[
string
]
http
.
HandlerFunc
),
}
BeeAdminApp
.
Route
(
"/"
,
AdminIndex
)
BeeAdminApp
.
Route
(
"/qps"
,
QpsIndex
)
BeeAdminApp
.
Route
(
"/prof"
,
ProfIndex
)
FilterMonitorFunc
=
func
(
string
,
string
,
time
.
Duration
)
bool
{
return
true
}
}
func
AdminIndex
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
rw
.
Write
([]
byte
(
"Welcome to Admin Dashboard"
))
}
func
QpsIndex
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
info
:=
admin
.
UrlMap
.
GetMap
(
rw
)
}
func
ProfIndex
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
r
.
ParseForm
()
command
:=
r
.
Form
.
Get
(
"command"
)
if
command
!=
""
{
admin
.
ProcessInput
(
command
)
}
else
{
rw
.
Write
([]
byte
(
"request url like '/prof?command=lookup goroutine'"
))
}
}
type
AdminApp
struct
{
routers
map
[
string
]
http
.
HandlerFunc
}
...
...
admin/statistics.go
View file @
097bcb3b
package
admin
import
(
"encoding/json"
"io"
"strconv"
"sync"
"time"
)
...
...
@@ -60,14 +61,21 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
}
}
func
(
m
*
UrlMap
)
GetMap
(
)
[]
byte
{
func
(
m
*
UrlMap
)
GetMap
(
rw
io
.
Writer
)
{
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
r
,
err
:=
json
.
Marshal
(
m
.
urlmap
)
if
err
!=
nil
{
return
[]
byte
(
""
)
rw
.
Write
([]
byte
(
"requestURL avgTime"
))
for
k
,
v
:=
range
m
.
urlmap
{
rw
.
Write
([]
byte
(
k
+
""
))
for
kk
,
vv
:=
range
v
{
rw
.
Write
([]
byte
(
kk
))
rw
.
Write
([]
byte
(
strconv
.
FormatInt
(
vv
.
RequestNum
,
10
)))
rw
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
vv
.
TotalTime
),
10
)))
rw
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
vv
.
MaxTime
),
10
)))
rw
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
vv
.
MinTime
),
10
)))
rw
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
vv
.
TotalTime
)
/
vv
.
RequestNum
,
10
)))
}
}
return
r
}
var
StatisticsMap
*
UrlMap
...
...
admin/statistics_test.go
View file @
097bcb3b
package
admin
import
(
"os"
"testing"
"time"
)
...
...
@@ -13,5 +14,5 @@ func TestStatics(t *testing.T) {
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/astaxie"
,
"&admin.user"
,
time
.
Duration
(
1200000
))
StatisticsMap
.
AddStatistics
(
"POST"
,
"/api/user/xiemengjun"
,
"&admin.user"
,
time
.
Duration
(
1300000
))
StatisticsMap
.
AddStatistics
(
"DELETE"
,
"/api/user"
,
"&admin.user"
,
time
.
Duration
(
1400000
))
s
:=
StatisticsMap
.
GetMap
(
)
StatisticsMap
.
GetMap
(
os
.
Stdout
)
}
router.go
View file @
097bcb3b
...
...
@@ -846,12 +846,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
Admin
:
//admin module record QPS
if
EnableAdmin
{
if
runrouter
!=
nil
{
go
admin
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
requestPath
,
runrouter
.
controllerType
.
Name
(),
time
.
Since
(
starttime
))
}
else
{
go
admin
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
requestPath
,
""
,
time
.
Since
(
starttime
))
timeend
:=
time
.
Since
(
starttime
)
if
FilterMonitorFunc
(
r
.
Method
,
requestPath
,
timeend
)
{
if
runrouter
!=
nil
{
go
admin
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
requestPath
,
runrouter
.
controllerType
.
Name
(),
timeend
)
}
else
{
go
admin
.
StatisticsMap
.
AddStatistics
(
r
.
Method
,
requestPath
,
""
,
timeend
)
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment