-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
go-metric.go
204 lines (193 loc) · 6.87 KB
/
go-metric.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
197
198
199
200
201
202
203
204
package metric
import (
"fmt"
"runtime"
)
func newGoMetricCollector() *Collector {
golang := &golang{
goRoutineMetric: goMetric{
Name: "go_goroutines",
Help: "Number of goroutines that currently exist.",
GetFunc: func() float64 { return float64(runtime.NumGoroutine()) },
},
goProcessMetric: goMetric{
Name: "go_threads",
Help: "Number of OS threads created",
GetFunc: func() float64 {
n, _ := runtime.ThreadCreateProfile(nil)
return float64(n)
},
},
goCPUMetric: goMetric{
Name: "go_cpu_used",
Help: " the number of logical CPUs usable by the current process.",
GetFunc: func() float64 { return float64(runtime.NumCPU() )},
},
goMemStateMetrics: []goMetric{
{
Name: memstatNamespace("alloc_bytes"),
Help: "Number of bytes allocated and still in use.",
MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.Alloc)},
},
{
Name: memstatNamespace("alloc_bytes_total"),
Help: "Total number of bytes allocated, even if freed.",
MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.TotalAlloc)},
},
{
Name: memstatNamespace("sys_bytes"),
Help: "Number of bytes obtained from system.",
MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.Sys)},
},
{
Name: memstatNamespace("mallocs_total"),
Help: "Total number of mallocs.",
MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.Mallocs) },
},
{
Name: memstatNamespace("frees_total"),
Help: "Total number of frees.",
MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.Frees)},
},
//{
// Name: memstatNamespace("lookups_total"),
// Help: "Total number of pointer lookups.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.Lookups)},
//},
//{
// Name: memstatNamespace("heap_alloc_bytes"),
// Help: "Number of heap bytes allocated and still in use.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.HeapAlloc},
//},
//{
// Name: memstatNamespace("heap_sys_bytes"),
// Help: "Number of heap bytes obtained from system.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.HeapSys)},
//},
//{
// Name: memstatNamespace("heap_idle_bytes"),
// Help: "Number of heap bytes waiting to be used.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.HeapIdle)},
//},
//{
// Name: memstatNamespace("heap_inuse_bytes"),
// Help: "Number of heap bytes that are in use.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.HeapInuse) },
//},
//{
// Name: memstatNamespace("heap_released_bytes"),
// Help: "Number of heap bytes released to OS.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.HeapReleased},
//},
//{
// Name: memstatNamespace("heap_objects"),
// Help: "Number of allocated objects.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.HeapObjects},
//},
//{
// Name: memstatNamespace("stack_inuse_bytes"),
// Help: "Number of bytes in use by the stack allocator.",
// MemGetFunc: func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse},
//},
//{
// Name: memstatNamespace("stack_sys_bytes"),
// Help: "Number of bytes obtained from system for stack allocator.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.StackSys},
//},
//{
// Name: memstatNamespace("mspan_inuse_bytes"),
// Help: "Number of bytes in use by mspan structures.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.MSpanInuse},
//},
//{
// Name: memstatNamespace("mspan_sys_bytes"),
// Help: "Number of bytes used for mspan structures obtained from system.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.MSpanSys) },
//},
//{
// Name: memstatNamespace("mcache_inuse_bytes"),
// Help: "Number of bytes in use by mcache structures.",
// MemGetFunc: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) },
//},
//{
// Name: memstatNamespace("mcache_sys_bytes"),
// Help: "Number of bytes used for mcache structures obtained from system.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.MCacheSys) },
//},
//{
// Name: memstatNamespace("buck_hash_sys_bytes"),
// Help: "Number of bytes used by the profiling bucket hash table.",
// MemGetFunc: func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys},
//},
//{
// Name: memstatNamespace("gc_sys_bytes"),
// Help: "Number of bytes used for garbage collection system metadata.",
// MemGetFunc: func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) },
//},
//{
// Name: memstatNamespace("other_sys_bytes"),
// Help: "Number of bytes used for other system allocations.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.OtherSys)},
//},
//{
// Name: memstatNamespace("next_gc_bytes"),
// Help: "Number of heap bytes when next garbage collection will take place.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.NextGC) },
//},
//{
// Name: memstatNamespace("last_gc_time_seconds"),
// Help: "Number of seconds since 1970 of last garbage collection.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return float64(ms.LastGC) / 1e9},
//},
//{
// Name: memstatNamespace("gc_cpu_fraction"),
// Help: "The fraction of this program's available CPU time used by the GC since the program started.",
// MemGetFunc: func(ms *runtime.MemStats) float64 {return ms.GCCPUFraction },
//},
},
}
return NewCollector("golang_metrics", golang)
}
func memstatNamespace(s string) string {
return fmt.Sprintf("go_memstats_%s", s)
}
type golang struct {
goRoutineMetric goMetric
goProcessMetric goMetric
goCPUMetric goMetric
goMemStateMetrics []goMetric
}
func (g golang) Collect()[]MetricInterf {
m := make([]MetricInterf, 0)
m = append(m, &g.goRoutineMetric)
m = append(m, &g.goProcessMetric)
ms := &runtime.MemStats{}
runtime.ReadMemStats(ms)
for idx := range g.goMemStateMetrics {
g.goMemStateMetrics[idx].MemStats = ms
m = append(m, &g.goMemStateMetrics[idx])
}
return m
}
type goMetric struct {
Name string
Help string
MemStats *runtime.MemStats
MemGetFunc func(stat *runtime.MemStats) float64
GetFunc func() float64
}
func (m goMetric)GetMeta() MetricMeta {
return MetricMeta{
Name: m.Name,
Help: m.Help,
}
}
func (m goMetric) GetValue() (*FloatOrString, error) {
if m.MemStats != nil {
return FormFloatOrString(m.MemGetFunc(m.MemStats))
}
return FormFloatOrString(m.GetFunc())
}
func (m goMetric) GetExtension() (*MetricExtension, error) {
return nil, nil
}