forked from gari/beat-exporter
/
beat.go
182 lines (168 loc) · 4.7 KB
/
beat.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
package collector
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
//CPUTimings json structure
type CPUTimings struct {
Ticks float64 `json:"ticks"`
Time struct {
MS float64 `json:"ms"`
} `json:"time"`
Value float64 `json:"value"`
}
//BeatStats json structure
type BeatStats struct {
CPU struct {
Sytem CPUTimings `json:"system"`
Total CPUTimings `json:"total"`
User CPUTimings `json:"user"`
} `json:"cpu"`
BeatUptime struct {
Uptime struct {
MS float64 `json:"ms"`
} `json:"uptime"`
EmphemeralID string `json:"emphemeral_id"`
} `json:"info"`
Memstats struct {
GCNext float64 `json:"gc_next"`
MemoryAlloc float64 `json:"memory_alloc"`
MemoryTotal float64 `json:"memory_total"`
RSS float64 `json:"rss"`
} `json:"memstats"`
}
type beatCollector struct {
beatInfo *BeatInfo
stats *Stats
metrics exportedMetrics
}
// NewBeatCollector constructor
func NewBeatCollector(beatInfo *BeatInfo, stats *Stats) prometheus.Collector {
return &beatCollector{
beatInfo: beatInfo,
stats: stats,
metrics: exportedMetrics{
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "cpu_time", "miliseconds"),
"beat.cpu.time",
nil, prometheus.Labels{"mode": "system"},
),
eval: func(stats *Stats) float64 { return stats.Beat.CPU.Sytem.Time.MS },
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "cpu_time", "miliseconds"),
"beat.cpu.time",
nil, prometheus.Labels{"mode": "user"},
),
eval: func(stats *Stats) float64 { return stats.Beat.CPU.User.Time.MS },
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "cpu_time", "miliseconds"),
"beat.cpu.time",
nil, prometheus.Labels{"mode": "total"},
),
eval: func(stats *Stats) float64 { return stats.Beat.CPU.Total.Time.MS },
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "cpu", "ticks"),
"beat.cpu.ticks",
nil, prometheus.Labels{"mode": "system"},
),
eval: func(stats *Stats) float64 { return stats.Beat.CPU.Sytem.Ticks },
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "cpu", "ticks"),
"beat.cpu.ticks",
nil, prometheus.Labels{"mode": "user"},
),
eval: func(stats *Stats) float64 { return stats.Beat.CPU.User.Ticks },
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "cpu", "ticks"),
"beat.cpu.ticks",
nil, prometheus.Labels{"mode": "total"},
),
eval: func(stats *Stats) float64 { return stats.Beat.CPU.Total.Ticks },
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "uptime", "seconds"),
"beat.info.uptime.ms",
nil, nil,
),
eval: func(stats *Stats) float64 {
return (time.Duration(stats.Beat.BeatUptime.Uptime.MS) * time.Millisecond).Seconds()
},
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "memstats", "gc_next"),
"beat.memstats.gc_next",
nil, nil,
),
eval: func(stats *Stats) float64 {
return stats.Beat.Memstats.GCNext
},
valType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "memstats", "memory_alloc"),
"beat.memstats.memory_alloc",
nil, nil,
),
eval: func(stats *Stats) float64 {
return stats.Beat.Memstats.MemoryAlloc
},
valType: prometheus.GaugeValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "memstats", "memory_total"),
"beat.memstats.memory_total",
nil, nil,
),
eval: func(stats *Stats) float64 {
return stats.Beat.Memstats.MemoryTotal
},
valType: prometheus.GaugeValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(beatInfo.Beat, "memstats", "rss"),
"beat.memstats.rss",
nil, nil,
),
eval: func(stats *Stats) float64 {
return stats.Beat.Memstats.RSS
},
valType: prometheus.GaugeValue,
},
},
}
}
// Describe returns all descriptions of the collector.
func (c *beatCollector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.metrics {
ch <- metric.desc
}
}
// Collect returns the current state of all metrics of the collector.
func (c *beatCollector) Collect(ch chan<- prometheus.Metric) {
for _, i := range c.metrics {
ch <- prometheus.MustNewConstMetric(i.desc, i.valType, i.eval(c.stats))
}
}