forked from Intellection/passenger-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassenger_exporter.go
472 lines (431 loc) · 15.6 KB
/
passenger_exporter.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package main
import (
"bytes"
"encoding/xml"
"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"
)
// Info represents the info section of passenger's status.
type Info struct {
PassengerVersion string `xml:"passenger_version"`
AppCount string `xml:"group_count"`
CurrentProcessCount string `xml:"process_count"`
MaxProcessCount string `xml:"max"`
CapacityUsed string `xml:"capacity_used"`
TopLevelRequestQueueSize string `xml:"get_wait_list_size"`
SuperGroups []SuperGroup `xml:"supergroups>supergroup"`
}
// SuperGroup represents the super group section of passenger's status.
type SuperGroup struct {
Name string `xml:"name"`
State string `xml:"state"`
RequestQueueSize string `xml:"get_wait_list_size"`
CapacityUsed string `xml:"capacity_used"`
Group Group `xml:"group"`
}
// Group represents the group section of passenger's status.
type Group struct {
Name string `xml:"name"`
ComponentName string `xml:"component_name"`
AppRoot string `xml:"app_root"`
AppType string `xml:"app_type"`
Environment string `xml:"environment"`
UUID string `xml:"uuid"`
EnabledProcessCount string `xml:"enabled_process_count"`
DisablingProcessCount string `xml:"disabling_process_count"`
DisabledProcessCount string `xml:"disabled_process_count"`
CapacityUsed string `xml:"capacity_used"`
RequestQueueSize string `xml:"get_wait_list_size"`
DisableWaitListSize string `xml:"disable_wait_list_size"`
ProcessesSpawning string `xml:"processes_being_spawned"`
LifeStatus string `xml:"life_status"`
User string `xml:"user"`
UID string `xml:"uid"`
Group string `xml:"group"`
GID string `xml:"gid"`
Default string `xml:"default,attr"`
Options Options `xml:"options"`
Processes []Process `xml:"processes>process"`
}
// Process represents the process section of passenger's status.
type Process struct {
PID string `xml:"pid"`
StickySessionID string `xml:"sticky_session_id"`
GUPID string `xml:"gupid"`
Concurrency string `xml:"concurrency"`
Sessions string `xml:"sessions"`
Busyness string `xml:"busyness"`
RequestsProcessed string `xml:"processed"`
SpawnerCreationTime string `xml:"spawner_creation_time"`
SpawnStartTime string `xml:"spawn_start_time"`
SpawnEndTime string `xml:"spawn_end_time"`
LastUsed string `xml:"last_used"`
LastUsedDesc string `xml:"last_used_desc"`
Uptime string `xml:"uptime"`
LifeStatus string `xml:"life_status"`
Enabled string `xml:"enabled"`
HasMetrics string `xml:"has_metrics"`
CPU string `xml:"cpu"`
RSS string `xml:"rss"`
PSS string `xml:"pss"`
PrivateDirty string `xml:"private_dirty"`
Swap string `xml:"swap"`
RealMemory string `xml:"real_memory"`
VMSize string `xml:"vmsize"`
ProcessGroupID string `xml:"process_group_id"`
Command string `xml:"command"`
}
// Options represents the options section of passenger's status.
type Options struct {
AppRoot string `xml:"app_root"`
AppGroupName string `xml:"app_group_name"`
AppType string `xml:"app_type"`
StartCommand string `xml:"start_command"`
StartupFile string `xml:"startup_file"`
ProcessTitle string `xml:"process_title"`
LogLevel string `xml:"log_level"`
StartTimeout string `xml:"start_timeout"`
Environment string `xml:"environment"`
BaseURI string `xml:"base_uri"`
SpawnMethod string `xml:"spawn_method"`
DefaultUser string `xml:"default_user"`
DefaultGroup string `xml:"default_group"`
IntegrationMode string `xml:"integration_mode"`
RubyBinPath string `xml:"ruby"`
PythonBinPath string `xml:"python"`
NodeJSBinPath string `xml:"nodejs"`
USTRouterAddress string `xml:"ust_router_address"`
USTRouterUsername string `xml:"ust_router_username"`
USTRouterPassword string `xml:"ust_router_password"`
Debugger string `xml:"debugger"`
Analytics string `xml:"analytics"`
APIKey string `xml:"api_key"`
MinProcesses string `xml:"min_processes"`
MaxProcesses string `xml:"max_processes"`
MaxPreloaderIdleTime string `xml:"max_preloader_idle_time"`
MaxOutOfBandWorkInstances string `xml:"max_out_of_band_work_instances"`
}
const (
namespace = "passenger"
nanosecondsPerSecond = 1000000000
)
var (
processIdentifiers = make(map[string]int)
)
// Exporter collects metrics from passenger.
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
topLevelRequestQueue *prometheus.Desc
maxProcessCount *prometheus.Desc
currentProcessCount *prometheus.Desc
appCount *prometheus.Desc
// App metrics.
appRequestQueue *prometheus.Desc
appProcsSpawning *prometheus.Desc
// Process metrics.
requestsProcessed *prometheus.Desc
procSessionCount *prometheus.Desc
procStartTime *prometheus.Desc
procMemory *prometheus.Desc
}
// NewExporter returns an initialized exporter.
func NewExporter(cmd string, timeout float64) *Exporter {
cmdComponents := strings.Split(cmd, " ")
return &Exporter{
cmd: cmdComponents[0],
args: cmdComponents[1:],
timeout: time.Duration(timeout * nanosecondsPerSecond),
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Current health of passenger.",
nil,
nil,
),
version: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "version"),
"Version of passenger.",
[]string{"version"},
nil,
),
topLevelRequestQueue: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "top_level_request_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,
),
appRequestQueue: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "app_request_queue"),
"Number of requests in the app queue.",
[]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 requests served by a process.",
[]string{"name", "id"},
nil,
),
procSessionCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "proc_current_sessions"),
"Current number of sessions server by process.",
[]string{"name", "id"},
nil,
),
procStartTime: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "proc_start_time_seconds"),
"Number of seconds since process started.",
[]string{"name", "id"},
nil,
),
procMemory: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "proc_memory"),
"Memory consumed by a process",
[]string{"name", "id"},
nil,
),
}
}
// Describe describes all the metrics exported by the passenger exporter.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.up
ch <- e.version
ch <- e.topLevelRequestQueue
ch <- e.maxProcessCount
ch <- e.currentProcessCount
ch <- e.appCount
ch <- e.appRequestQueue
ch <- e.appProcsSpawning
ch <- e.requestsProcessed
ch <- e.procSessionCount
ch <- e.procStartTime
ch <- e.procMemory
}
// Collect fetches the statistics from passenger, and delivers them as
// Prometheus metrics.
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.topLevelRequestQueue, prometheus.GaugeValue, parseFloat(info.TopLevelRequestQueueSize))
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.appRequestQueue, prometheus.GaugeValue, parseFloat(sg.Group.RequestQueueSize), sg.Name)
ch <- prometheus.MustNewConstMetric(e.appProcsSpawning, prometheus.GaugeValue, parseFloat(sg.Group.ProcessesSpawning), sg.Name)
// Update process identifiers map.
processIdentifiers = updateProcesses(processIdentifiers, sg.Group.Processes, parseInt(info.MaxProcessCount))
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.procSessionCount, prometheus.GaugeValue, parseFloat(proc.Sessions), sg.Name, strconv.Itoa(bucketID))
ch <- prometheus.MustNewConstMetric(e.requestsProcessed, prometheus.CounterValue, parseFloat(proc.RequestsProcessed), sg.Name, strconv.Itoa(bucketID))
if startTime, err := strconv.Atoi(proc.SpawnStartTime); err == nil {
ch <- prometheus.MustNewConstMetric(e.procStartTime, prometheus.GaugeValue, float64(startTime/nanosecondsPerSecond),
sg.Name, strconv.Itoa(bucketID),
)
}
}
}
}
}
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
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(e.timeout):
if err := cmd.Process.Kill(); err != nil {
log.Errorf("failed to kill process: %s", err)
}
err = fmt.Errorf("status command timed out after %f seconds", e.timeout.Seconds())
return nil, err
case err := <-done:
if err != nil {
return nil, err
}
}
return parseOutput(&out)
}
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
}
func parseInt(val string) int {
v, err := strconv.Atoi(val)
if err != nil {
log.Errorf("failed to parse %s: %v", val, err)
v = 0
}
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, maxProcesses int) map[string]int {
var (
updated = make(map[string]int)
found = make([]string, maxProcesses)
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.Float64("passenger.command.timeout-seconds", 0.5, "Timeout in seconds for passenger.command.")
pidFile = flag.String("passenger.pid-file", "", "Optional path to a file containing the passenger 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())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Passenger Exporter</title></head>
<body>
<h1>Passenger Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Starting passenger-exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}