util/openmetrics.lua
changeset 11604 a02c277eb97a
parent 11597 0db763f3f3be
child 11933 85d51bfcf56b
equal deleted inserted replaced
11603:ed405b6357a8 11604:a02c277eb97a
    23 local select = select;
    23 local select = select;
    24 local array = require "util.array";
    24 local array = require "util.array";
    25 local log = require "util.logger".init("util.openmetrics");
    25 local log = require "util.logger".init("util.openmetrics");
    26 local new_multitable = require "util.multitable".new;
    26 local new_multitable = require "util.multitable".new;
    27 local iter_multitable = require "util.multitable".iter;
    27 local iter_multitable = require "util.multitable".iter;
       
    28 local t_pack, t_unpack = require "util.table".pack, table.unpack or unpack; --luacheck: ignore 113/unpack
    28 
    29 
    29 -- BEGIN of Utility: "metric proxy"
    30 -- BEGIN of Utility: "metric proxy"
    30 -- This allows to wrap a MetricFamily in a proxy which only provides the
    31 -- This allows to wrap a MetricFamily in a proxy which only provides the
    31 -- `with_labels` and `with_partial_label` methods. This allows to pre-set one
    32 -- `with_labels` and `with_partial_label` methods. This allows to pre-set one
    32 -- or more labels on a metric family. This is used in particular via
    33 -- or more labels on a metric family. This is used in particular via
   127 	if count == 0 then
   128 	if count == 0 then
   128 		return self.data
   129 		return self.data
   129 	end
   130 	end
   130 	local metric = self.data:get(...)
   131 	local metric = self.data:get(...)
   131 	if not metric then
   132 	if not metric then
   132 		local values = table.pack(...)
   133 		local values = t_pack(...)
   133 		metric = self:new_metric(values)
   134 		metric = self:new_metric(values)
   134 		values[values.n+1] = metric
   135 		values[values.n+1] = metric
   135 		self.data:set(table.unpack(values, 1, values.n+1))
   136 		self.data:set(t_unpack(values, 1, values.n+1))
   136 	end
   137 	end
   137 	return metric
   138 	return metric
   138 end
   139 end
   139 
   140 
   140 function metric_family_mt:with_partial_label(label)
   141 function metric_family_mt:with_partial_label(label)
   157 	local searchkeys = {};
   158 	local searchkeys = {};
   158 	local nlabels = #self.label_keys
   159 	local nlabels = #self.label_keys
   159 	for i=1,nlabels do
   160 	for i=1,nlabels do
   160 		searchkeys[i] = nil;
   161 		searchkeys[i] = nil;
   161 	end
   162 	end
   162 	local it, state = iter_multitable(self.data, table.unpack(searchkeys, 1, nlabels))
   163 	local it, state = iter_multitable(self.data, t_unpack(searchkeys, 1, nlabels))
   163 	return function(_s)
   164 	return function(_s)
   164 		local label_values = table.pack(it(_s))
   165 		local label_values = t_pack(it(_s))
   165 		if label_values.n == 0 then
   166 		if label_values.n == 0 then
   166 			return nil, nil
   167 			return nil, nil
   167 		end
   168 		end
   168 		local metric = label_values[label_values.n]
   169 		local metric = label_values[label_values.n]
   169 		label_values[label_values.n] = nil
   170 		label_values[label_values.n] = nil