mod_measure_storage/mod_measure_storage.lua
changeset 2301 992e40dab31d
child 2690 39cb2a51e779
equal deleted inserted replaced
2300:8c0bf3151e37 2301:992e40dab31d
       
     1 module:set_global()
       
     2 
       
     3 local function return_args_after_calling(f, ...)
       
     4 	f();
       
     5 	return ...
       
     6 end
       
     7 
       
     8 local function time_method(module, store_name, store_type, method_name, method_function)
       
     9 	local opt_use_tags = module:get_option_boolean("measure_storage_tagged_metric", false);
       
    10 
       
    11 	local metric_name, metric_tags;
       
    12 	if opt_use_tags then
       
    13 		metric_name, metric_tags = "storage_operation", ("store_name:%s,store_type:%s,store_operation:%s"):format(store_name, store_type, method_name);
       
    14 	else
       
    15 		metric_name = store_name.."_"..store_type.."_"..method_name;
       
    16 	end
       
    17 	local measure_operation_started = module:measure(metric_name, metric_tags);
       
    18 
       
    19 	return function (...)
       
    20 		module:log("debug", "Measuring storage operation %s (%s)", metric_name, metric_tags or "no tags");
       
    21 		local measure_operation_complete = measure_operation_started();
       
    22 		return return_args_after_calling(measure_operation_complete, method_function(...));
       
    23 	end;
       
    24 end
       
    25 
       
    26 local function wrap_store(module, store_name, store_type, store)
       
    27 	local new_store = setmetatable({}, {
       
    28 		__index = function (t, method_name)
       
    29 			local original_method = store[method_name];
       
    30 			if type(original_method) ~= "function" then
       
    31 				if original_method then
       
    32 					rawset(t, method_name, original_method);
       
    33 				end
       
    34 				return original_method;
       
    35 			end
       
    36 			local timed_method = time_method(module, store_name, store_type, method_name, original_method);
       
    37 			rawset(t, method_name, timed_method);
       
    38 			return timed_method;
       
    39 		end;
       
    40 	});
       
    41 	return new_store;
       
    42 end
       
    43 
       
    44 local function hook_event(module)
       
    45 	module:hook("store-opened", function(event)
       
    46 		event.store = wrap_store(module, event.store_name, event.store_type, event.store);
       
    47 	end);
       
    48 end
       
    49 
       
    50 function module.load()
       
    51 	hook_event(module);
       
    52 end
       
    53 
       
    54 function module.add_host(module)
       
    55 	hook_event(module);
       
    56 end