comparison mod_measure_process/mod_measure_process.lua @ 4554:025cf93acfe9

mod_measure_process: Provide metrics about the process itself
author Jonas Schäfer <jonas@wielicki.name>
date Wed, 28 Apr 2021 08:21:54 +0200
parents
children adc6241e5d16
comparison
equal deleted inserted replaced
4553:7e5c8186f121 4554:025cf93acfe9
1 module:set_global()
2
3 local get_cpu_time = os.clock
4
5 local custom_metric = require "core.statsmanager".metric
6 local cpu_time = custom_metric(
7 "counter", "process_cpu", "seconds",
8 "CPU time used by Prosody as reported by clock(3)."
9 ):with_labels()
10
11 local lfs = require "lfs"
12
13 module:hook("stats-update", function ()
14 cpu_time:set(get_cpu_time())
15 end);
16
17 if lfs.attributes("/proc/self/statm", "mode") == "file" then
18 local pagesize = module:get_option_number("memory_pagesize", 4096); -- getconf PAGESIZE
19
20 local vsz = custom_metric(
21 "gauge", "process_virtual_memory", "bytes",
22 "Virtual memory size in bytes."
23 ):with_labels()
24 local rss = custom_metric(
25 "gauge", "process_resident_memory", "bytes",
26 "Resident memory size in bytes."
27 ):with_labels()
28
29 module:hook("stats-update", function ()
30 local statm, err = io.open("/proc/self/statm");
31 if not statm then
32 module:log("error", tostring(err));
33 return;
34 end
35 -- virtual memory (caches, opened librarys, everything)
36 vsz:set(statm:read("*n") * pagesize);
37 -- resident set size (actually used memory)
38 rss:set(statm:read("*n") * pagesize);
39 statm:close();
40 end);
41 end
42
43 if lfs.attributes("/proc/self/fd", "mode") == "directory" then
44 local open_fds = custom_metric(
45 "gauge", "process_open_fds", "",
46 "Number of open file descriptors."
47 ):with_labels()
48
49 local has_posix, posix = pcall(require, "util.pposix")
50 local max_fds
51 if has_posix then
52 max_fds = custom_metric(
53 "gauge", "process_max_fds", "",
54 "Maximum number of open file descriptors"
55 ):with_labels()
56 else
57 module:log("warn", "not reporting maximum number of file descriptors because mod_posix is not available")
58 end
59
60 module:hook("stats-update", function ()
61 local count = 0
62 for _ in lfs.dir("/proc/self/fd") do
63 count = count + 1
64 end
65 open_fds:set(count)
66
67 if has_posix then
68 local ok, soft, hard = posix.getrlimit("NOFILE")
69 if ok then
70 max_fds:set(hard)
71 end
72 end
73 end);
74 end