-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathruntime.go
48 lines (40 loc) · 908 Bytes
/
runtime.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
package stats
import (
"context"
"runtime"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)
var (
MNumGoRoutines = stats.Int64("runtime/goroutines", "The number of goroutines", "1")
GoroutinesNumView = &view.View{
Name: "runtime/goroutines",
Measure: MNumGoRoutines,
Description: "The number of goroutines",
Aggregation: view.Sum(),
}
)
func init() {
view.Register(GoroutinesNumView)
}
func RunGoroutineStat() {
RunCancellableGoroutineStat(context.Background())
}
func RunCancellableGoroutineStat(ctx context.Context) {
numgoroutines := int64(0)
for {
select {
default:
time.Sleep(time.Second)
newnumgoroutines := int64(runtime.NumGoroutine())
diff := newnumgoroutines - numgoroutines
numgoroutines = newnumgoroutines
if diff != 0 {
stats.Record(context.TODO(), MNumGoRoutines.M(diff))
}
case <-ctx.Done():
return
}
}
}