Skip to content

Commit

Permalink
add metrics of prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Jul 2, 2023
1 parent dd8d958 commit ad035f3
Show file tree
Hide file tree
Showing 11 changed files with 537 additions and 19 deletions.
128 changes: 111 additions & 17 deletions lualib-src/lualib-metrics.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <dirent.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
Expand All @@ -28,6 +31,106 @@ lmemallocator(lua_State *L)
return 1;
}

static int
lcpu(lua_State *L)
{
struct rusage ru;
float stime,utime;
getrusage(RUSAGE_SELF, &ru);
stime = (float)ru.ru_stime.tv_sec;
stime += (float)ru.ru_stime.tv_usec / 1000000;
utime = (float)ru.ru_utime.tv_sec;
utime += (float)ru.ru_utime.tv_usec / 1000000;
lua_pushnumber(L, stime);
lua_pushnumber(L, utime);
return 2;
}

static int
lmaxfds(lua_State *L)
{
struct rlimit rlim;
int ret = getrlimit(RLIMIT_NOFILE, &rlim);
if (ret != 0) {
silly_log_error("[metrics] getrlimit errno:%d", errno);
rlim.rlim_cur = 0;
rlim.rlim_max = 0;
}
lua_pushinteger(L, rlim.rlim_cur); //soft
lua_pushinteger(L, rlim.rlim_max); //hard
return 2;
}

static int
lopenfds(lua_State *L)
{
int fd_count = 0;
struct dirent *entry;
DIR *fd_dir = opendir("/proc/self/fd");
if (fd_dir == NULL) {
silly_log_error("[metrics] failed to open /proc/self/fd");
lua_pushinteger(L, 0);
return 1;
}
while ((entry = readdir(fd_dir)) != NULL) {
if (entry->d_name[0] != '.') {
fd_count++;
}
}
closedir(fd_dir);
lua_pushinteger(L, fd_count);
return 1;
}

static void
parse_memstat(char *p, char *e, int page, size_t vmstat[2])
{
//VmSize is the 23th field in /proc/self/stat
//RSS is the 24th field in /proc/self/stat
int i = 0;
char *end;
while (p < e) {
if (*p++ != ' ')
continue;
switch (++i) {
case 22: //vmsize
end = strchr(p, ' ');
vmstat[0] = strtoll(p, &end, 10);
break;
case 23: //rss
end = strchr(p, ' ');
vmstat[1] = strtoll(p, &end, 10) * page;
goto finish;
break;
}
}
finish:
return ;
}

static int
lmemstat(lua_State *L)
{
int fd, count;
char buf[4096];
int page = sysconf(_SC_PAGESIZE);
size_t vmstat[2] = {-1, -1}; //vmsize, vmrss;
fd = open("/proc/self/stat", O_RDONLY);
if (fd >= 0) {
count = read(fd, buf, sizeof(buf) - 1);
close(fd);
} else {
count = 0;
}
if (count > 0) {
parse_memstat(buf, &buf[count], page, vmstat);
}
lua_pushinteger(L, vmstat[0]);
lua_pushinteger(L, vmstat[1]);
lua_pushinteger(L, silly_memused());
return 3;
}

static int
lmemallocatorinfo(lua_State *L)
{
Expand All @@ -38,6 +141,7 @@ lmemallocatorinfo(lua_State *L)
lua_pushinteger(L, resident);
return 3;
}

static int
lmemused(lua_State *L)
{
Expand Down Expand Up @@ -65,21 +169,6 @@ lmsgsize(lua_State *L)
return 1;
}

static int
lcpuinfo(lua_State *L)
{
struct rusage ru;
float stime,utime;
getrusage(RUSAGE_SELF, &ru);
stime = (float)ru.ru_stime.tv_sec;
stime += (float)ru.ru_stime.tv_usec / 1000000;
utime = (float)ru.ru_utime.tv_sec;
utime += (float)ru.ru_utime.tv_usec / 1000000;
lua_pushnumber(L, stime);
lua_pushnumber(L, utime);
return 2;
}

static int
lpollapi(lua_State *L)
{
Expand Down Expand Up @@ -154,15 +243,20 @@ ltimerresolution(lua_State *L)
}

