-
Notifications
You must be signed in to change notification settings - Fork 39
/
meter.go
91 lines (75 loc) · 1.99 KB
/
meter.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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2016
package instana
import (
"runtime"
"time"
"github.com/instana/go-sensor/acceptor"
)
// SnapshotS struct to hold snapshot data
type SnapshotS acceptor.RuntimeInfo
// MemoryS struct to hold snapshot data
type MemoryS acceptor.MemoryStats
// MetricsS struct to hold snapshot data
type MetricsS acceptor.Metrics
// EntityData struct to hold snapshot data
type EntityData acceptor.GoProcessData
type meterS struct {
numGC uint32
done chan struct{}
}
func newMeter(logger LeveledLogger) *meterS {
logger.Debug("initializing meter")
return &meterS{
done: make(chan struct{}, 1),
}
}
func (m *meterS) Run(collectInterval time.Duration) {
ticker := time.NewTicker(collectInterval)
defer ticker.Stop()
for {
select {
case <-m.done:
return
case <-ticker.C:
if sensor.Agent().Ready() {
go sensor.Agent().SendMetrics(m.collectMetrics())
}
}
}
}
func (m *meterS) Stop() {
m.done <- struct{}{}
}
func (m *meterS) collectMemoryMetrics() acceptor.MemoryStats {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
ret := acceptor.MemoryStats{
Alloc: memStats.Alloc,
TotalAlloc: memStats.TotalAlloc,
Sys: memStats.Sys,
Lookups: memStats.Lookups,
Mallocs: memStats.Mallocs,
Frees: memStats.Frees,
HeapAlloc: memStats.HeapAlloc,
HeapSys: memStats.HeapSys,
HeapIdle: memStats.HeapIdle,
HeapInuse: memStats.HeapInuse,
HeapReleased: memStats.HeapReleased,
HeapObjects: memStats.HeapObjects,
PauseTotalNs: memStats.PauseTotalNs,
NumGC: memStats.NumGC,
GCCPUFraction: memStats.GCCPUFraction}
if m.numGC < memStats.NumGC {
ret.PauseNs = memStats.PauseNs[(memStats.NumGC+255)%256]
m.numGC = memStats.NumGC
}
return ret
}
func (m *meterS) collectMetrics() acceptor.Metrics {
return acceptor.Metrics{
CgoCall: runtime.NumCgoCall(),
Goroutine: runtime.NumGoroutine(),
MemoryStats: m.collectMemoryMetrics(),
}
}