[GitHub] [apisix] tzssangglass commented on a change in pull request #6215: feat: clickhouse logger

2022-02-07 Thread GitBox


tzssangglass commented on a change in pull request #6215:
URL: https://github.com/apache/apisix/pull/6215#discussion_r801216324



##
File path: t/plugin/clickhouse-logger.t
##
@@ -0,0 +1,223 @@
+#
+# 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();
+
+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");
+}
+
+my $http_config = $block->http_config // <<_EOC_;
+server {
+listen 10420;
+location /loggly/bulk/tok/tag/bulk {
+content_by_lua_block {
+ngx.req.read_body()
+local data = ngx.req.get_body_data()
+local headers = ngx.req.get_headers()
+ngx.log(ngx.ERR, "loggly body: ", data)
+ngx.log(ngx.ERR, "loggly tags: " .. 
require("toolkit.json").encode(headers["X-LOGGLY-TAG"]))
+ngx.say("ok")
+}
+}
+}
+_EOC_
+
+$block->set_value("http_config", $http_config);
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: Full configuration verification
+--- yaml_config
+plugins:
+  - clickhouse-logger
+--- config
+location /t {
+content_by_lua_block {
+local plugin = require("apisix.plugins.clickhouse-logger")
+local ok, err = plugin.check_schema({timeout = 3,
+ retry_delay = 1,
+ batch_max_size = 500,
+ user = "default",
+ password = "a",
+ database = "default",
+ logtable = "t",
+ endpoint_addr = 
"http://127.0.0.1:8123;,
+ max_retry_count = 1,
+ name = "clickhouse logger",
+ ssl_verify = false
+ })
+
+if not ok then
+ngx.say(err)
+else
+ngx.say("passed")
+end
+}
+}
+--- response_body
+passed
+
+
+
+=== TEST 2: Basic configuration verification
+--- yaml_config
+plugins:
+  - clickhouse-logger
+--- config
+location /t {
+content_by_lua_block {
+local plugin = require("apisix.plugins.clickhouse-logger")
+local ok, err = plugin.check_schema({user = "default",
+ password = "a",
+ database = "default",
+ logtable = "t",
+ endpoint_addr = 
"http://127.0.0.1:8123;
+ })
+
+if not ok then
+ngx.say(err)
+else
+ngx.say("passed")
+end
+}
+}
+--- response_body
+passed
+
+
+
+=== TEST 3: auth configure undefined
+--- yaml_config
+plugins:
+  - clickhouse-logger
+--- config
+location /t {
+content_by_lua_block {
+local plugin = require("apisix.plugins.clickhouse-logger")
+local ok, err = plugin.check_schema({user = "default",
+ password = "a",
+ database = "default",
+ logtable = "t"
+ })
+
+if not ok then
+ngx.say(err)
+else
+ngx.say("passed")
+end
+}
+}
+--- response_body
+property "endpoint_addr" is required
+
+
+
+=== TEST 4: add plugin on routes
+--- yaml_config
+plugins:
+  - clickhouse-logger
+--- config
+location /t {
+   

[GitHub] [apisix] tzssangglass commented on a change in pull request #6215: feat: clickhouse logger

2022-02-07 Thread GitBox


tzssangglass commented on a change in pull request #6215:
URL: https://github.com/apache/apisix/pull/6215#discussion_r801208708



##
File path: docs/zh/latest/plugins/clickhouse-logger.md
##
@@ -0,0 +1,146 @@
+---
+title: clickhouse-logger
+---
+
+
+
+## 目录
+
+- [**定义**](#定义)
+- [**属性列表**](#属性列表)
+- [**如何开启**](#如何开启)
+- [**测试插件**](#测试插件)
+- [**插件元数据设置**](#插件元数据设置)
+- [**禁用插件**](#禁用插件)
+
+## 定义
+
+`clickhouse-logger` 是一个插件,可将Log数据请求推送到clickhouse服务器。
+
+## 属性列表
+
+| 名称 | 类型| 必选项 | 默认值| 有效值  | 描述
 |
+|  | --- | -- | - | --- | 
 |
+| endpoint_addr| string  | 必须   |   | | `clickhouse` 
服务器的 endpoint。   |
+| database | string  | 必须   |   | | 使用的数据库。
|
+| logtable | string  | 必须   |   | | 写入的表名 。|
+| user | string  | 必须   |   | |clickhouse的用户。 |
+| password | string  | 必须   |   | |clickhouse的密码 。 
 |

Review comment:
   ```suggestion
   | user | string  | 必须   |   | | 
clickhouse的用户。 |
   | password | string  | 必须   |   | | 
clickhouse的密码 。  |
   ```




-- 
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-unsubscr...@apisix.apache.org

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




[GitHub] [apisix] tzssangglass commented on a change in pull request #6215: feat: clickhouse logger

2022-01-27 Thread GitBox


tzssangglass commented on a change in pull request #6215:
URL: https://github.com/apache/apisix/pull/6215#discussion_r794183325



##
File path: apisix/plugins/clickhouse-logger.lua
##
@@ -0,0 +1,179 @@
+--
+-- 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 bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+local log_util= require("apisix.utils.log-util")
+local core= require("apisix.core")
+local http= require("resty.http")
+local url = require("net.url")
+local plugin  = require("apisix.plugin")
+
+local ngx  = ngx
+local tostring = tostring
+
+local plugin_name = "clickhouse-logger"
+local batch_processor_manager = bp_manager_mod.new(plugin_name)
+
+local schema = {
+type = "object",
+properties = {
+endpoint_addr = core.schema.uri_def,
+user = {type = "string", default = ""},
+password = {type = "string", default = ""},
+database = {type = "string", default = ""},
+logtable = {type = "string", default = ""},
+timeout = {type = "integer", minimum = 1, default = 3},
+name = {type = "string", default = "clickhouse logger"},
+max_retry_count = {type = "integer", minimum = 0, default = 0},
+retry_delay = {type = "integer", minimum = 0, default = 1},
+batch_max_size = {type = "integer", minimum = 1, default = 100}
+},
+required = {"endpoint_addr", "user", "password", "database", "logtable"}
+}
+
+
+local metadata_schema = {
+type = "object",
+properties = {
+log_format = log_util.metadata_schema_log_format,
+},
+}
+
+
+local _M = {
+version = 0.1,
+priority = 398,
+name = plugin_name,
+schema = schema,
+metadata_schema = metadata_schema,
+}
+
+
+function _M.check_schema(conf, schema_type)
+if schema_type == core.schema.TYPE_METADATA then
+return core.schema.check(metadata_schema, conf)
+end
+return core.schema.check(schema, conf)
+end
+
+
+local function send_http_data(conf, log_message)
+local err_msg
+local res = true
+local url_decoded = url.parse(conf.endpoint_addr)
+local host = url_decoded.host
+local port = url_decoded.port
+
+core.log.info("sending a batch logs to ", conf.endpoint_addr)
+
+if ((not port) and url_decoded.scheme == "https") then
+port = 443
+elseif not port then
+port = 80
+end

Review comment:
   ```suggestion
   if not port then
   if url_decoded.scheme == "https" then
   port = 443
   else
   port = 80
   end
   end
   ```
   
   is better?

##
File path: apisix/plugins/clickhouse-logger.lua
##
@@ -0,0 +1,231 @@
+--
+-- 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 batch_processor = require("apisix.utils.batch-processor")
+local log_util= require("apisix.utils.log-util")
+local core= require("apisix.core")
+local http= require("resty.http")
+local url = require("net.url")
+local plugin  = require("apisix.plugin")
+
+local ngx  = ngx
+local tostring = tostring
+local ipairs   = ipairs
+local timer_at = ngx.timer.at
+
+local plugin_name = "clickhouse-logger"
+local stale_timer_running = false
+local buffers = {}
+
+local schema = {
+type = "object",
+properties = {
+endpoint_addr = core.schema.uri_def,
+user = {type =