Skip to content

Commit 965fa9e

Browse files
committed
add metrics for golang goroutineNum, gcNum and gcPauseTime
1 parent d34e4c7 commit 965fa9e

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sentry sdk for sending metrics to sentry_agent or sentry_server in golang
33

44
# Install
55
```
6-
https://github.com/sentrycloud/sentry-sdk-go
6+
go get https://github.com/sentrycloud/sentry-sdk-go
77
```
88

99
# Usage

gcinfo.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sentry
2+
3+
import (
4+
"runtime"
5+
"runtime/debug"
6+
"time"
7+
)
8+
9+
const GcInterval = 10
10+
11+
var (
12+
goNumCollector = GetCollector("sentry_go_num", nil, Sum, GcInterval)
13+
gcNumCollector = GetCollector("sentry_gc_num", nil, Sum, GcInterval)
14+
gcPauseCollector = GetCollector("sentry_gc_pause", nil, Sum, GcInterval)
15+
16+
lastGCNum int64 = 0
17+
lastGCPause time.Duration = 0
18+
)
19+
20+
func startCollectGC() {
21+
t := time.NewTicker(GcInterval * time.Second)
22+
23+
for {
24+
select {
25+
case <-t.C:
26+
collectGC()
27+
}
28+
}
29+
}
30+
31+
func collectGC() {
32+
goNum := runtime.NumGoroutine()
33+
goNumCollector.Put(float64(goNum))
34+
35+
stats := &debug.GCStats{}
36+
debug.ReadGCStats(stats)
37+
38+
gcNum := stats.NumGC - lastGCNum
39+
pauseTime := stats.PauseTotal - lastGCPause
40+
41+
gcNumCollector.Put(float64(gcNum))
42+
gcPauseCollector.Put(float64(pauseTime.Milliseconds()))
43+
44+
lastGCNum = stats.NumGC
45+
lastGCPause = stats.PauseTotal
46+
}

init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ func init() {
44
dataChan = make(chan DataPoint, DataChanSize)
55

66
go collect()
7+
go startCollectGC()
78
}

sentry_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func aggregatorToString(aggregator CollectorType) string {
2424
}
2525

2626
func testCollector(aggregator CollectorType, value float64) {
27-
metric := "testApp_http_qps"
27+
metric := "myapp_http_qps"
2828
tags := map[string]string{
2929
"from": "iOS",
3030
"aggregator": aggregatorToString(aggregator),
@@ -61,3 +61,8 @@ func TestAllCollectorsInGoroutine(t *testing.T) {
6161
go testInGoroutine(&wg, Min)
6262
wg.Wait()
6363
}
64+
65+
func TestGcCollector(t *testing.T) {
66+
SetReportURL("http://127.0.0.1:51001/server/api/putMetrics")
67+
startCollectGC()
68+
}

0 commit comments

Comments
 (0)