Skip to content

Commit

Permalink
add request/response size
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Nov 28, 2023
1 parent 0c62bab commit de256a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 14 additions & 1 deletion bridge_rx.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local http = ...

local metric_commands, metric_requests
local metric_commands, metric_requests, metric_time, metric_size
if minetest.get_modpath("monitoring") then
metric_commands = monitoring.counter("mtui_rx_commands", "number of received commands")
metric_requests = monitoring.counter("mtui_rx_requests", "number of rx requests")
metric_time = monitoring.counter("mtui_rx_request_time", "time usage in microseconds for rx requests")
metric_size = monitoring.counter("mtui_rx_response_size", "rx response size in bytes")
end

local command_handlers = {}
Expand All @@ -18,6 +20,8 @@ local function fetch_commands()
metric_requests.inc()
end

local t0 = minetest.get_us_time()

http.fetch({
url = mtui.url .. "/api/bridge",
extra_headers = {
Expand All @@ -26,7 +30,16 @@ local function fetch_commands()
timeout = 30,
method = "GET"
}, function(res)
if metric_time then
local t1 = minetest.get_us_time()
metric_time.inc(t1 - t0)
end

if res.succeeded and res.code == 200 and res.data ~= "" then
if metric_size then
metric_size.inc(#res.data)
end

local command_list = minetest.parse_json(res.data)

for _, cmd in ipairs(command_list) do
Expand Down
18 changes: 16 additions & 2 deletions bridge_tx.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local http = ...

local metric_commands, metric_requests
local metric_commands, metric_requests, metric_time, metric_size
if minetest.get_modpath("monitoring") then
metric_commands = monitoring.counter("mtui_tx_commands", "number of sent commands")
metric_requests = monitoring.counter("mtui_tx_requests", "number of tx requests")
metric_time = monitoring.counter("mtui_tx_request_time", "time usage in microseconds for tx requests")
metric_size = monitoring.counter("mtui_tx_request_size", "tx request size in bytes")
end

-- list of commands to send
Expand All @@ -17,15 +19,27 @@ local function send_commands()
metric_requests.inc()
end

local t0 = minetest.get_us_time()

local data = minetest.write_json(commands)
if metric_size then
metric_size.inc(#data)
end

http.fetch({
url = mtui.url .. "/api/bridge",
extra_headers = {
"Api-Key: " .. mtui.key
},
timeout = 10,
method = "POST",
data = minetest.write_json(commands)
data = data
}, function(res)
if metric_time then
local t1 = minetest.get_us_time()
metric_time.inc(t1 - t0)
end

if not res.succeeded or res.code ~= 200 then
minetest.log("error", "[mtui] failed to send command, " ..
"status: " .. res.code .. " response: " .. (res.data or "<none>"))
Expand Down

0 comments on commit de256a5

Please sign in to comment.