Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add elasticsearch-logger #7643

Merged
merged 57 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
6e1f2d4
add elasticsearch-logging base function
ccxhwmy Aug 10, 2022
446b174
add the support of option of username and password
ccxhwmy Aug 11, 2022
6dfc58c
add the support of elasticsearch authentication
ccxhwmy Aug 13, 2022
54cc283
add docs of elasticsearch plugin
ccxhwmy Aug 13, 2022
9916235
update docker compose file to add elasticsearch config
ccxhwmy Aug 13, 2022
0e96d5c
add test case
ccxhwmy Aug 13, 2022
00fe216
repair ci error
ccxhwmy Aug 13, 2022
d7e95c4
update test case
ccxhwmy Aug 14, 2022
f03e804
set elasticsearch index and type with configure
ccxhwmy Aug 14, 2022
ecbd983
Update apisix/plugins/elasticsearch-logging.lua
ccxhwmy Aug 15, 2022
5765063
Update apisix/plugins/elasticsearch-logging.lua
ccxhwmy Aug 15, 2022
2b8801c
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
1f7c530
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
219c4fa
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
acf8cac
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
35a5304
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
54ea755
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
354e89b
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
1b1474b
Update docs/en/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
cf163f7
Update docs/en/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
471dcad
Update docs/en/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
15f3dd7
Update docs/en/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
1b5fda7
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
2f2e017
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
c392327
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
454ff00
Update docs/en/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
78087d7
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
55c2ff1
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
2dc72a5
Update docs/zh/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
b98d4bd
Update docs/en/latest/plugins/elasticsearch-logging.md
ccxhwmy Aug 15, 2022
edcfa11
modify with suggestions
ccxhwmy Aug 17, 2022
9573140
pass test case
ccxhwmy Aug 17, 2022
a596e28
modify docs and test case
ccxhwmy Aug 18, 2022
c2000cb
format docs
ccxhwmy Aug 18, 2022
b7a306f
Update apisix/plugins/elasticsearch-logger.lua
ccxhwmy Aug 18, 2022
bc4bd75
add test case to check custom log
ccxhwmy Aug 20, 2022
2ce415d
modify docs
ccxhwmy Aug 20, 2022
bd2853b
modify docs
ccxhwmy Aug 20, 2022
9dfd2d5
not check username & password if auth is exist
ccxhwmy Aug 21, 2022
214e9a7
modify test case
ccxhwmy Aug 21, 2022
31e27f0
Update t/plugin/elasticsearch-logger.t
ccxhwmy Aug 22, 2022
b9059ab
update test case
ccxhwmy Aug 22, 2022
b859b90
Merge branch 'feat_elasticsearch_logging' of https://github.com/ccxhw…
ccxhwmy Aug 22, 2022
e7639ca
update docs
ccxhwmy Aug 22, 2022
45ca13a
add log metadata check
ccxhwmy Aug 22, 2022
cb6afd3
Merge branch 'master' into feat_elasticsearch_logging
ccxhwmy Aug 24, 2022
4b37d1b
update endpoint_addr check pattern
ccxhwmy Aug 24, 2022
cd96bc4
Update apisix/plugins/elasticsearch-logger.lua
ccxhwmy Aug 24, 2022
4c4859a
Update t/plugin/elasticsearch-logger.t
ccxhwmy Aug 24, 2022
1a56fd9
update test case
ccxhwmy Aug 24, 2022
902aa05
Update docs/zh/latest/plugins/elasticsearch-logger.md
ccxhwmy Aug 24, 2022
1693a2a
Update docs/zh/latest/plugins/elasticsearch-logger.md
ccxhwmy Aug 24, 2022
9914004
Update docs/zh/latest/plugins/elasticsearch-logger.md
ccxhwmy Aug 24, 2022
05e93e7
Update docs/zh/latest/plugins/elasticsearch-logger.md
ccxhwmy Aug 24, 2022
5d9028a
Update docs/en/latest/plugins/elasticsearch-logger.md
ccxhwmy Aug 24, 2022
5062c8d
remove endpoint_addr end with "/" judgement logic
ccxhwmy Aug 28, 2022
a5972f2
modify reviewer suggestion
ccxhwmy Aug 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions apisix/plugins/elasticsearch-logger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
--
-- 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 http = require("resty.http")
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 str_format = core.string.format

