Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
406 changes: 406 additions & 0 deletions metrics/annotations.lua

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions metrics/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ local function invoke_callbacks()
return registry:invoke_callbacks()
end

---@private
---@param collector metrics.collector
---@param result metrics.observation[]
local function get_collector_values(collector, result)
Expand Down Expand Up @@ -84,6 +85,7 @@ local function collect(opts)
return result
end

---@private
--- Clear all collectors and callbacks from the registry.
---@return any
local function clear()
Expand Down Expand Up @@ -178,6 +180,7 @@ local function set_global_labels(label_pairs)
registry:set_labels(label_pairs)
end

---@private
--- Set a runtime filter for collectors and callbacks by metric selectors.
---@param include string|table?
---@param exclude string|table?
Expand All @@ -188,12 +191,14 @@ local function set_filter(include, exclude)
registry:set_filter(include, exclude)
end

---@private
---@class metrics.namespace
---@field selector string

local Namespace = {}
Namespace.__index = Namespace

---@private
---@param self table
---@param name string
---@param metainfo metrics.metainfo?
Expand All @@ -205,6 +210,7 @@ local function namespace_metainfo(self, name, metainfo)
return res
end

---@private
--- Register a new counter in the namespace.
---@param name string
---@param help string?
Expand All @@ -216,6 +222,7 @@ function Namespace:counter(name, help, metainfo, label_keys)
label_keys)
end

---@private
--- Register a new gauge in the namespace.
---@param name string
---@param help string?
Expand All @@ -227,6 +234,7 @@ function Namespace:gauge(name, help, metainfo, label_keys)
label_keys)
end

---@private
--- Register a new histogram in the namespace.
---@param name string
---@param help string?
Expand All @@ -238,6 +246,7 @@ function Namespace:histogram(name, help, buckets, metainfo)
namespace_metainfo(self, name, metainfo))
end

---@private
--- Register a new summary in the namespace.
---@param name string
---@param help string?
Expand All @@ -250,6 +259,7 @@ function Namespace:summary(name, help, objectives, params, metainfo)
namespace_metainfo(self, name, metainfo))
end

---@private
--- Register a callback in the namespace.
---@param callback function
---@param metainfo metrics.metainfo?
Expand All @@ -260,13 +270,15 @@ function Namespace:register_callback(callback, metainfo)
return register_callback(callback, res)
end

---@private
--- Unregister a callback from the namespace.
---@param callback function
---@return any
function Namespace.unregister_callback(_, callback)
return unregister_callback(callback)
end

---@private
--- Create a namespace object that marks collectors and callbacks with
--- selectors derived from `selector`.
---@param selector string
Expand Down
5 changes: 5 additions & 0 deletions metrics/cfg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local metrics_tarantool = require('metrics.tarantool')

-- Split a metrics.cfg include/exclude side into two parts: built-in metric
-- groups for metrics.tarantool and custom selectors for the registry filter.
---@private
---@param values string|table|nil
---@param default_value string|table
---@param empty_default string|table
Expand Down Expand Up @@ -54,6 +55,7 @@ local function split_metric_groups_and_selectors(values, default_value,
return default_metrics, custom_selectors
end

---@private
---@param cfg table
---@return table
local function set_defaults_if_empty(cfg)
Expand All @@ -72,6 +74,7 @@ local function set_defaults_if_empty(cfg)
return cfg
end

---@private
---@param cfg table
---@param opts {include?: string|table, exclude?: table, labels?: table}
---@return any
Expand Down Expand Up @@ -111,6 +114,7 @@ if _cfg_internal.initialized then
configure(_cfg, {})
end

---@private
--- Entrypoint to setup the module.
---@param self table
---@param opts {include?: string|table, exclude?: table, labels?: metrics.label_pairs}
Expand All @@ -131,6 +135,7 @@ local function __call(self, opts)
return self
end

---@private
---@param key string
---@return any
local function __index(_, key)
Expand Down
7 changes: 3 additions & 4 deletions metrics/collectors/histogram.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ local DEFAULT_BUCKETS = {.005, .01, .025, .05, .075, .1, .25, .5,
.75, 1.0, 2.5, 5.0, 7.5, 10.0, INF}

---@class metrics.collector.histogram : metrics.collector
---@field buckets number[]
---@field count_collector metrics.collector.counter
---@field sum_collector metrics.collector.counter
---@field bucket_collector metrics.collector.counter
local Histogram = Shared:new_class('histogram', {'observe_latency'})

---@private
---@param buckets number[]
---@return boolean
function Histogram.check_buckets(buckets)
Expand All @@ -27,6 +24,7 @@ function Histogram.check_buckets(buckets)
return true
end

---@private
---@param name string
---@param help string?
---@param buckets number[]?
Expand All @@ -49,6 +47,7 @@ function Histogram:new(name, help, buckets, metainfo)
return obj
end

---@private
---@param registry metrics.registry
function Histogram:set_registry(registry)
Shared.set_registry(self, registry)
Expand Down
13 changes: 6 additions & 7 deletions metrics/collectors/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@ local log = require('log')
---@alias metrics.observation {metric_name: string, label_pairs: metrics.label_pairs, value: number, timestamp: integer}

---@class metrics.collector
---@field name string
---@field help string
---@field observations table<string, number>
---@field label_pairs table<string, metrics.label_pairs>
---@field label_keys string[]|nil
---@field metainfo metrics.metainfo
---@field registry metrics.registry|nil

local Shared = {}

-- Create collector class with the list of instance methods copied from
-- this class (like an inheritance but with limited list of methods).
---@private
---@param kind string
---@param method_names string[]?
---@return metrics.collector
Expand All @@ -40,6 +34,7 @@ function Shared:new_class(kind, method_names)
return setmetatable(class, {__index = methods})
end

---@private
---@generic T
---@param self T
---@param name string
Expand All @@ -63,11 +58,13 @@ function Shared.new(self, name, help, metainfo, label_keys)
}, self)
end

