-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[apicast] prometheus metrics policy #5
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
aceb192
[apicast] update roverfile.lock
mikz 6ad3c2d
[apicast] prometheus metrics policy
mikz 21b2dfa
[apicast] add http stats to prometheus metrics
mikz 93c84cd
[apicast] export metrics about rate limits
mikz 4eb00b5
[apicast] prometheus metrics for balancer
mikz 71ce807
[apicast] add upstream metrics
mikz ca8ccbf
[apicast] expose metrics on port 9100
mikz eba9e37
[apicast] test metrics
mikz df43d6b
[apicast] metrics about shared memory use
mikz d2f09fc
[apicast] make test to verify more cases of issues
mikz aba27c7
[apicast] rate limit should default to 429 status
mikz cdbd29e
[apicast] fix module test
mikz 5df84e4
[apicast] improve balancer metrics
mikz 3362ebb
[apicast] rate-limit policy should happen in the rewrite phase
mikz 7fff097
[apicast] rate limit should not print errors
mikz 0b6c979
Merge pull request #5 from 3scale/apicast-prometheus
mikz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
requires 'Test::APIcast', '0.04'; | ||
requires 'Test::APIcast', '0.11'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lua_capture_error_log 4k; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
return require('metrics') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
local _M = require('apicast.policy').new('Metrics', '0.1') | ||
|
||
local errlog = require('ngx.errlog') | ||
local prometheus = require('apicast.prometheus') | ||
local tonumber = tonumber | ||
local select = select | ||
local find = string.find | ||
local pairs = pairs | ||
|
||
local new = _M.new | ||
|
||
local log_map = { | ||
'emerg', | ||
'alert', | ||
'crit', | ||
'error', | ||
'warn', | ||
'notice', | ||
'info', | ||
'debug', | ||
} | ||
|
||
|
||
local function find_i(t, value) | ||
for i=1, #t do | ||
if t[i] == value then return i end | ||
end | ||
end | ||
|
||
local empty = {} | ||
|
||
local function get_logs(max) | ||
return errlog.get_logs(max) or empty | ||
end | ||
|
||
function _M.new(configuration) | ||
local m = new() | ||
|
||
local config = configuration or empty | ||
local filter_level = config.log_level or 'error' | ||
|
||
local i = find_i(log_map, filter_level) | ||
|
||
if not i then | ||
ngx.log(ngx.WARN, _M._NAME, ': invalid level: ', filter_level, ' using error instead') | ||
i = find_i(log_map, 'error') | ||
end | ||
|
||
m.filter_level = i | ||
-- how many logs to take in one iteration | ||
m.max_logs = tonumber(config.max_logs) or 100 | ||
|
||
return m | ||
end | ||
|
||
local logs_metric = prometheus('counter', 'nginx_error_log', "Items in nginx error log", {'level'}) | ||
local http_connections_metric = prometheus('gauge', 'nginx_http_connections', 'Number of HTTP connections', {'state'}) | ||
local shdict_capacity_metric = prometheus('gauge', 'openresty_shdict_capacity', 'OpenResty shared dictionary capacity', {'dict'}) | ||
local shdict_free_space_metric = prometheus('gauge', 'openresty_shdict_free_space', 'OpenResty shared dictionary free space', {'dict'}) | ||
|
||
|
||
local metric_labels = {} | ||
|
||
local function metric_op(op, metric, value, label) | ||
if not metric then return end | ||
metric_labels[1] = label | ||
metric[op](metric, tonumber(value) or 0, metric_labels) | ||
end | ||
|
||
local function metric_set(metric, value, label) | ||
return metric_op('set', metric, value, label) | ||
end | ||
|
||
local function metric_inc(metric, label) | ||
return metric_op('inc', metric, 1, label) | ||
end | ||
|
||
function _M:init() | ||
local ok, err = errlog.set_filter_level(self.filter_level) | ||
|
||
get_logs(100) -- to throw them away after setting the filter level (and get rid of debug ones) | ||
|
||
if not ok then | ||
ngx.log(ngx.WARN, self._NAME, ' failed to set errlog filter level: ', err) | ||
end | ||
|
||
for name,dict in pairs(ngx.shared) do | ||
metric_set(shdict_capacity_metric, dict:capacity(), name) | ||
end | ||
end | ||
|
||
function _M:metrics() | ||
local logs = get_logs(self.max_logs) | ||
|
||
for i = 1, #logs, 3 do | ||
metric_inc(logs_metric, log_map[logs[i]] or 'unknown') | ||
end | ||
|
||
local response = ngx.location.capture("/nginx_status") | ||
|
||
if response.status == 200 then | ||
local accepted, handled, total = select(3, find(response.body, [[accepts handled requests%s+(%d+) (%d+) (%d+)]])) | ||
local var = ngx.var | ||
|
||
metric_set(http_connections_metric, var.connections_reading, 'reading') | ||
metric_set(http_connections_metric, var.connections_waiting, 'waiting') | ||
metric_set(http_connections_metric, var.connections_writing, 'writing') | ||
metric_set(http_connections_metric, var.connections_active, 'active') | ||
metric_set(http_connections_metric, accepted, 'accepted') | ||
metric_set(http_connections_metric, handled, 'handled') | ||
metric_set(http_connections_metric, total, 'total') | ||
else | ||
prometheus:log_error('Could not get status from nginx') | ||
end | ||
|
||
for name,dict in pairs(ngx.shared) do | ||
metric_set(shdict_free_space_metric, dict:free_space(), name) | ||
end | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
BEGIN { | ||
$ENV{TEST_NGINX_APICAST_BINARY} ||= 'rover exec apicast'; | ||
$ENV{APICAST_POLICY_LOAD_PATH} = './policies'; | ||
$ENV{APICAST_BALANCER_WHITELIST} = '127.0.0.1/32'; | ||
$ENV{METRICS_LOG_LEVEL} = 'info'; | ||
} | ||
|
||
use strict; | ||
use warnings FATAL => 'all'; | ||
use Test::APIcast::Blackbox 'no_plan'; | ||
|
||
repeat_each(1); | ||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: metrics endpoint | ||
--- environment_file: config/cloud_hosted.lua | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "cloud_hosted.upstream", "version": "0.1", | ||
"configuration": { | ||
"url": "http://127.0.0.1:$TEST_NGINX_SERVER_PORT", "host": "prometheus" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request | ||
GET /metrics | ||
--- response_body | ||
# HELP cloud_hosted_balancer Cloud hosted balancer | ||
# TYPE cloud_hosted_balancer counter | ||
cloud_hosted_balancer{status="success"} 1 | ||
# HELP nginx_error_log Items in nginx error log | ||
# TYPE nginx_error_log counter | ||
nginx_error_log{level="info"} 1 | ||
# HELP nginx_http_connections Number of HTTP connections | ||
# TYPE nginx_http_connections gauge | ||
nginx_http_connections{state="accepted"} 2 | ||
nginx_http_connections{state="active"} 2 | ||
nginx_http_connections{state="handled"} 2 | ||
nginx_http_connections{state="reading"} 0 | ||
nginx_http_connections{state="total"} 2 | ||
nginx_http_connections{state="waiting"} 0 | ||
nginx_http_connections{state="writing"} 2 | ||
# HELP nginx_metric_errors_total Number of nginx-lua-prometheus errors | ||
# TYPE nginx_metric_errors_total counter | ||
nginx_metric_errors_total 0 | ||
# HELP openresty_shdict_capacity OpenResty shared dictionary capacity | ||
# TYPE openresty_shdict_capacity gauge | ||
openresty_shdict_capacity{dict="api_keys"} 10485760 | ||
openresty_shdict_capacity{dict="configuration"} 10485760 | ||
openresty_shdict_capacity{dict="init"} 16384 | ||
openresty_shdict_capacity{dict="locks"} 1048576 | ||
openresty_shdict_capacity{dict="prometheus_metrics"} 16777216 | ||
openresty_shdict_capacity{dict="rate_limit_req_store"} 10485760 | ||
# HELP openresty_shdict_free_space OpenResty shared dictionary free space | ||
# TYPE openresty_shdict_free_space gauge | ||
openresty_shdict_free_space{dict="api_keys"} 10412032 | ||
openresty_shdict_free_space{dict="configuration"} 10412032 | ||
openresty_shdict_free_space{dict="init"} 4096 | ||
openresty_shdict_free_space{dict="locks"} 1032192 | ||
openresty_shdict_free_space{dict="prometheus_metrics"} 16662528 | ||
openresty_shdict_free_space{dict="rate_limit_req_store"} 10412032 | ||
--- error_code: 200 | ||
--- no_error_log | ||
[error] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is enough 10 log entries.
So it will collect last 10 log entries with desired log level.
For example when you have between prometheus pulls 15 log entries like: 5 error, 10 warning then the error will not appear.
It is always good to set the desired log level to something we actually need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So maybe this conf could be an env var? Because we certainly will have to play with the Prometheus scrape time and the log capture size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be when it would be part of the liquid template in the main repo.
But here it could not be templated.
And disregard my comment about 10 entries. It fits more.
Well I think 4k is fine when properly configured. We should not capture log levels we don't care about. So lets say there are top ones: emerg, alert, crit, error.
And we configure to capture
error
and up. We would not really care if there are some missing higher levels, because the error itself is enough to trigger a warning.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this is controlled by the "log_map" var? can we expose this one as ENV ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see so
set_filter_level
will capture everything>=METRICS_LOG_LEVEL
right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmprusi done in f45985c as
METRICS_LOG_LEVEL
. better name suggestions much welcome :)@maneta yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! :)