Skip to content

Commit a933f2e

Browse files
authored
Merge pull request #1707 from evan361425/patch-1
feat: add prometheus
2 parents 86e0b7f + 57255d4 commit a933f2e

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
---@meta
2+
3+
---@class PrometheusLib
4+
local PrometheusLib = {}
5+
6+
---@class PrometheusOptions
7+
---is a table of configuration options that can be provided.
8+
local PrometheusOptions = {}
9+
10+
---metric name prefix.
11+
---This string will be prepended to metric names on output.
12+
PrometheusOptions.prefix = ''
13+
---Can be used to change the default name of error metric (see
14+
---[Built-in metrics](https://github.com/knyar/nginx-lua-prometheus#built-in-metrics)
15+
---for details).
16+
PrometheusOptions.error_metric_name = ''
17+
---sets per-worker counter sync interval in seconds.
18+
---This sets the boundary on eventual consistency of counter metrics.
19+
---Defaults to `1`.
20+
PrometheusOptions.sync_interval = 0
21+
---maximum size of a per-metric lookup table maintained by
22+
---each worker to cache full metric names. Defaults to `1000`.
23+
---If you have metrics with extremely high cardinality and lots
24+
---of available RAM, you might want to increase this to avoid
25+
---cache getting flushed too often.
26+
---Decreasing this makes sense if you have a very large number of metrics
27+
---or need to minimize memory usage of this library.
28+
PrometheusOptions.lookup_max_size = 0
29+
30+
---Initializes the module.
31+
---This should be called once from the
32+
---[init_worker_by_lua_block](https://github.com/openresty/lua-nginx-module#init_worker_by_lua_block)
33+
---section of nginx configuration.
34+
---
35+
---Example:
36+
---```
37+
---init_worker_by_lua_block {
38+
--- prometheus = require("prometheus").init("prometheus_metrics", {sync_interval=3})
39+
---}
40+
---```
41+
---@param dict_name string is the name of the nginx shared dictionary which will be used to store all metrics. Defaults to `prometheus_metrics` if not specified.
42+
---@param options? PrometheusOptions is a table of configuration options that can be provided.
43+
---@return Prometheus prometheus Returns a `prometheus` object that should be used to register metrics.
44+
PrometheusLib.init = function(dict_name, options) end
45+
46+
---@class Prometheus
47+
local Prometheus = {}
48+
49+
---Registers a counter.
50+
---
51+
---Should be called once for each gauge from the
52+
---[init_worker_by_lua_block](https://github.com/openresty/lua-nginx-module#init_worker_by_lua_block)
53+
---section.
54+
---
55+
---Example:
56+
---```
57+
---init_worker_by_lua_block {
58+
--- prometheus = require("prometheus").init("prometheus_metrics")
59+
---
60+
--- metric_bytes = prometheus:counter(
61+
--- "nginx_http_request_size_bytes", "Total size of incoming requests")
62+
--- metric_requests = prometheus:counter(
63+
--- "nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
64+
---}
65+
---```
66+
---@param name string is the name of the metric.
67+
---@param description? string is the text description. Optional.
68+
---@param label_names? string[] is an array of label names for the metric. Optional.
69+
---@return PrometheusCounter
70+
function Prometheus:counter(name, description, label_names) end
71+
72+
---Registers a gauge.
73+
---
74+
---Should be called once for each gauge from the
75+
---[init_worker_by_lua_block](https://github.com/openresty/lua-nginx-module#init_worker_by_lua_block)
76+
---section.
77+
---
78+
---Example:
79+
---```
80+
---init_worker_by_lua_block {
81+
--- prometheus = require("prometheus").init("prometheus_metrics")
82+
---
83+
--- metric_connections = prometheus:gauge(
84+
--- "nginx_http_connections", "Number of HTTP connections", {"state"})
85+
---}
86+
---```
87+
---@param name string is the name of the metric.
88+
---@param description? string is the text description. Optional.
89+
---@param label_names? string[] is an array of label names for the metric. Optional.
90+
---@return PrometheusGauge
91+
function Prometheus:gauge(name, description, label_names) end
92+
93+
---Registers a histogram.
94+
---
95+
---Should be called once for each histogram from the
96+
---[init_worker_by_lua_block](https://github.com/openresty/lua-nginx-module#init_worker_by_lua_block)
97+
---section.
98+
---
99+
---Example:
100+
---```
101+
---init_worker_by_lua_block {
102+
--- prometheus = require("prometheus").init("prometheus_metrics")
103+
---
104+
--- metric_latency = prometheus:histogram(
105+
--- "nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
106+
--- metric_response_sizes = prometheus:histogram(
107+
--- "nginx_http_response_size_bytes", "Size of HTTP responses", nil,
108+
--- {10,100,1000,10000,100000,1000000})
109+
---}
110+
---```
111+
---@param name string is the name of the metric.
112+
---@param description? string is the text description. Optional.
113+
---@param label_names? string[] is an array of label names for the metric. Optional.
114+
---@param buckets? number[] is an array of numbers defining bucket boundaries. Optional, defaults to 20 latency buckets covering a range from 5ms to 10s (in seconds).
115+
---@return PrometheusHist
116+
function Prometheus:histogram(name, description, label_names, buckets) end
117+
118+
---Presents all metrics in a text format compatible with Prometheus.
119+
---This should be called in [content_by_lua_block](https://github.com/openresty/lua-nginx-module#content_by_lua_block)
120+
---to expose the metrics on a separate HTTP page.
121+
---
122+
---Example:
123+
---```
124+
---location /metrics {
125+
--- content_by_lua_block { prometheus:collect() }
126+
--- allow 192.168.0.0/16;
127+
--- deny all;
128+
---}
129+
---```
130+
function Prometheus:collect() end
131+
132+
---Returns metric data as an array of strings.
133+
---@return string[]
134+
function Prometheus:metric_data() end
135+
136+
---@class PrometheusCounter
137+
local PrometheusCounter = {}
138+
139+
---Increments a previously registered counter.
140+
---This is usually called from log_by_lua_block globally or per server/location.
141+
---
142+
---The number of label values should match the number of label names
143+
---defined when the counter was registered using `prometheus:counter()`.
144+
---No label values should be provided for counters with no labels.
145+
---Non-printable characters will be stripped from label values.
146+
---
147+
---Example:
148+
---```
149+
---log_by_lua_block {
150+
--- metric_bytes:inc(tonumber(ngx.var.request_length))
151+
--- metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
152+
---}
153+
---```
154+
---@param value number is a value that should be added to the counter. Defaults to 1.
155+
---@param label_values? string[] is an array of label values.
156+
function PrometheusCounter:inc(value, label_values) end
157+
158+
---Delete a previously registered counter.
159+
---This is usually called when you don't need to observe such counter
160+
---(or a metric with specific label values in this counter) any more.
161+
---If this counter has labels, you have to pass `label_values` to delete
162+
---the specific metric of this counter.
163+
---If you want to delete all the metrics of a counter with labels,
164+
---you should call `Counter:reset()`.
165+
---
166+
---This function will wait for sync_interval before deleting the metric to
167+
---allow all workers to sync their counters.
168+
---@param label_values string[] The number of label values should match the number of label names defined when the counter was registered using `prometheus:counter()`. No label values should be provided for counters with no labels. Non-printable characters will be stripped from label values.
169+
function PrometheusCounter:del(label_values) end
170+
171+
---Delete all metrics for a previously registered counter.
172+
---If this counter have no labels, it is just the same as `Counter:del()` function.
173+
---If this counter have labels, it will delete all the metrics with different label values.
174+
---
175+
---This function will wait for `sync_interval` before deleting the metrics to allow all workers to sync their counters.
176+
function PrometheusCounter:reset() end
177+
178+
---@class PrometheusGauge
179+
local PrometheusGauge = {}
180+
181+
---Sets the current value of a previously registered gauge.
182+
---This could be called from
183+
---[log_by_lua_block](https://github.com/openresty/lua-nginx-module#log_by_lua_block)
184+
---globally or per server/location to modify a gauge on each request, or from
185+
---[content_by_lua_block](https://github.com/openresty/lua-nginx-module#content_by_lua_block)
186+
---just before `prometheus::collect()` to return a real-time value.
187+
---
188+
---@param value number is a value that the gauge should be set to. Required.
189+
---@param label_values? string[] is an array of label values.
190+
function PrometheusGauge:set(value, label_values) end
191+
192+
---Increments a previously registered gauge.
193+
---This is usually called from log_by_lua_block globally or per server/location.
194+
---
195+
---The number of label values should match the number of label names
196+
---defined when the gauge was registered using `prometheus:gauge()`.
197+
---No label values should be provided for gauge with no labels.
198+
---Non-printable characters will be stripped from label values.
199+
---@param value number is a value that should be added to the gauge. Defaults to 1.
200+
---@param label_values? string[] is an array of label values.
201+
function PrometheusGauge:inc(value, label_values) end
202+
203+
---Delete a previously registered gauge.
204+
---This is usually called when you don't need to observe such gauge
205+
---(or a metric with specific label values in this gauge) any more.
206+
---If this gauge has labels, you have to pass `label_values` to delete
207+
---the specific metric of this gauge.
208+
---If you want to delete all the metrics of a gauge with labels,
209+
---you should call `Gauge:reset()`.
210+
---
211+
---This function will wait for sync_interval before deleting the metric to
212+
---allow all workers to sync their counters.
213+
---@param label_values string[] The number of label values should match the number of label names defined when the gauge was registered using `prometheus:gauge()`. No label values should be provided for counters with no labels. Non-printable characters will be stripped from label values.
214+
function PrometheusGauge:del(label_values) end
215+
216+
---Delete all metrics for a previously registered gauge.
217+
---If this gauge have no labels, it is just the same as `Gauge:del()` function.
218+
---If this gauge have labels, it will delete all the metrics with different
219+
---label values.
220+
function PrometheusGauge:reset() end
221+
222+
---@class PrometheusHist
223+
local PrometheusHist = {}
224+
225+
---Records a value in a previously registered histogram.
226+
---Usually called from
227+
---[log_by_lua_block](https://github.com/openresty/lua-nginx-module#log_by_lua_block)
228+
---globally or per server/location.
229+
---
230+
---Example:
231+
---```
232+
---log_by_lua_block {
233+
--- metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
234+
--- metric_response_sizes:observe(tonumber(ngx.var.bytes_sent))
235+
---}
236+
---```
237+
---@param value string is a value that should be recorded. Required.
238+
---@param label_values? string[] is an array of label values.
239+
function PrometheusHist:observe(value, label_values) end
240+
241+
---Delete all metrics for a previously registered histogram.
242+
---
243+
---This function will wait for `sync_interval` before deleting the
244+
---metrics to allow all workers to sync their counters.
245+
function PrometheusHist:reset() end
246+
247+
return PrometheusLib

0 commit comments

Comments
 (0)