|
| 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 | +local core = require("apisix.core") |
| 18 | +local log_util = require("apisix.utils.log-util") |
| 19 | +local logger_socket = require("resty.logger.socket") |
| 20 | +local plugin_name = "syslog" |
| 21 | +local ngx = ngx |
| 22 | + |
| 23 | +local schema = { |
| 24 | + type = "object", |
| 25 | + properties = { |
| 26 | + host = {type = "string"}, |
| 27 | + port = {type = "integer"}, |
| 28 | + flush_limit = {type = "integer", minimum = 1, default = 4096}, |
| 29 | + drop_limit = {type = "integer", default = 1048576}, |
| 30 | + timeout = {type = "integer", minimum = 1, default = 3}, |
| 31 | + sock_type = {type = "string", default = "tcp"}, |
| 32 | + max_retry_times = {type = "integer", minimum = 1, default = 3}, |
| 33 | + retry_interval = {type = "integer", minimum = 10, default = 100}, |
| 34 | + pool_size = {type = "integer", minimum = 5, default = 5}, |
| 35 | + tls = {type = "boolean", default = false}, |
| 36 | + }, |
| 37 | + required = {"host", "port"} |
| 38 | +} |
| 39 | + |
| 40 | +local lrucache = core.lrucache.new({ |
| 41 | + ttl = 300, count = 512 |
| 42 | +}) |
| 43 | + |
| 44 | +local _M = { |
| 45 | + version = 0.1, |
| 46 | + priority = 401, |
| 47 | + name = plugin_name, |
| 48 | + schema = schema, |
| 49 | +} |
| 50 | + |
| 51 | +function _M.check_schema(conf) |
| 52 | + return core.schema.check(schema, conf) |
| 53 | +end |
| 54 | + |
| 55 | +function _M.flush_syslog(logger) |
| 56 | + local ok, err = logger:flush(logger) |
| 57 | + if not ok then |
| 58 | + core.log.error("failed to flush message:", err) |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +-- log phase in APISIX |
| 63 | +function _M.log(conf) |
| 64 | + local entry = log_util.get_full_log(ngx) |
| 65 | + |
| 66 | + if not entry.route_id then |
| 67 | + core.log.error("failed to obtain the route id for sys logger") |
| 68 | + return |
| 69 | + end |
| 70 | + |
| 71 | + -- fetch api_ctx |
| 72 | + local api_ctx = ngx.ctx.api_ctx |
| 73 | + if not api_ctx then |
| 74 | + core.log.error("invalid api_ctx cannot proceed with sys logger plugin") |
| 75 | + return core.response.exit(500) |
| 76 | + end |
| 77 | + |
| 78 | + -- fetch it from lrucache |
| 79 | + local logger, err = lrucache(api_ctx.conf_type .. "#" .. api_ctx.conf_id, api_ctx.conf_version, |
| 80 | + logger_socket.new, logger_socket, { |
| 81 | + host = conf.host, |
| 82 | + port = conf.port, |
| 83 | + flush_limit = conf.flush_limit, |
| 84 | + drop_limit = conf.drop_limit, |
| 85 | + timeout = conf.timeout, |
| 86 | + sock_type = conf.sock_type, |
| 87 | + max_retry_times = conf.max_retry_times, |
| 88 | + retry_interval = conf.retry_interval, |
| 89 | + pool_size = conf.pool_size, |
| 90 | + tls = conf.tls, |
| 91 | + }) |
| 92 | + |
| 93 | + if not logger then |
| 94 | + core.log.error("failed when initiating the sys logger processor", err) |
| 95 | + end |
| 96 | + |
| 97 | + -- reuse the logger object |
| 98 | + local ok, err = logger:log(core.json.encode(entry)) |
| 99 | + if not ok then |
| 100 | + core.log.error("failed to log message", err) |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +return _M |
0 commit comments