Skip to content

Commit 4990927

Browse files
feat: allow to use secrets in clickhouse-logger plugin (#12951)
1 parent 0458f89 commit 4990927

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

apisix/plugins/clickhouse-logger.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
-- See the License for the specific language governing permissions and
1515
-- limitations under the License.
1616
--
17+
18+
local fetch_secrets = require("apisix.secret").fetch_secrets
1719
local bp_manager_mod = require("apisix.utils.batch-processor-manager")
1820
local log_util = require("apisix.utils.log-util")
1921
local plugin = require("apisix.plugin")
@@ -105,6 +107,8 @@ end
105107

106108

107109
local function send_http_data(conf, log_message)
110+
conf = fetch_secrets(conf, true)
111+
108112
local err_msg
109113
local res = true
110114
local selected_endpoint_addr

docs/en/latest/plugins/clickhouse-logger.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ The `clickhouse-logger` Plugin is used to push logs to [ClickHouse](https://clic
5454

5555
NOTE: `encrypt_fields = {"password"}` is also defined in the schema, which means that the field will be stored encrypted in etcd. See [encrypted storage fields](../plugin-develop.md#encrypted-storage-fields).
5656

57+
NOTE: In addition, you can use Environment Variables or APISIX secret to store and reference plugin attributes. APISIX currently supports storing secrets in two ways - [Environment Variables and HashiCorp Vault](../terminology/secret.md).
58+
5759
This Plugin supports using batch processors to aggregate and process entries (logs/data) in a batch. This avoids the need for frequently submitting the data. The batch processor submits data every `5` seconds or when the data in the queue reaches `1000`. See [Batch Processor](../batch-processor.md#configuration) for more information or setting your custom configuration.
5860

5961
### Example of default log format

docs/zh/latest/plugins/clickhouse-logger.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ description: 本文介绍了 API 网关 Apache APISIX 如何使用 clickhouse-lo
5252

5353
注意:schema 中还定义了 `encrypt_fields = {"password"}`,这意味着该字段将会被加密存储在 etcd 中。具体参考 [加密存储字段](../plugin-develop.md#加密存储字段)
5454

55+
此外:你可以使用环境变量或者 APISIX secret 来存放和引用插件配置,APISIX 当前支持通过两种方式配置 secrets - [Environment Variables and HashiCorp Vault](../terminology/secret.md)
56+
5557
该插件支持使用批处理器来聚合并批量处理条目(日志/数据)。这样可以避免插件频繁地提交数据,默认情况下批处理器每 `5` 秒钟或队列中的数据达到 `1000` 条时提交数据,如需了解批处理器相关参数设置,请参考 [Batch-Processor](../batch-processor.md#配置)
5658

5759
### 默认日志格式示例

t/plugin/clickhouse-logger3.t

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
use t::APISIX 'no_plan';
19+
20+
log_level("info");
21+
repeat_each(1);
22+
no_long_string();
23+
no_root_location();
24+
25+
add_block_preprocessor(sub {
26+
my ($block) = @_;
27+
28+
if (!defined $block->request) {
29+
$block->set_value("request", "GET /t");
30+
}
31+
});
32+
33+
BEGIN {
34+
$ENV{CLICK_HOUSE_USER} = "default";
35+
}
36+
37+
run_tests();
38+
39+
__DATA__
40+
41+
=== TEST 1: pass clickhouse user via environment variable
42+
--- config
43+
location /t {
44+
content_by_lua_block {
45+
local t = require("lib.test_admin").test
46+
local code, body = t('/apisix/admin/routes/1',
47+
ngx.HTTP_PUT,
48+
[[{
49+
"plugins": {
50+
"clickhouse-logger": {
51+
"user": "$ENV://CLICK_HOUSE_USER",
52+
"password": "",
53+
"database": "default",
54+
"logtable": "test",
55+
"endpoint_addr": "http://127.0.0.1:8123"
56+
}
57+
},
58+
"upstream": {
59+
"nodes": {
60+
"127.0.0.1:1982": 1
61+
},
62+
"type": "roundrobin"
63+
},
64+
"uri": "/opentracing"
65+
}]]
66+
)
67+
68+
if code >= 300 then
69+
ngx.status = code
70+
end
71+
ngx.say(body)
72+
}
73+
}
74+
--- response_body
75+
passed
76+
77+
78+
79+
=== TEST 2: hit route
80+
--- request
81+
GET /opentracing
82+
--- error_code: 200
83+
--- wait: 5
84+
85+
86+
87+
=== TEST 3: get log
88+
--- exec
89+
echo "select * from default.test" | curl 'http://localhost:8123/' --data-binary @-
90+
--- response_body_like
91+
.*127.0.0.1.*1.*

0 commit comments

Comments
 (0)