forked from GoBelieveIO/im_service
/
monitoring.go
79 lines (66 loc) · 1.91 KB
/
monitoring.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
package main
import "net/http"
import "encoding/json"
import "os"
import "runtime/pprof"
import log "github.com/golang/glog"
type ServerSummary struct {
nconnections int64
nclients int64
in_message_count int64
out_message_count int64
}
func NewServerSummary() *ServerSummary {
s := new(ServerSummary)
return s
}
func Summary(rw http.ResponseWriter, req *http.Request) {
obj := make(map[string]interface{})
obj["connection_count"] = server_summary.nconnections
obj["client_count"] = server_summary.nclients
obj["in_message_count"] = server_summary.in_message_count
obj["out_message_count"] = server_summary.out_message_count
res, err := json.Marshal(obj)
if err != nil {
log.Info("json marshal:", err)
return
}
rw.Header().Add("Content-Type", "application/json")
_, err = rw.Write(res)
if err != nil {
log.Info("write err:", err)
}
return
}
func Stack(rw http.ResponseWriter, req *http.Request) {
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
rw.WriteHeader(200)
}
func WriteHttpError(status int, err string, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
obj := make(map[string]interface{})
meta := make(map[string]interface{})
meta["code"] = status
meta["message"] = err
obj["meta"] = meta
b, _ := json.Marshal(obj)
w.WriteHeader(status)
w.Write(b)
}
func WriteHttpObj(data map[string]interface{}, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
obj := make(map[string]interface{})
obj["data"] = data
b, _ := json.Marshal(obj)
w.Write(b)
}
func StartHttpServer(addr string) {
http.HandleFunc("/summary", Summary)
http.HandleFunc("/stack", Stack)
//rpc function
http.HandleFunc("/post_group_notification", PostGroupNotification)
http.HandleFunc("/post_im_message", PostIMMessage)
http.HandleFunc("/load_latest_message", LoadLatestMessage)
http.HandleFunc("/post_system_message", SendSystemMessage)
HTTPService(addr, nil)
}