int
luaopen_sys_metrics(lua_State *L)
luaopen_sys_metrics_c(lua_State *L)
{
luaL_Reg tbl[] = {
//process
{"cpu", lcpu},
{"maxfds", lmaxfds},
{"openfds", lopenfds},
{"memstat", lmemstat},

{"memused", lmemused},
{"memrss", lmemrss},
{"memallocator", lmemallocator},
{"memallocatorinfo", lmemallocatorinfo},
{"msgsize", lmsgsize},
{"cpuinfo", lcpuinfo},
{"pollapi", lpollapi},
{"netinfo", lnetinfo},
{"timerinfo", ltimerinfo},
Expand Down
4 changes: 2 additions & 2 deletions lualib/sys/console.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local core = require "sys.core"
local time = require "sys.time"
local metrics = require "sys.metrics"
local metrics = require "sys.metrics.c"
local logger = require "sys.logger"
local patch = require "sys.patch"
local tcp = require "sys.net.tcp"
Expand Down Expand Up @@ -73,7 +73,7 @@ function console.timerinfo()
end

function console.cpuinfo()
local sys, usr = metrics.cpuinfo()
local sys, usr = metrics.cpu()
return format("#CPU\r\ncpu_sys: %.2fs\r\ncpu_user: %.2fs", sys, usr)
end

Expand Down
16 changes: 16 additions & 0 deletions lualib/sys/metrics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local M = {}


function M.counter(name, help, labels)

end

function M.gauge(name, help, labels)

end

function M.summary(name, help, labels)

end

function
39 changes: 39 additions & 0 deletions lualib/sys/metrics/2
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local M = {}
local pairs = table.pairs
local remove = table.remove
local setmetatable = setmetatable

local mt = {__index = M}


function M:new()
return setmetatable({metrics = {}}, mt)
end

function M:register(obj)
for i = 1, #self do
if self[i] == obj then
return
end
end
self[#self + 1] = obj
end

function M:unregister(obj)
for i = 1, #self do
if self[i] == obj then
remove(self, i)
return
end
end
end

function M:collect()
local len = 0
local metrics = self.metrics
for i = 1, #list do
len = list[i]:collect(metrics, len)


return M

33 changes: 33 additions & 0 deletions lualib/sys/metrics/counter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local labels = require "sys.metrics.labels"
local helper = require "sys.metrics.helper"

local mt
local label_mt

mt = {
__index = nil,
new = nil,
collect = helper.collect,
add = helper.add,
inc = helper.inc,
}

label_mt = {
__index = nil,
new = nil,
collect = helper.collect,
labels = labels({__index = {
add = helper.add,
inc = helper.inc,
}})
}

local new = helper.new("counter", {__index = mt}, {__index = label_mt})

mt.new = new
label_mt.new = new

mt.__index = mt
label_mt.__index = label_mt

return new
39 changes: 39 additions & 0 deletions lualib/sys/metrics/gauge.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local labels = require "sys.metrics.labels"
local helper = require "sys.metrics.helper"

local mt
local label_mt

mt = {
__index = nil,
new = nil,
collect = helper.collect,
add = helper.add,
inc = helper.inc,
sub = helper.sub,
dec = helper.dec,
set = helper.set,
}

label_mt = {
__index = nil,
new = nil,
collect = helper.collect,
labels = labels({__index = {
add = helper.add,
inc = helper.inc,
sub = helper.sub,
dec = helper.dec,
set = helper.set,
}})
}

local new = helper.new("gauge", {__index = mt}, {__index = label_mt})

mt.new = new
label_mt.new = new

mt.__index = mt
label_mt.__index = label_mt

return new
58 changes: 58 additions & 0 deletions lualib/sys/metrics/helper.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local M = {}

local assert = assert
local setmetatable = setmetatable

function M:set(v)
self[1] = v
end

function M:add(v)
assert(v >= 0)
self[1] = self[1] + v
end

function M:inc()
self[1] = self[1] + 1
end

function M:sub(v)
self[1] = self[1] - v
end

function M:dec()
self[1] = self[1] - 1
end

function M.new(kind, mt, label_mt)
assert(kind, "kind")
assert(mt, "mt")
assert(label_mt, "label_mt")
return function(name, help, labels)
local obj
if labels then
obj = setmetatable({
name = name,
help = help,
kind = kind,
labelnames = labels,
}, label_mt)
else
obj = setmetatable({
name = name,
help = help,
kind = kind,
[1] = 0, --the count value
}, mt)
end
return obj
end
end

function M.collect(self, buf, len)
len = len + 1
buf[len] = self
return len
end

return M
Loading

0 comments on commit ad035f3

Please sign in to comment.