-
Notifications
You must be signed in to change notification settings - Fork 22
/
srv_stats_http.go
130 lines (112 loc) · 3.17 KB
/
srv_stats_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// +build httpstats
package go9p
import (
"fmt"
"io"
"net/http"
"sync"
)
var mux sync.RWMutex
var stat map[string]http.Handler
var httponce sync.Once
func register(s string, h http.Handler) {
mux.Lock()
if stat == nil {
stat = make(map[string]http.Handler)
}
if h == nil {
delete(stat, s)
} else {
stat[s] = h
}
mux.Unlock()
}
func (srv *Srv) statsRegister() {
httponce.Do(func() {
http.HandleFunc("/go9p/", StatsHandler)
go http.ListenAndServe(":6060", nil)
})
register("/go9p/srv/"+srv.Id, srv)
}
func (srv *Srv) statsUnregister() {
register("/go9p/srv/"+srv.Id, nil)
}
func (srv *Srv) ServeHTTP(c http.ResponseWriter, r *http.Request) {
io.WriteString(c, fmt.Sprintf("<html><body><h1>Server %s</h1>", srv.Id))
defer io.WriteString(c, "</body></html>")
// connections
io.WriteString(c, "<h2>Connections</h2><p>")
srv.Lock()
defer srv.Unlock()
if len(srv.conns) == 0 {
io.WriteString(c, "none")
return
}
for _, conn := range srv.conns {
io.WriteString(c, fmt.Sprintf("<a href='/go9p/srv/%s/conn/%s'>%s</a><br>", srv.Id, conn.Id, conn.Id))
}
}
func (conn *Conn) statsRegister() {
register("/go9p/srv/"+conn.Srv.Id+"/conn/"+conn.Id, conn)
}
func (conn *Conn) statsUnregister() {
register("/go9p/srv/"+conn.Srv.Id+"/conn/"+conn.Id, nil)
}
func (conn *Conn) ServeHTTP(c http.ResponseWriter, r *http.Request) {
io.WriteString(c, fmt.Sprintf("<html><body><h1>Connection %s/%s</h1>", conn.Srv.Id, conn.Id))
defer io.WriteString(c, "</body></html>")
// statistics
conn.Lock()
io.WriteString(c, fmt.Sprintf("<p>Number of processed requests: %d", conn.nreqs))
io.WriteString(c, fmt.Sprintf("<br>Sent %v bytes", conn.rsz))
io.WriteString(c, fmt.Sprintf("<br>Received %v bytes", conn.tsz))
io.WriteString(c, fmt.Sprintf("<br>Pending requests: %d max %d", conn.npend, conn.maxpend))
io.WriteString(c, fmt.Sprintf("<br>Number of reads: %d", conn.nreads))
io.WriteString(c, fmt.Sprintf("<br>Number of writes: %d", conn.nwrites))
conn.Unlock()
// fcalls
if conn.Debuglevel&DbgLogFcalls != 0 {
fs := conn.Srv.Log.Filter(conn, DbgLogFcalls)
io.WriteString(c, fmt.Sprintf("<h2>Last %d 9P messages</h2>", len(fs)))
for i, l := range fs {
fc := l.Data.(*Fcall)
if fc.Type == 0 {
continue
}
lbl := ""
if fc.Type%2 == 0 {
// try to find the response for the T message
for j := i + 1; j < len(fs); j++ {
rc := fs[j].Data.(*Fcall)
if rc.Tag == fc.Tag {
lbl = fmt.Sprintf("<a href='#fc%d'>➴</a>", j)
break
}
}
} else {
// try to find the request for the R message
for j := i - 1; j >= 0; j-- {
tc := fs[j].Data.(*Fcall)
if tc.Tag == fc.Tag {
lbl = fmt.Sprintf("<a href='#fc%d'>➶</a>", j)
break
}
}
}
io.WriteString(c, fmt.Sprintf("<br id='fc%d'>%d: %s%s", i, i, fc, lbl))
}
}
}
func StatsHandler(c http.ResponseWriter, r *http.Request) {
mux.RLock()
if v, ok := stat[r.URL.Path]; ok {
v.ServeHTTP(c, r)
} else if r.URL.Path == "/go9p/" {
io.WriteString(c, fmt.Sprintf("<html><body><br><h1>On offer: </h1><br>"))
for v := range stat {
io.WriteString(c, fmt.Sprintf("<a href='%s'>%s</a><br>", v, v))
}
io.WriteString(c, "</body></html>")
}
mux.RUnlock()
}