Skip to content

Commit

Permalink
#77 First try stream metrics (it doesn't compile)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmonterrubio committed Jul 15, 2020
1 parent d0e594b commit eb5e935
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
21 changes: 18 additions & 3 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ nginx_external_modules:
version: '0.33'
url: https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
- name: lua-nginx-module # Remove this module if metrics are disabled
version: '0.10.15'
url: https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
version: '0.10.17'
url: https://github.com/openresty/lua-nginx-module/archive/v0.10.17.tar.gz
- name: stream-lua-nginx-module # Remove this module if metrics are disabled
version: '0.0.8'
url: https://github.com/openresty/stream-lua-nginx-module/archive/v0.0.8.tar.gz
- name: ngx_devel_kit
version: '0.3.1' # Remove this module if metrics are disabled
url: https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
Expand All @@ -89,12 +92,24 @@ nginx_compile_time_options:
luaJIT_version: '2.1-20200102'

## Metrics
nginx_lua_prometheus_version: '0.20181120'
nginx_lua_prometheus_version: '0.20200523'
nginx_prometheus_metrics_enabled: true # Remove lua and ngx_devel_kit modules if metrics are disabled
nginx_prometheus_metrics_port: 9145
nginx_prometheus_metrics_role: nginx
nginx_prometheus_metrics_path: metrics
metrics_templates_path: "{{ playbook_dir }}/templates/metrics"
nginx_lua_scripts:
- prometheus.lua
- prometheus_resty_counter.lua
- prometheus_keys.lua

nginx_lua_external_dependencies:
- name: lua-resty-core
version: '0.1.19' # Remove this module if metrics are disabled
url: https://github.com/openresty/lua-resty-core/archive/v0.1.19.tar.gz
- name: lua-resty-lrucache
version: '0.10' # Remove this module if metrics are disabled
url: https://github.com/openresty/lua-resty-lrucache/archive/v0.10.tar.gz

## Miscellaneous
# Set true to force the download and installation of the package
Expand Down
38 changes: 38 additions & 0 deletions molecule/default/templates/conf/nginx.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,42 @@ http {
include {{ nginx_conf_path }}/sites-enabled/*;

include {{ nginx_conf_path }}/extra-conf/*;

stream {

lua_shared_dict prometheus_metrics_stream 10M;
{%- set lua_package_list = [] -%}
{%- for plugin in nginx_lua_external_dependencies -%}
{{ lua_package_list.append(nginx_conf_path + '/plugins/' + plugin.name + '-' + plugin.version + '/lib/?.lua') }}
{%- endfor -%}
{{ lua_package_list.append(nginx_conf_path + '/plugins/?.lua;;') }}
lua_package_path "{{ lua_package_list | join(';') }}"

init_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics_stream")
metric_requests = prometheus:counter("nginx_http_requests_total", "Number of HTTP requests", {"test"})
metric_connections = prometheus:gauge("nginx_http_connections", "Number of HTTP connections", {"test"})
}
log_by_lua_block {
metric_requests:inc(1, {"test"})
metric_connections:set(1, {"test"})
}

server {
listen 8000;

content_by_lua_block {
local sock = assert(ngx.req.socket(true))
local data = sock:receive()
local location = "GET /metrics"
if string.sub(data, 1, string.len(location)) == location then
ngx.say("HTTP/1.1 200 OK")
ngx.say("Content-Type: text/plain")
ngx.say("")
ngx.say(table.concat(prometheus:metric_data(), ""))
else
ngx.say("HTTP/1.1 404 Not Found")
end
}
}
}
12 changes: 10 additions & 2 deletions tasks/metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@

- name: NGINX | Copy prometheus lib
copy:
src: "{{ nginx_download_dir }}/nginx-lua-prometheus-{{ nginx_lua_prometheus_version }}/prometheus.lua"
dest: "{{ nginx_conf_path }}/plugins/prometheus.lua"
src: "{{ nginx_download_dir }}/nginx-lua-prometheus-{{ nginx_lua_prometheus_version }}/{{ item }}"
dest: "{{ nginx_conf_path }}/plugins/{{ item }}"
remote_src: true
owner: "{{ nginx_user }}"
group: "{{ nginx_group }}"
loop: "{{ nginx_lua_scripts }}"

- name: NGINX | Get prometheus lua external dependencies
unarchive:
copy: false
src: "{{ item.url }}"
dest: "{{ nginx_conf_path }}/plugins"
loop: "{{ nginx_lua_external_dependencies }}"

- name: NGINX | Copy prometheus metric server conf
template:
Expand Down
7 changes: 6 additions & 1 deletion templates/metrics/prometheus.conf.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# PROMETHEUS METRIC
lua_shared_dict prometheus_metrics 10M;
lua_package_path "{{ nginx_conf_path }}/plugins/prometheus.lua";
{%- set lua_package_list = [] -%}
{%- for plugin in nginx_lua_external_dependencies -%}
{{ lua_package_list.append(nginx_conf_path + '/plugins/' + plugin.name + '-' + plugin.version + '/lib/?.lua') }}
{%- endfor -%}
{{ lua_package_list.append(nginx_conf_path + '/plugins/?.lua;;') }}
lua_package_path "{{ lua_package_list | join(';') }}"

init_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
Expand Down

0 comments on commit eb5e935

Please sign in to comment.