forked from stuartnelson3/passenger_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
384 lines (341 loc) · 11.4 KB
/
main.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"bytes"
"encoding/xml"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"os/exec"
"strconv"
"strings"
"time"
"golang.org/x/net/html/charset"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
const (
namespace = "passenger_nginx"
nanosecondsPerSecond = 1000000000
)
var (
timeoutErr = errors.New("passenger-status command timed out")
processIdentifiers = make(map[string]int)
)
// Exporter collects metrics from a passenger-nginx integration.
type Exporter struct {
// binary file path for querying passenger state.
cmd string
args []string
// Passenger command timeout.
timeout time.Duration
// Passenger metrics.
up *prometheus.Desc
version *prometheus.Desc
toplevelQueue *prometheus.Desc
maxProcessCount *prometheus.Desc
currentProcessCount *prometheus.Desc
appCount *prometheus.Desc
// App metrics.
appQueue *prometheus.Desc
appGroupQueue *prometheus.Desc
appProcsSpawning *prometheus.Desc
appCapacityUsed *prometheus.Desc
// Process metrics.
requestsProcessed *prometheus.Desc
sessions *prometheus.Desc
procStartTime *prometheus.Desc
procMemory *prometheus.Desc
procDetached *prometheus.Desc
}
func NewExporter(cmd string, timeout time.Duration) *Exporter {
cmdComponents := strings.Split(cmd, " ")
return &Exporter{
cmd: cmdComponents[0],
args: cmdComponents[1:],
timeout: timeout,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Could passenger status be queried.",
nil,
nil,
),
version: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "version"),
"Version of passenger",
[]string{"version"},
nil,
),
toplevelQueue: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "top_level_queue"),
"Number of requests in the top-level queue.",
nil,
nil,
),
maxProcessCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_processes"),
"Configured maximum number of processes.",
nil,
nil,
),
currentProcessCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "current_processes"),
"Current number of processes.",
nil,
nil,
),
appCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "app_count"),
"Number of apps.",
nil,
nil,
),
appQueue: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "app_queue"),
"Number of requests in app process queues.",
[]string{"name"},
nil,
),
appGroupQueue: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "app_group_queue"),
"Number of requests in app group process queues.",
[]string{"group", "default"},
nil,
),
appCapacityUsed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "app_capacity_used"),
"Number of processes from overall capacity.",
[]string{"name"},
nil,
),
appProcsSpawning: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "app_procs_spawning"),
"Number of processes spawning.",
[]string{"name"},
nil,
),
requestsProcessed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "requests_processed_total"),
"Number of processes served by a process.",
[]string{"name", "id"},
nil,
),
sessions: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "current_sessions"),
"Number of sessions currently being handled by a process.",
[]string{"name", "id"},
nil,
),
procStartTime: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "proc_start_time_seconds"),
"Number of seconds since processor started.",
[]string{"name", "id", "codeRevision"},
nil,
),
procMemory: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "proc_memory"),
"Memory consumed by a process",
[]string{"name", "id"},
nil,
),
procDetached: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "proc_detached"),
"Number of detached processes",
[]string{"name"},
nil,
),
}
}
// Collect fetches the statistics from the configured passenger frontend, and
// delivers them as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
info, err := e.status()
if err != nil {
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0)
log.Errorf("failed to collect status from passenger: %s", err)
return
}
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1)
ch <- prometheus.MustNewConstMetric(e.version, prometheus.GaugeValue, 1, info.PassengerVersion)
ch <- prometheus.MustNewConstMetric(e.toplevelQueue, prometheus.GaugeValue, parseFloat(info.TopLevelRequestsInQueue))
ch <- prometheus.MustNewConstMetric(e.maxProcessCount, prometheus.GaugeValue, parseFloat(info.MaxProcessCount))
ch <- prometheus.MustNewConstMetric(e.currentProcessCount, prometheus.GaugeValue, parseFloat(info.CurrentProcessCount))
ch <- prometheus.MustNewConstMetric(e.appCount, prometheus.GaugeValue, parseFloat(info.AppCount))
for _, sg := range info.SuperGroups {
ch <- prometheus.MustNewConstMetric(e.appQueue, prometheus.GaugeValue, parseFloat(sg.RequestsInQueue), sg.Name)
ch <- prometheus.MustNewConstMetric(e.appProcsSpawning, prometheus.GaugeValue, parseFloat(sg.Group.ProcessesSpawning), sg.Name)
ch <- prometheus.MustNewConstMetric(e.appCapacityUsed, prometheus.GaugeValue, parseFloat(sg.CapacityUsed), sg.Name)
ch <- prometheus.MustNewConstMetric(e.appGroupQueue, prometheus.GaugeValue, parseFloat(sg.Group.GetWaitListSize), sg.Group.Name, sg.Group.Default)
detachedProcesses := 0
// Update process identifiers map.
processIdentifiers = updateProcesses(processIdentifiers, sg.Group.Processes)
for _, proc := range sg.Group.Processes {
if bucketID, ok := processIdentifiers[proc.PID]; ok {
ch <- prometheus.MustNewConstMetric(e.procMemory, prometheus.GaugeValue, parseFloat(proc.RealMemory), sg.Name, strconv.Itoa(bucketID))
ch <- prometheus.MustNewConstMetric(e.requestsProcessed, prometheus.CounterValue, parseFloat(proc.RequestsProcessed), sg.Name, strconv.Itoa(bucketID))
ch <- prometheus.MustNewConstMetric(e.sessions, prometheus.GaugeValue, parseFloat(proc.Sessions), sg.Name, strconv.Itoa(bucketID))
if proc.LifeStatus == "ALIVE" && proc.Enabled == "DETACHED" {
detachedProcesses += 1
}
if startTime, err := strconv.Atoi(proc.SpawnStartTime); err == nil {
ch <- prometheus.MustNewConstMetric(e.procStartTime, prometheus.GaugeValue, float64(startTime/nanosecondsPerSecond),
sg.Name, strconv.Itoa(bucketID), proc.CodeRevision,
)
}
}
}
ch <- prometheus.MustNewConstMetric(e.procDetached, prometheus.GaugeValue, float64(detachedProcesses), sg.Name)
}
}
func (e *Exporter) status() (*Info, error) {
var (
out bytes.Buffer
cmd = exec.Command(e.cmd, e.args...)
)
cmd.Stdout = &out
err := cmd.Start()
if err != nil {
return nil, err
}
errc := make(chan error, 1)
go func(cmd *exec.Cmd, c chan<- error) {
c <- cmd.Wait()
}(cmd, errc)
select {
case err := <-errc:
if err != nil {
return nil, err
}
case <-time.After(e.timeout):
return nil, timeoutErr
}
return parseOutput(&out)
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.up
ch <- e.version
ch <- e.toplevelQueue
ch <- e.maxProcessCount
ch <- e.currentProcessCount
ch <- e.appCount
ch <- e.appQueue
ch <- e.appGroupQueue
ch <- e.appCapacityUsed
ch <- e.appProcsSpawning
ch <- e.requestsProcessed
ch <- e.sessions
ch <- e.procStartTime
ch <- e.procMemory
ch <- e.procDetached
}
func parseOutput(r io.Reader) (*Info, error) {
var info Info
decoder := xml.NewDecoder(r)
decoder.CharsetReader = charset.NewReaderLabel
err := decoder.Decode(&info)
if err != nil {
return nil, err
}
return &info, nil
}
func parseFloat(val string) float64 {
v, err := strconv.ParseFloat(val, 64)
if err != nil {
log.Errorf("failed to parse %s: %v", val, err)
v = math.NaN()
}
return v
}
// updateProcesses updates the global map from process id:exporter id. Process
// TTLs cause new processes to be created on a user-defined cycle. When a new
// process replaces an old process, the new process's statistics will be
// bucketed with those of the process it replaced.
// Processes are restarted at an offset, user-defined interval. The
// restarted process is appended to the end of the status output. For
// maintaining consistent process identifiers between process starts,
// pids are mapped to an identifier based on process count. When a new
// process/pid appears, it is mapped to either the first empty place
// within the global map storing process identifiers, or mapped to
// pid:id pair in the map.
func updateProcesses(old map[string]int, processes []Process) map[string]int {
var (
updated = make(map[string]int)
found = make([]string, len(old))
missing []string
)
for _, p := range processes {
if id, ok := old[p.PID]; ok {
found[id] = p.PID
// id also serves as an index.
// By putting the pid at a certain index, we can loop
// through the array to find the values that are the 0
// value (empty string).
// If index i has the empty value, then it was never
// updated, so we slot the first of the missingPIDs
// into that position. Passenger-status orders output
// by pid, increasing. We can then assume that
// unclaimed pid positions map in order to the missing
// pids.
} else {
missing = append(missing, p.PID)
}
}
j := 0
for i, pid := range found {
if pid == "" {
if j >= len(missing) {
continue
}
pid = missing[j]
j++
}
updated[pid] = i
}
// If the number of elements in missing iterated through is less
// than len(missing), there are new elements to be added to the map.
// Unused pids from the last collection are not copied from old to
// updated, thereby cleaning the return value of unused PIDs.
if j < len(missing) {
count := len(found)
for i, pid := range missing[j:] {
updated[pid] = count + i
}
}
return updated
}
func main() {
var (
cmd = flag.String("passenger.command", "passenger-status --show=xml", "Passenger command for querying passenger status.")
timeout = flag.Duration("passenger.command.timeout", 500*time.Millisecond, "Timeout for passenger.command.")
pidFile = flag.String("passenger.pid-file", "", "Optional path to a file containing the passenger/nginx PID for additional metrics.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
listenAddress = flag.String("web.listen-address", ":9149", "Address to listen on for web interface and telemetry.")
)
flag.Parse()
if *pidFile != "" {
prometheus.MustRegister(prometheus.NewProcessCollectorPIDFn(
func() (int, error) {
content, err := ioutil.ReadFile(*pidFile)
if err != nil {
return 0, fmt.Errorf("error reading pidfile %q: %s", *pidFile, err)
}
value, err := strconv.Atoi(strings.TrimSpace(string(content)))
if err != nil {
return 0, fmt.Errorf("error parsing pidfile %q: %s", *pidFile, err)
}
return value, nil
},
namespace),
)
}
prometheus.MustRegister(NewExporter(*cmd, *timeout))
http.Handle(*metricsPath, prometheus.Handler())
log.Infoln("starting passenger_exporter_nginx", version.Info())
log.Infoln("build context", version.BuildContext())
log.Infoln("listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}