...
1 package profiling
2
3 import (
4 "flag"
5 "sync"
6 "time"
7
8 "golang.conradwood.net/go-easyops/prometheus"
9 )
10
11 var (
12 prof_lock sync.Mutex
13 rpcCtr = 0
14 sqlCtr = 0
15 serverRPCCtr = 0
16 ms = flag.Int("ge_profiling_interval", 300, "interval in `milliseconds` to collect codepath stuff")
17 cm_total = prometheus.NewCounterVec(
18 prometheus.CounterOpts{
19 Name: "ge_profiling_total_samples",
20 Help: "total number of codepath samples (whilst at least one server grpc is being executed)",
21 },
22 []string{"rpcactive"},
23 )
24 cm_ctr = prometheus.NewCounterVec(
25 prometheus.CounterOpts{
26 Name: "ge_profiling_samples_wait",
27 Help: "total number of codepath samples where at least one thread was blocked",
28 },
29 []string{"codepath"},
30 )
31 gm_ctr = prometheus.NewGaugeVec(
32 prometheus.GaugeOpts{
33 Name: "ge_profiling_current_blocks",
34 Help: "threads within codepath",
35 },
36 []string{"codepath"},
37 )
38 )
39
40 func init() {
41 prometheus.MustRegister(cm_total, cm_ctr, gm_ctr)
42 go statsCheck()
43 }
44
45 func statsCheck() {
46
47 for {
48 prof_lock.Lock()
49 gm_ctr.With(prometheus.Labels{"codepath": "grpccall"}).Set(float64(rpcCtr))
50 gm_ctr.With(prometheus.Labels{"codepath": "sqlquery"}).Set(float64(sqlCtr))
51 if serverRPCCtr == 0 {
52 cm_total.With(prometheus.Labels{"rpcactive": "false"}).Inc()
53 prof_lock.Unlock()
54 time.Sleep(time.Duration(*ms) * time.Millisecond)
55 continue
56 }
57 cm_total.With(prometheus.Labels{"rpcactive": "true"}).Inc()
58
59
60
61 a := serverRPCCtr
62 if a > 0 {
63 cm_ctr.With(prometheus.Labels{"codepath": "serving"}).Add(float64(a))
64 }
65 a = rpcCtr
66 if a > 0 {
67 cm_ctr.With(prometheus.Labels{"codepath": "grpccall"}).Add(float64(a))
68 }
69 a = sqlCtr
70 if a > 0 {
71 cm_ctr.With(prometheus.Labels{"codepath": "sqlquery"}).Add(float64(a))
72 }
73 prof_lock.Unlock()
74 time.Sleep(time.Duration(*ms) * time.Millisecond)
75 }
76 }
77
78 func ClientRpcEntered() {
79 prof_lock.Lock()
80 defer prof_lock.Unlock()
81 rpcCtr++
82 }
83 func ClientRpcDone() {
84 prof_lock.Lock()
85 defer prof_lock.Unlock()
86 rpcCtr--
87 }
88 func ServerRpcEntered() {
89 prof_lock.Lock()
90 serverRPCCtr++
91 prof_lock.Unlock()
92 }
93 func ServerRpcDone() {
94 prof_lock.Lock()
95 serverRPCCtr--
96 prof_lock.Unlock()
97 }
98 func SqlEntered() {
99 prof_lock.Lock()
100 defer prof_lock.Unlock()
101 sqlCtr++
102 }
103 func SqlDone() {
104 prof_lock.Lock()
105 defer prof_lock.Unlock()
106 sqlCtr--
107 }
108
View as plain text