diff --git a/apisix/plugins/elasticsearch-logger.lua b/apisix/plugins/elasticsearch-logger.lua index 3566da2c636d..af65f2cc19ce 100644 --- a/apisix/plugins/elasticsearch-logger.lua +++ b/apisix/plugins/elasticsearch-logger.lua @@ -20,8 +20,10 @@ local log_util = require("apisix.utils.log-util") local bp_manager_mod = require("apisix.utils.batch-processor-manager") local plugin = require("apisix.plugin") local ngx = ngx +local ngx_re = ngx.re local str_format = core.string.format local math_random = math.random +local os_date = os.date local pairs = pairs local plugin_name = "elasticsearch-logger" @@ -200,11 +202,37 @@ local function get_es_major_version(uri, conf) end -local function get_logger_entry(conf, ctx) +local function replace_time(m) + local time_format = m[1] + local time = os_date(time_format) + if not time then + core.log.error("failed to parse time format: ", time_format) + return "" + end + return time +end + + +local function resolve_index_vars(index, var) + local new_index, _, err = ngx_re.gsub(index, "(?= 300 then + ngx.status = code + end + + local code, _, body = t("/hello") + } + } +--- error_log eval +qr/body: \{"index":\{"_index":"services-\d\d\d\d\.\d\d\.\d\d"\}\}/ + + + +=== TEST 6: test APISIX variable in index +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + + local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, { + uri = "/hello", + upstream = { + type = "roundrobin", + nodes = { + ["127.0.0.1:1980"] = 1 + } + }, + plugins = { + ["elasticsearch-logger"] = { + endpoint_addr = "http://127.0.0.1:9201", + field = { + index = "services-$host" + }, + auth = { + username = "elastic", + password = "123456" + }, + batch_max_size = 1, + inactive_timeout = 1, + } + } + }) + + if code >= 300 then + ngx.status = code + end + + local code, _, body = t("/hello") + } + } +--- error_log eval +qr/body: \{"index":\{"_index":"services-127.0.0.1"\}\}/ + + + +=== TEST 7: test both APISIX variable and date variable in index +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + + local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, { + uri = "/hello", + upstream = { + type = "roundrobin", + nodes = { + ["127.0.0.1:1980"] = 1 + } + }, + plugins = { + ["elasticsearch-logger"] = { + endpoint_addr = "http://127.0.0.1:9201", + field = { + index = "services-$host-{%Y.%m.%d}" + }, + auth = { + username = "elastic", + password = "123456" + }, + batch_max_size = 1, + inactive_timeout = 1, + } + } + }) + + if code >= 300 then + ngx.status = code + end + + local code, _, body = t("/hello") + } + } +--- error_log eval +qr/body: \{"index":\{"_index":"services-127.0.0.1-\d\d\d\d\.\d\d\.\d\d"\}\}/ + + + +=== TEST 8: dynamic index template should not be mutated across requests +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local httpc = http.new() + local t = require("lib.test_admin").test + + local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, { + uri = "/hello", + upstream = { + type = "roundrobin", + nodes = { + ["127.0.0.1:1980"] = 1 + } + }, + plugins = { + ["elasticsearch-logger"] = { + endpoint_addr = "http://127.0.0.1:9201", + field = { + index = "services-$arg_id-{%Y.%m.%d}" + }, + auth = { + username = "elastic", + password = "123456" + }, + batch_max_size = 1, + inactive_timeout = 1, + } + } + }) + + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + local port = ngx.var.server_port + local res, err = httpc:request_uri("http://127.0.0.1:" .. port .. "/hello?id=first", {method = "GET"}) + if not res then + ngx.say("request 1 failed: ", err) + return + end + res, err = httpc:request_uri("http://127.0.0.1:" .. port .. "/hello?id=second", {method = "GET"}) + if not res then + ngx.say("request 2 failed: ", err) + return + end + ngx.sleep(2) + ngx.say("done") + } + } +--- response_body +done +--- error_log eval +[qr/body: \{"index":\{"_index":"services-first-\d\d\d\d\.\d\d\.\d\d"\}\}/, qr/body: \{"index":\{"_index":"services-second-\d\d\d\d\.\d\d\.\d\d"\}\}/] +--- timeout: 5 + + + +=== TEST 9: ${xx} variable syntax should not trigger time replacement +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local httpc = http.new() + local t = require("lib.test_admin").test + + local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, { + uri = "/hello", + upstream = { + type = "roundrobin", + nodes = { + ["127.0.0.1:1980"] = 1 + } + }, + plugins = { + ["elasticsearch-logger"] = { + endpoint_addr = "http://127.0.0.1:9201", + field = { + index = "services-${arg_id}-{%Y.%m.%d}" + }, + auth = { + username = "elastic", + password = "123456" + }, + batch_max_size = 1, + inactive_timeout = 1, + } + } + }) + + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + local port = ngx.var.server_port + local res, err = httpc:request_uri("http://127.0.0.1:" .. port .. "/hello?id=myservice", {method = "GET"}) + if not res then + ngx.say("request failed: ", err) + return + end + ngx.sleep(2) + ngx.say("done") + } + } +--- response_body +done +--- error_log eval +qr/body: \{"index":\{"_index":"services-myservice-\d\d\d\d\.\d\d\.\d\d"\}\}/ +--- no_error_log +failed to parse time format +--- timeout: 5