-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[WIP] Kafka Logger. #1260
Closed
Closed
[WIP] Kafka Logger. #1260
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d9ff1bf
WIP Basic Version of Kafka Logger.
3e445f1
Merge branch 'master' into patch9
2e8c9b7
fix remove redis run.
4e9be20
add multiple broker support.
d02056f
Merge branch 'master' into patch9
d37a0fc
fix.
b2b28c5
Merge remote-tracking branch 'origin/patch9' into patch9
edac18b
fix.
c875a0c
fix.
a4d07fd
fix.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,7 @@ plugins: # plugin list | |
- proxy-cache | ||
- tcp-logger | ||
- proxy-mirror | ||
- kafka-logger | ||
|
||
stream_plugins: | ||
- mqtt-proxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
-- | ||
-- 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 core = require("apisix.core") | ||
local log_util = require("apisix.utils.log-util") | ||
local producer = require ("resty.kafka.producer") | ||
local pairs = pairs | ||
local type = type | ||
local table = table | ||
|
||
local plugin_name = "kafka-logger" | ||
local ngx = ngx | ||
|
||
local timer_at = ngx.timer.at | ||
|
||
local schema = { | ||
type = "object", | ||
properties = { | ||
broker_list = { | ||
type = "object" | ||
}, | ||
timeout = { -- timeout in milliseconds | ||
type = "integer", minimum = 1, default= 2000 | ||
}, | ||
kafka_topic = {type = "string"}, | ||
async = {type = "boolean", default = false}, | ||
key = {type = "string"}, | ||
max_retry = {type = "integer", minimum = 0 , default = 3}, | ||
}, | ||
required = {"broker_list", "kafka_topic", "key"} | ||
} | ||
|
||
local _M = { | ||
version = 0.1, | ||
priority = 403, | ||
name = plugin_name, | ||
schema = schema, | ||
} | ||
|
||
function _M.check_schema(conf) | ||
return core.schema.check(schema, conf) | ||
end | ||
|
||
local function log(premature, conf, log_message) | ||
if premature then | ||
return | ||
end | ||
|
||
if core.table.nkeys(conf.broker_list) == 0 then | ||
core.log.error("failed to identify the broker specified") | ||
end | ||
|
||
local broker_list = {} | ||
local broker_config = {} | ||
|
||
for host, port in pairs(conf.broker_list) do | ||
if type(host) == 'string' | ||
and type(port) == 'number' then | ||
|
||
local broker = { | ||
host = host, port = port | ||
} | ||
table.insert(broker_list,broker) | ||
end | ||
end | ||
|
||
broker_config["request_timeout"] = conf.timeout | ||
broker_config["max_retry"] = conf.max_retry | ||
|
||
--Async producers will queue logs and push them when the buffer exceeds. | ||
if conf.async then | ||
broker_config["producer_type"] = "async" | ||
end | ||
|
||
local prod, err = producer:new(broker_list) | ||
if err then | ||
core.log.error("failed to identify the broker specified", err) | ||
|
||
return | ||
end | ||
|
||
local ok, err = prod:send(conf.kafka_topic, conf.key, log_message) | ||
if not ok then | ||
core.log.error("failed to send data to Kafka topic", err) | ||
end | ||
|
||
end | ||
|
||
function _M.log(conf) | ||
return timer_at(0, log, conf, core.json.encode(log_util.get_full_log(ngx))) | ||
end | ||
|
||
return _M | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
# | ||
# 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(); | ||
run_tests; | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: sanity | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local plugin = require("apisix.plugins.kafka-logger") | ||
local ok, err = plugin.check_schema({ | ||
kafka_topic = "test", | ||
key = "key1", | ||
broker_list = { | ||
["127.0.0.1"] = 3 | ||
} | ||
}) | ||
if not ok then | ||
ngx.say(err) | ||
end | ||
|
||
ngx.say("done") | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
done | ||
--- no_error_log | ||
[error] | ||
|
||
|
||
=== TEST 2: missing broker list | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local plugin = require("apisix.plugins.kafka-logger") | ||
local ok, err = plugin.check_schema({kafka_topic = "test", key= "key1"}) | ||
if not ok then | ||
ngx.say(err) | ||
end | ||
ngx.say("done") | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
property "broker_list" is required | ||
done | ||
--- no_error_log | ||
[error] | ||
|
||
=== TEST 3: wrong type of string | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local plugin = require("apisix.plugins.kafka-logger") | ||
local ok, err = plugin.check_schema({ | ||
broker_list = { | ||
["127.0.0.1"] = 3000 | ||
}, | ||
timeout = "10", | ||
kafka_topic ="test", | ||
key= "key1" | ||
}) | ||
if not ok then | ||
ngx.say(err) | ||
end | ||
ngx.say("done") | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
property "timeout" validation failed: wrong type: expected integer, got string | ||
done | ||
--- no_error_log | ||
[error] | ||
|
||
=== TEST 4: add plugin | ||
--- 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, | ||
[[{ | ||
"plugins": { | ||
"kafka-logger": { | ||
"broker_list" : | ||
{ | ||
"127.0.0.1":9092 | ||
}, | ||
"kafka_topic" : "test2", | ||
"key" : "key1" | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]], | ||
[[{ | ||
"node": { | ||
"value": { | ||
"plugins": { | ||
"kafka-logger": { | ||
"broker_list" : | ||
{ | ||
"127.0.0.1":9092 | ||
}, | ||
"kafka_topic" : "test2", | ||
"key" : "key1" | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}, | ||
"key": "/apisix/routes/1" | ||
}, | ||
"action": "set" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
passed | ||
--- no_error_log | ||
[error] | ||
|
||
=== TEST 5: access | ||
--- request | ||
GET /hello | ||
--- response_body | ||
hello world | ||
--- no_error_log | ||
[error] | ||
--- wait: 0.2 | ||
|
||
=== TEST 6: error log | ||
--- 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, | ||
[[{ | ||
"plugins": { | ||
"kafka-logger": { | ||
"broker_list" : | ||
{ | ||
"127.0.0.1":9092, | ||
"127.0.0.1":9093 | ||
}, | ||
"kafka_topic" : "test2", | ||
"key" : "key1" | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]], | ||
[[{ | ||
"node": { | ||
"value": { | ||
"plugins": { | ||
"kafka-logger": { | ||
"broker_list" : | ||
{ | ||
"127.0.0.1":9092, | ||
"127.0.0.1":9093 | ||
}, | ||
"kafka_topic" : "test2", | ||
"key" : "key1" | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}, | ||
"key": "/apisix/routes/1" | ||
}, | ||
"action": "set" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
local http = require "resty.http" | ||
local httpc = http.new() | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" | ||
local res, err = httpc:request_uri(uri, {method = "GET"}) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- error_log | ||
failed to send data to Kafka topic | ||
[error] | ||
--- wait: 0.2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should install dependencies in specific scripts, eg: https://github.com/apache/incubator-apisix/blob/master/.travis/linux_openresty_runner.sh#L35
It may be a little between different os, eg: Linux and OSX.