You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/08/14 12:52:18 UTC

[GitHub] [apisix] spacewander commented on a diff in pull request #7643: feat: add elasticsearch-logging

spacewander commented on code in PR #7643:
URL: https://github.com/apache/apisix/pull/7643#discussion_r945284179


##########
apisix/plugins/elasticsearch-logging.lua:
##########
@@ -0,0 +1,154 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local ngx             = ngx
+local core            = require("apisix.core")
+local ngx_now         = ngx.now
+local http            = require("resty.http")
+local log_util        = require("apisix.utils.log-util")
+local bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+
+local DEFAULT_ELASTICSEARCH_SOURCE = "apache-apisix-elasticsearch-logging"
+
+local plugin_name = "elasticsearch-logging"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+local str_format = core.string.format
+local str_sub = string.sub
+
+
+local schema = {
+    type = "object",
+    properties = {
+        endpoint = {
+            type = "object",
+            properties = {
+                uri = core.schema.uri_def,
+                index = { type = "string"},
+                type = { type = "string"},
+                username = { type = "string"},
+                password = { type = "string"},
+                timeout = {
+                    type = "integer",
+                    minimum = 1,
+                    default = 10
+                },
+                ssl_verify = {
+                    type = "boolean",
+                    default = true
+                }
+            },
+            required = { "uri", "index" }
+        },
+    },
+    required = { "endpoint" },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 413,
+    name = plugin_name,
+    schema = batch_processor_manager:wrap_schema(schema),
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function get_logger_entry(conf)
+    local entry = log_util.get_full_log(ngx, conf)
+    return core.json.encode({
+            create = {
+                _index = conf.endpoint.index,
+                _type = conf.endpoint.type
+            }
+        }) .. "\n" ..
+        core.json.encode({
+            time = ngx_now(),
+            host = entry.server.hostname,
+            source = DEFAULT_ELASTICSEARCH_SOURCE,
+            request_url = entry.request.url,
+            request_method = entry.request.method,
+            request_headers = entry.request.headers,
+            request_query = entry.request.querystring,
+            request_size = entry.request.size,
+            response_headers = entry.response.headers,
+            response_status = entry.response.status,
+            response_size = entry.response.size,
+            latency = entry.latency,
+            upstream = entry.upstream,
+        }) .. "\n"
+end
+
+
+local function send_to_elasticsearch(conf, entries)
+    local httpc, err = http.new()
+    if not httpc then
+        return false, str_format("create http error: %s", err)
+    end
+
+    local uri = conf.endpoint.uri ..
+        (str_sub(conf.endpoint.uri, -1) == "/" and "_bulk" or "/_bulk")

Review Comment:
   Use string.byte would be better



##########
apisix/plugins/elasticsearch-logging.lua:
##########
@@ -0,0 +1,154 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local ngx             = ngx
+local core            = require("apisix.core")
+local ngx_now         = ngx.now
+local http            = require("resty.http")
+local log_util        = require("apisix.utils.log-util")
+local bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+
+local DEFAULT_ELASTICSEARCH_SOURCE = "apache-apisix-elasticsearch-logging"
+
+local plugin_name = "elasticsearch-logging"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+local str_format = core.string.format
+local str_sub = string.sub
+
+
+local schema = {
+    type = "object",
+    properties = {
+        endpoint = {
+            type = "object",
+            properties = {
+                uri = core.schema.uri_def,
+                index = { type = "string"},
+                type = { type = "string"},
+                username = { type = "string"},

Review Comment:
   We can store username & password in an additional field like https://github.com/apache/apisix/blob/a8d03acdc1f7253960ede56a580487fc5219e9d6/apisix/plugins/kafka-proxy.lua#L25
   So that we can require them easily.



##########
apisix/plugins/elasticsearch-logging.lua:
##########
@@ -0,0 +1,154 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local ngx             = ngx
+local core            = require("apisix.core")
+local ngx_now         = ngx.now
+local http            = require("resty.http")
+local log_util        = require("apisix.utils.log-util")
+local bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+
+local DEFAULT_ELASTICSEARCH_SOURCE = "apache-apisix-elasticsearch-logging"
+
+local plugin_name = "elasticsearch-logging"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+local str_format = core.string.format
+local str_sub = string.sub
+
+
+local schema = {
+    type = "object",
+    properties = {
+        endpoint = {

Review Comment:
   Why do we wrap all the fields in an extra endpoint field?



##########
apisix/plugins/elasticsearch-logging.lua:
##########
@@ -0,0 +1,154 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local ngx             = ngx
+local core            = require("apisix.core")
+local ngx_now         = ngx.now
+local http            = require("resty.http")
+local log_util        = require("apisix.utils.log-util")
+local bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+
+local DEFAULT_ELASTICSEARCH_SOURCE = "apache-apisix-elasticsearch-logging"
+
+local plugin_name = "elasticsearch-logging"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+local str_format = core.string.format
+local str_sub = string.sub
+
+
+local schema = {
+    type = "object",
+    properties = {
+        endpoint = {
+            type = "object",
+            properties = {
+                uri = core.schema.uri_def,
+                index = { type = "string"},
+                type = { type = "string"},
+                username = { type = "string"},
+                password = { type = "string"},
+                timeout = {
+                    type = "integer",
+                    minimum = 1,
+                    default = 10
+                },
+                ssl_verify = {
+                    type = "boolean",
+                    default = true
+                }
+            },
+            required = { "uri", "index" }
+        },
+    },
+    required = { "endpoint" },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 413,
+    name = plugin_name,
+    schema = batch_processor_manager:wrap_schema(schema),
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function get_logger_entry(conf)
+    local entry = log_util.get_full_log(ngx, conf)
+    return core.json.encode({
+            create = {
+                _index = conf.endpoint.index,
+                _type = conf.endpoint.type
+            }
+        }) .. "\n" ..
+        core.json.encode({
+            time = ngx_now(),
+            host = entry.server.hostname,

Review Comment:
   Why should we invent a format structure for a specific plugin?



##########
apisix/plugins/elasticsearch-logging.lua:
##########
@@ -0,0 +1,154 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local ngx             = ngx
+local core            = require("apisix.core")
+local ngx_now         = ngx.now
+local http            = require("resty.http")
+local log_util        = require("apisix.utils.log-util")
+local bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+
+local DEFAULT_ELASTICSEARCH_SOURCE = "apache-apisix-elasticsearch-logging"
+
+local plugin_name = "elasticsearch-logging"

Review Comment:
   We should call it elasticsearch-logger like the kafka-logger plugin?



##########
apisix/plugins/elasticsearch-logging.lua:
##########
@@ -0,0 +1,154 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local ngx             = ngx

Review Comment:
   We should move the localized variable after `require ...`



##########
apisix/plugins/elasticsearch-logging.lua:
##########
@@ -0,0 +1,154 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local ngx             = ngx
+local core            = require("apisix.core")
+local ngx_now         = ngx.now
+local http            = require("resty.http")
+local log_util        = require("apisix.utils.log-util")
+local bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+
+local DEFAULT_ELASTICSEARCH_SOURCE = "apache-apisix-elasticsearch-logging"
+
+local plugin_name = "elasticsearch-logging"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+local str_format = core.string.format
+local str_sub = string.sub
+
+
+local schema = {
+    type = "object",
+    properties = {
+        endpoint = {
+            type = "object",
+            properties = {
+                uri = core.schema.uri_def,
+                index = { type = "string"},
+                type = { type = "string"},
+                username = { type = "string"},
+                password = { type = "string"},
+                timeout = {
+                    type = "integer",
+                    minimum = 1,
+                    default = 10
+                },
+                ssl_verify = {
+                    type = "boolean",
+                    default = true
+                }
+            },
+            required = { "uri", "index" }
+        },
+    },
+    required = { "endpoint" },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 413,
+    name = plugin_name,
+    schema = batch_processor_manager:wrap_schema(schema),
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function get_logger_entry(conf)
+    local entry = log_util.get_full_log(ngx, conf)

Review Comment:
   Please also support the custom log format.



##########
t/plugin/elasticsearch-logging.t:
##########
@@ -0,0 +1,299 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local ok, err
+            local configs = {
+                -- full configuration
+                {
+                    endpoint = {
+                        uri = "http://127.0.0.1:9200",
+                        index = "services",
+                        type = "collector",
+                        timeout = 60,
+                        username = "elastic",
+                        password = "123456",
+                        ssl_verify = false
+                    },
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize configuration
+                {
+                    endpoint = {
+                        uri = "http://127.0.0.1:9200",
+                        index = "services"
+                    }
+                },
+                -- property "uri" is required
+                {
+                    endpoint = {
+                        index = "services",
+                    }
+                },
+                -- property "index" is required
+                {
+                    endpoint = {
+                        uri = "http://127.0.0.1:9200",
+                    }
+                },
+                -- property "uri" validation failed
+                {
+                    endpoint = {
+                        uri = "127.0.0.1:9200",
+                        index = "services"
+                    }
+                }
+            }
+
+            local plugin = require("apisix.plugins.elasticsearch-logging")
+            for i = 1, #configs do
+                ok, err = plugin.check_schema(configs[i])
+                if err then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body_like
+passed
+passed
+property "endpoint" validation failed: property "uri" is required
+property "endpoint" validation failed: property "index" is required
+property "endpoint" validation failed: property "uri" validation failed.*
+
+
+
+=== TEST 2: set route
+--- 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-logging"] = {
+                        endpoint = {
+                            uri = "http://127.0.0.1:9200",
+                            index = "services"
+                        },
+                        batch_max_size = 1,
+                        inactive_timeout = 1
+                    }
+                }
+            })
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: test route (success write)
+--- request
+GET /hello
+--- wait: 2
+--- response_body
+hello world
+
+
+
+=== TEST 4: set route (auth)
+--- 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-logging"] = {
+                        endpoint = {
+                            uri = "http://127.0.0.1:9201",
+                            index = "services",
+                            username = "elastic",
+                            password = "123456"
+                        },
+                        batch_max_size = 1,
+                        inactive_timeout = 1
+                    }
+                }
+            })
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 5: test route (auth success)
+--- request
+GET /hello
+--- wait: 2
+--- response_body

Review Comment:
   We should check the data sent to the elasticsearch, via injecting like: https://github.com/apache/apisix/pull/7593#issuecomment-1210208755



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org