forked from yvasiyarov/gorelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_metrics.go
196 lines (177 loc) · 4.83 KB
/
runtime_metrics.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package gorelic
import (
"fmt"
"github.com/yvasiyarov/newrelic_platform_go"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
"time"
)
const linuxSystemQueryInterval = 60
// Number of goroutines metrica
type noGoroutinesMetrica struct{}
func (metrica *noGoroutinesMetrica) GetName() string {
return "Runtime/General/NOGoroutines"
}
func (metrica *noGoroutinesMetrica) GetUnits() string {
return "goroutines"
}
func (metrica *noGoroutinesMetrica) GetValue() (float64, error) {
return float64(runtime.NumGoroutine()), nil
}
// Number of CGO calls metrica
type noCgoCallsMetrica struct {
lastValue int64
}
func (metrica *noCgoCallsMetrica) GetName() string {
return "Runtime/General/NOCgoCalls"
}
func (metrica *noCgoCallsMetrica) GetUnits() string {
return "calls"
}
func (metrica *noCgoCallsMetrica) GetValue() (float64, error) {
currentValue := runtime.NumCgoCall()
value := float64(currentValue - metrica.lastValue)
metrica.lastValue = currentValue
return value, nil
}
//OS specific metrics data source interface
type iSystemMetricaDataSource interface {
GetValue(key string) (float64, error)
}
// iSystemMetricaDataSource fabrica
func newSystemMetricaDataSource() iSystemMetricaDataSource {
var ds iSystemMetricaDataSource
switch runtime.GOOS {
default:
ds = &systemMetricaDataSource{}
case "linux":
ds = &linuxSystemMetricaDataSource{
systemData: make(map[string]string),
}
}
return ds
}
//Default implementation of iSystemMetricaDataSource. Just return an error
type systemMetricaDataSource struct{}
func (ds *systemMetricaDataSource) GetValue(key string) (float64, error) {
return 0, fmt.Errorf("this metrica was not implemented yet for %s", runtime.GOOS)
}
// Linux OS implementation of ISystemMetricaDataSource
type linuxSystemMetricaDataSource struct {
lastUpdate time.Time
systemData map[string]string
}
func (ds *linuxSystemMetricaDataSource) GetValue(key string) (float64, error) {
if err := ds.checkAndUpdateData(); err != nil {
return 0, err
} else if val, ok := ds.systemData[key]; !ok {
return 0, fmt.Errorf("system data with key %s was not found", key)
} else if key == "VmSize" || key == "VmPeak" || key == "VmHWM" || key == "VmRSS" {
valueParts := strings.Split(val, " ")
if len(valueParts) != 2 {
return 0, fmt.Errorf("invalid format for value %s", key)
}
valConverted, err := strconv.ParseFloat(valueParts[0], 64)
if err != nil {
return 0, err
}
switch valueParts[1] {
case "kB":
valConverted *= 1 << 10
case "mB":
valConverted *= 1 << 20
case "gB":
valConverted *= 1 << 30
}
return valConverted, nil
} else if valConverted, err := strconv.ParseFloat(val, 64); err != nil {
return valConverted, nil
} else {
return valConverted, nil
}
}
func (ds *linuxSystemMetricaDataSource) checkAndUpdateData() error {
startTime := time.Now()
if startTime.Sub(ds.lastUpdate) > time.Second*linuxSystemQueryInterval {
path := fmt.Sprintf("/proc/%d/status", os.Getpid())
rawStats, err := ioutil.ReadFile(path)
if err != nil {
return err
}
lines := strings.Split(string(rawStats), "\n")
for _, line := range lines {
parts := strings.Split(line, ":")
if len(parts) == 2 {
k := strings.TrimSpace(parts[0])
v := strings.TrimSpace(parts[1])
ds.systemData[k] = v
}
}
ds.lastUpdate = startTime
}
return nil
}
// OS specific metrica
type systemMetrica struct {
sourceKey string
newrelicName string
units string
dataSource iSystemMetricaDataSource
}
func (metrica *systemMetrica) GetName() string {
return metrica.newrelicName
}
func (metrica *systemMetrica) GetUnits() string {
return metrica.units
}
func (metrica *systemMetrica) GetValue() (float64, error) {
return metrica.dataSource.GetValue(metrica.sourceKey)
}
func addRuntimeMericsToComponent(component newrelic_platform_go.IComponent) {
component.AddMetrica(&noGoroutinesMetrica{})
component.AddMetrica(&noCgoCallsMetrica{})
ds := newSystemMetricaDataSource()
metrics := []*systemMetrica{
&systemMetrica{
sourceKey: "Threads",
units: "Threads",
newrelicName: "Runtime/System/Threads",
},
&systemMetrica{
sourceKey: "FDSize",
units: "fd",
newrelicName: "Runtime/System/FDSize",
},
// Peak virtual memory size
&systemMetrica{
sourceKey: "VmPeak",
units: "bytes",
newrelicName: "Runtime/System/Memory/VmPeakSize",
},
//Virtual memory size
&systemMetrica{
sourceKey: "VmSize",
units: "bytes",
newrelicName: "Runtime/System/Memory/VmCurrent",
},
//Peak resident set size
&systemMetrica{
sourceKey: "VmHWM",
units: "bytes",
newrelicName: "Runtime/System/Memory/RssPeak",
},
//Resident set size
&systemMetrica{
sourceKey: "VmRSS",
units: "bytes",
newrelicName: "Runtime/System/Memory/RssCurrent",
},
}
for _, m := range metrics {
m.dataSource = ds
component.AddMetrica(m)
}
}