---@private
---@param registry metrics.registry
function Shared:set_registry(registry)
self.registry = registry
end

---@private
---@param label_pairs metrics.label_pairs|nil
---@param label_keys string[]?
---@return string
Expand Down Expand Up @@ -182,6 +179,7 @@ local function observe_latency_tail(collector, label_pairs, start_time, ok, resu
return result, ...
end

---@private
--- Measure latency of function call
---
---@param label_pairs metrics.label_pairs|fun(ok: boolean, result: any, ...: any): metrics.label_pairs|nil
Expand All @@ -194,6 +192,7 @@ function Shared:observe_latency(label_pairs, fn, ...)
return observe_latency_tail(self, label_pairs, clock.monotonic(), pcall(fn, ...))
end

---@private
---@param label_pairs metrics.label_pairs|nil
---@return metrics.label_pairs
function Shared:append_global_labels(label_pairs)
Expand Down
13 changes: 6 additions & 7 deletions metrics/collectors/summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ local Quantile = require('metrics.quantile')
local fiber = require('fiber')

---@class metrics.collector.summary : metrics.collector
---@field count_collector metrics.collector.counter
---@field sum_collector metrics.collector.counter
---@field objectives table<number, number>|nil
---@field quantiles number[]|nil
---@field max_age_time number|nil
---@field age_buckets_count number
---@field observations table<string, table>
local Summary = Shared:new_class('summary', {'observe_latency'})

---@private
---@param name string
---@param help string?
---@param objectives table<number, number>?
Expand Down Expand Up @@ -41,6 +35,7 @@ function Summary:new(name, help, objectives, params, metainfo)
return obj
end

---@private
---@param objectives table<number, number>
---@return boolean
function Summary.check_quantiles(objectives)
Expand All @@ -52,13 +47,15 @@ function Summary.check_quantiles(objectives)
return true
end

---@private
---@param registry metrics.registry
function Summary:set_registry(registry)
Shared.set_registry(self, registry)
self.count_collector:set_registry(registry)
self.sum_collector:set_registry(registry)
end

---@private
---@param key string
function Summary:rotate_age_buckets(key)
--- @type any
Expand Down Expand Up @@ -126,6 +123,7 @@ function Summary:remove(label_pairs)
end

---@return metrics.observation[]
---@private
function Summary:collect_quantiles()
local quantiles = self.quantiles
if not self.objectives or not quantiles or next(self.observations) == nil then
Expand Down Expand Up @@ -174,6 +172,7 @@ end
-- single quantile object if summary has only one bucket
---@param label_pairs metrics.label_pairs?
---@return table|any
---@private
function Summary:get_observations(label_pairs)
local key = self.make_key(label_pairs or {})
local obs = self.observations[key]
Expand Down
1 change: 1 addition & 0 deletions metrics/const.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---@private
---@class metrics.const
---@field INF number
---@field NAN number
Expand Down
1 change: 1 addition & 0 deletions metrics/http_middleware.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function export.configure_default_collector(...)
export.set_default_collector(export.build_default_collector(...))
end

---@private
--- Measure latency and invoke collector with labels from given route.
---@param collector metrics.collector
---@param route {path: string, method: string}
Expand Down
27 changes: 3 additions & 24 deletions metrics/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,7 @@ local tarantool = require('metrics.tarantool')

local VERSION = require('metrics.version')

---@class metrics
---@field registry metrics.registry
---@field counter fun(name: string, help?: string, metainfo?: metrics.metainfo, label_keys?: string[]): metrics.collector.counter
---@field gauge fun(name: string, help?: string, metainfo?: metrics.metainfo, label_keys?: string[]): metrics.collector.gauge
---@field histogram fun(name: string, help?: string, buckets?: number[], metainfo?: metrics.metainfo): metrics.collector.histogram
---@field summary fun(name: string, help?: string, objectives?: table<number, number>,
--- params?: {age_buckets_count?: number, max_age_time?: number}, metainfo?: metrics.metainfo): metrics.collector.summary
---@field INF number
---@field NAN number
---@field clear fun(): any
---@field collectors fun(): table<string, metrics.collector>
---@field namespace fun(selector: string): metrics.namespace
---@field register_callback fun(callback: function, metainfo?: metrics.metainfo): any
---@field unregister_callback fun(...: any): any
---@field invoke_callbacks fun(): any
---@field set_global_labels fun(label_pairs?: metrics.label_pairs): any
---@field set_filter fun(include?: string|table, exclude?: string|table): any
---@field enable_default_metrics fun(include?: string|table, exclude?: table): any
---@field cfg fun(opts?: {include?: string|table, exclude?: table, labels?: metrics.label_pairs}): table
---@field http_middleware table
---@field collect fun(opts?: {invoke_callbacks?: boolean, default_only?: boolean}): metrics.observation[]
---@field _VERSION string

return setmetatable({
local metrics = setmetatable({
registry = api.registry,

counter = api.counter,
Expand Down Expand Up @@ -68,3 +45,5 @@ return setmetatable({
return nil
end
})

return metrics
2 changes: 2 additions & 0 deletions metrics/quantile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ local function insert_sample(sample_obj, value, width, delta)
sample_obj.Delta = delta
end

---@private
--- @class stream_state
--- @field f fun(s: table, r: number): number
--- @field l any
--- @field n integer
--- @field l_len integer
--- @field l_cap integer

---@private
--- @class stream
--- @field stream stream_state
--- @field b any
Expand Down
Loading
Loading