You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "karthi5634 (via GitHub)" <gi...@apache.org> on 2025/07/01 19:29:45 UTC
[I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
karthi5634 opened a new issue, #12394:
URL: https://github.com/apache/apisix/issues/12394
### Description
I have created custom Kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers. This issue I am facing only with Kafka custom plugin, PFA lua file for kafka-json-producer plugin.
Kafka brokers are properly connecting with kafka-logger inbuilt apisix kafka plugin but getting issue only with custom kafka plugin.
Kindly let me know what I am missing. I followed github copilot suggestions and made changes but still getting same issue.

### Environment
- APISIX version (run `apisix version`):
- Operating system (run `uname -a`):
- OpenResty / Nginx version (run `openresty -V` or `nginx -V`):
- etcd version, if relevant (run `curl http://127.0.0.1:9090/v1/server_info`):
- APISIX Dashboard version, if relevant:
- Plugin runner version, for issues related to plugin runners:
- LuaRocks version, for installation issues (run `luarocks --version`):
--
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-unsubscribe@apisix.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3051581474
> [kafka-json-producer.txt](https://github.com/user-attachments/files/21121655/kafka-json-producer.txt)
>
> hi [@Baoyuantop](https://github.com/Baoyuantop) I was not able to upload .lua extension files. PFA updated custom kafka plugin code and let me know. It is matching with kafka-logger plugin code.
Just pasting the code here for easy viewing:
```lua
local core = require("apisix.core")
local producer = require("resty.kafka.producer")
local schema = {
type = "object",
properties = {
broker_list = {
type = "object",
minProperties = 1
},
topic = { type = "string" },
producer_type = { type = "string", enum = { "sync", "async" } }
},
required = { "broker_list", "topic" }
}
local plugin_name = "kafka-json-producer"
local _M = {
version = 0.8,
priority = 10,
name = plugin_name,
schema = schema,
}
function _M.access(conf, ctx)
local raw_body = core.request.get_body()
if not raw_body then
core.log.error("❌ No request body found")
return 400, { message = "Missing request body" }
end
local kafka_producer = producer:new(conf.broker_list, {
producer_type = conf.producer_type or "sync",
required_acks = 1,
request_timeout = 2000
})
-- Manually fetch and inject metadata if needed
local metadata, err = kafka_producer.client:fetch_metadata(conf.topic)
if err then
core.log.error("❌ Failed to fetch Kafka metadata: ", err)
else
core.log.warn("📦 Raw Kafka metadata: ", core.json.encode(metadata))
-- Force inject brokers from metadata
if metadata and metadata.brokers then
for _, broker in ipairs(metadata.brokers) do
local key = broker.host .. ":" .. broker.port
kafka_producer.client.brokers[key] = {
host = broker.host,
port = broker.port,
socket_config = {},
version = kafka_producer.client.version
}
end
end
-- Confirm what's now stored in client
core.log.warn("💡 Injected Kafka brokers: ", core.json.encode(kafka_producer.client.brokers))
end
-- fallback to partition 0
local ok, send_err = kafka_producer:send({
topic = conf.topic,
partition_id = 0
}, raw_body)
if not ok then
core.log.error("❌ Kafka send failed: ", send_err)
return 500, { message = "Kafka send failed", error = send_err }
end
core.log.info("✅ Kafka message sent to topic: ", conf.topic)
return 200, { message = "Kafka send success" }
end
return _M
```
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3026109828
Hi @karthi5634, can you share why the kafka logger plugin does not meet your needs and make you create a custom plugin? Also, if I can't see your custom plugin code, I may not be able to find your problem.
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3116913265
Hi @karthi5634, sorry for taking so long to reply. Just wanted to ask, why not develop based on kafka-logger plugin? Then you don't need to deal with the connection part, just the data format conversion
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop closed issue #12394: help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers
URL: https://github.com/apache/apisix/issues/12394
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "karthi5634 (via GitHub)" <gi...@apache.org>.
karthi5634 commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3045029733
hi @Baoyuantop
Did you get a chance to look into the above issue?
Let me know your thoughts
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "karthi5634 (via GitHub)" <gi...@apache.org>.
karthi5634 commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3048730172
[kafka-json-producer.txt](https://github.com/user-attachments/files/21121655/kafka-json-producer.txt)
hi @Baoyuantop
I was not able to upload .lua extension files.
PFA updated custom kafka plugin code and let me know.
It is matching with kafka-logger plugin code.
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "karthi5634 (via GitHub)" <gi...@apache.org>.
karthi5634 commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3026157896
This is the issue I am facing while using Kafka-logger and as per your suggestion I have created custom kafka apisix plugin.
Below is the issue I have raised for kafka-logger where JSON data is not processing properly.
Please refer:
https://github.com/apache/apisix/issues/12201
**Custom Kafka Plugin code :**
local core = require("apisix.core")
local producer = require("resty.kafka.producer")
local schema = {
type = "object",
properties = {
broker_list = {
type = "object",
minProperties = 1
},
topic = { type = "string" },
producer_type = { type = "string", enum = { "sync", "async" } }
},
required = { "broker_list", "topic" }
}
local plugin_name = "kafka-json-producer"
local _M = {
version = 0.8,
priority = 10,
name = plugin_name,
schema = schema,
}
function _M.access(conf, ctx)
local raw_body = core.request.get_body()
if not raw_body then
core.log.error("❌ No request body found")
return 400, { message = "Missing request body" }
end
local kafka_producer = producer:new(conf.broker_list, {
producer_type = conf.producer_type or "sync",
required_acks = 1,
request_timeout = 2000
})
-- Manually fetch and inject metadata if needed
local metadata, err = kafka_producer.client:fetch_metadata(conf.topic)
if err then
core.log.error("❌ Failed to fetch Kafka metadata: ", err)
else
core.log.warn("📦 Raw Kafka metadata: ", core.json.encode(metadata))
-- Force inject brokers from metadata
if metadata and metadata.brokers then
for _, broker in ipairs(metadata.brokers) do
local key = broker.host .. ":" .. broker.port
kafka_producer.client.brokers[key] = {
host = broker.host,
port = broker.port,
socket_config = {},
version = kafka_producer.client.version
}
end
end
-- Confirm what's now stored in client
core.log.warn("💡 Injected Kafka brokers: ", core.json.encode(kafka_producer.client.brokers))
end
-- fallback to partition 0
local ok, send_err = kafka_producer:send({
topic = conf.topic,
partition_id = 0
}, raw_body)
if not ok then
core.log.error("❌ Kafka send failed: ", send_err)
return 500, { message = "Kafka send failed", error = send_err }
end
core.log.info("✅ Kafka message sent to topic: ", conf.topic)
return 200, { message = "Kafka send success" }
end
return _M
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3047782031
The plugin code is not well formatted and not easy to read. I think you can modify it based on the code of kafka-logger.
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "karthi5634 (via GitHub)" <gi...@apache.org>.
karthi5634 commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3052612909
hi @Baoyuantop
Did you get a chance to review above code?
Any issue with plugin CODE?
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3520250798
If there is still a problem, please open it again.
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] help request: I have created custom kafka plugin to send proper JSON data to Kafka consumer from Apisix API but API is not connecting to Kafka brokers [apisix]
Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12394:
URL: https://github.com/apache/apisix/issues/12394#issuecomment-3173724535
Hi @karthi5634, any updates?
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org