Skip to content
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

get rid of METRICS_PERIOD configuration item #82

Merged
merged 12 commits into from
Oct 22, 2024
Merged
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
13 changes: 5 additions & 8 deletions lib/crowdsec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local APPSEC_VERB_HEADER = "x-crowdsec-appsec-verb"
local APPSEC_URI_HEADER = "x-crowdsec-appsec-uri"
local APPSEC_USER_AGENT_HEADER = "x-crowdsec-appsec-user-agent"
local REMEDIATION_API_KEY_HEADER = 'x-api-key'
local METRICS_PERIOD = 300


--- init function
Expand Down Expand Up @@ -86,10 +87,6 @@ function csmod.init(configFile, userAgent)
runtime.conf["SSL_VERIFY"] = true
end

if runtime.conf["METRICS_PERIOD"] == "" or runtime.conf["METRICS_PERIOD"] == nil then
runtime.conf["METRICS_PERIOD"] = 300
end

runtime.cache:set("metrics_startup_time", ngx.time()) -- to make sure we have only one thread sending metrics
runtime.cache:set("metrics_first_run",true) -- to avoid sending metrics before the first period

Expand Down Expand Up @@ -148,11 +145,11 @@ end

local function Setup_metrics()
local function Setup_metrics_timer()
local ok, err = ngx.timer.at(runtime.conf["METRICS_PERIOD"], Setup_metrics)
local ok, err = ngx.timer.at(METRICS_PERIOD, Setup_metrics)
if not ok then
error("Failed to create the timer: " .. (err or "unknown"))
else
ngx.log(ngx.ERR, "Metrics timer started in " .. tostring(runtime.conf["METRICS_PERIOD"]) .. " seconds")
ngx.log(ngx.ERR, "Metrics timer started in " .. tostring(METRICS_PERIOD) .. " seconds")
end
end
local first_run = runtime.cache:get("metrics_first_run")
Expand All @@ -164,12 +161,12 @@ local function Setup_metrics()
return
end
local started = runtime.cache:get("metrics_startup_time")
if ngx.time() - started >= runtime.conf["METRICS_PERIOD"] then
if ngx.time() - started >= METRICS_PERIOD then
metrics:sendMetrics(
runtime.conf["API_URL"],
{['User-Agent']=runtime.userAgent,[REMEDIATION_API_KEY_HEADER]=runtime.conf["API_KEY"],["Content-Type"]="application/json"},
runtime.conf["SSL_VERIFY"],
runtime.conf["METRICS_PERIOD"]
METRICS_PERIOD
)
runtime.cache:set("metrics_startup_time",ngx.time()) --TODO add err handling
--TODO rename the cache key
Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/crowdsec/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function config.loadConfig(file)
end
local conf = {}
local valid_params = {'ENABLED', 'ENABLE_INTERNAL', 'API_URL', 'API_KEY', 'BOUNCING_ON_TYPE', 'MODE', 'SECRET_KEY', 'SITE_KEY', 'BAN_TEMPLATE_PATH' ,'CAPTCHA_TEMPLATE_PATH', 'REDIRECT_LOCATION', 'RET_CODE', 'EXCLUDE_LOCATION', 'FALLBACK_REMEDIATION', 'CAPTCHA_PROVIDER', 'APPSEC_URL', 'APPSEC_FAILURE_ACTION', 'ALWAYS_SEND_TO_APPSEC', 'SSL_VERIFY'}
local valid_int_params = {'CACHE_EXPIRATION', 'CACHE_SIZE', 'REQUEST_TIMEOUT', 'UPDATE_FREQUENCY', 'CAPTCHA_EXPIRATION', 'APPSEC_CONNECT_TIMEOUT', 'APPSEC_SEND_TIMEOUT', 'APPSEC_PROCESS_TIMEOUT', 'STREAM_REQUEST_TIMEOUT', 'METRICS_PERIOD'}
local valid_int_params = {'CACHE_EXPIRATION', 'CACHE_SIZE', 'REQUEST_TIMEOUT', 'UPDATE_FREQUENCY', 'CAPTCHA_EXPIRATION', 'APPSEC_CONNECT_TIMEOUT', 'APPSEC_SEND_TIMEOUT', 'APPSEC_PROCESS_TIMEOUT', 'STREAM_REQUEST_TIMEOUT'}
local valid_bouncing_on_type_values = {'ban', 'captcha', 'all'}
local valid_truefalse_values = {'false', 'true'}
local default_values = {
Expand All @@ -64,7 +64,6 @@ function config.loadConfig(file)
['APPSEC_FAILURE_ACTION'] = "passthrough",
['SSL_VERIFY'] = "true",
['ALWAYS_SEND_TO_APPSEC'] = "false",
['METRICS_PERIOD'] = 300,
}
for line in io.lines(file) do
local isOk = false
Expand Down