local plugin_name = "elasticsearch-logger"
local batch_processor_manager = bp_manager_mod.new(plugin_name)


local schema = {
type = "object",
properties = {
endpoint_addr = {
type = "string",
pattern = "[^/]$",
},
field = {
type = "object",
properties = {
index = { type = "string"},
type = { type = "string"}
},
required = {"index"}
},
auth = {
type = "object",
properties = {
username = {
type = "string",
minLength = 1
},
password = {
type = "string",
minLength = 1
},
},
required = {"username", "password"},
},
timeout = {
type = "integer",
minimum = 1,
default = 10
},
ssl_verify = {
type = "boolean",
default = true
}
},
required = { "endpoint_addr", "field" },
}


local metadata_schema = {
type = "object",
properties = {
log_format = log_util.metadata_schema_log_format,
},
}


local _M = {
version = 0.1,
priority = 413,
name = plugin_name,
schema = batch_processor_manager:wrap_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 get_logger_entry(conf, ctx)
local entry
local metadata = plugin.plugin_metadata(plugin_name)
core.log.info("metadata: ", core.json.delay_encode(metadata))
if metadata and metadata.value.log_format
and core.table.nkeys(metadata.value.log_format) > 0
then
entry = log_util.get_custom_format_log(ctx, metadata.value.log_format)
core.log.info("custom log format entry: ", core.json.delay_encode(entry))
else
entry = log_util.get_full_log(ngx, conf)
core.log.info("full log entry: ", core.json.delay_encode(entry))
end

return core.json.encode({
create = {
_index = conf.field.index,
_type = conf.field.type
}
}) .. "\n" ..
core.json.encode(entry) .. "\n"
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
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_addr .. "/_bulk"
local body = core.table.concat(entries, "")
local headers = {["Content-Type"] = "application/x-ndjson"}
if conf.auth then
local authorization = "Basic " .. ngx.encode_base64(
conf.auth.username .. ":" .. conf.auth.password
)
headers["Authorization"] = authorization
end

core.log.info("uri: ", uri, ", body: ", body)

httpc:set_timeout(conf.timeout * 1000)
local resp, err = httpc:request_uri(uri, {
ssl_verify = conf.ssl_verify,
method = "POST",
headers = headers,
body = body
})
if not resp then
return false, err
end

if resp.status ~= 200 then
return false, str_format("elasticsearch server returned status: %d, body: %s",
resp.status, resp.body or "")
end

return true
end


function _M.log(conf, ctx)
local entry = get_logger_entry(conf, ctx)

if batch_processor_manager:add_entry(conf, entry) then
return
end

local process = function(entries)
return send_to_elasticsearch(conf, entries)
end

batch_processor_manager:add_entry_to_new_processor(conf, entry, ctx, process)
end


return _M
27 changes: 27 additions & 0 deletions ci/pod/docker-compose.plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ services:
SPLUNK_HEC_TOKEN: "BD274822-96AA-4DA6-90EC-18940FB2414C"
SPLUNK_HEC_SSL: "False"

# Elasticsearch Logger Service
elasticsearch-noauth:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.1
restart: unless-stopped
ports:
- "9200:9200"
- "9300:9300"
environment:
ES_JAVA_OPTS: -Xms512m -Xmx512m
discovery.type: single-node
xpack.security.enabled: 'false'

elasticsearch-auth:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.1
restart: unless-stopped
ports:
- "9201:9201"
- "9301:9301"
environment:
ES_JAVA_OPTS: -Xms512m -Xmx512m
discovery.type: single-node
ELASTIC_USERNAME: elastic
ELASTIC_PASSWORD: 123456
http.port: 9201
transport.tcp.port: 9301
xpack.security.enabled: 'true'


networks:
apisix_net:
Expand Down
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ plugins: # plugin list (sorted by priority)
- public-api # priority: 501
- prometheus # priority: 500
- datadog # priority: 495
- elasticsearch-logger # priority: 413
- echo # priority: 412
- loggly # priority: 411
- http-logger # priority: 410
Expand Down
3 changes: 2 additions & 1 deletion docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
"plugins/google-cloud-logging",
"plugins/splunk-hec-logging",
"plugins/file-logger",
"plugins/loggly"
"plugins/loggly",
"plugins/elasticsearch-logger"
]
}
]
Expand Down
Loading