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

Support headers #208

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Changes from 1 commit
Commits
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
19 changes: 18 additions & 1 deletion extensions/eda/plugins/event_source/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Default to PLAIN.
sasl_plain_username: Username for SASL PLAIN authentication
sasl_plain_password: Password for SASL PLAIN authentication
with_headers: Include message headers. [True, False (default)]



Expand Down Expand Up @@ -64,6 +65,7 @@ async def main( # pylint: disable=R0914
offset = args.get("offset", "latest")
encoding = args.get("encoding", "utf-8")
security_protocol = args.get("security_protocol", "PLAINTEXT")
with_headers = args.get("with_headers", False)

if offset not in ("latest", "earliest"):
msg = f"Invalid offset option: {offset}"
Expand Down Expand Up @@ -110,6 +112,7 @@ async def main( # pylint: disable=R0914

try:
async for msg in kafka_consumer:
event = {}
data = None
try:
value = msg.value.decode(encoding)
Expand All @@ -119,8 +122,22 @@ async def main( # pylint: disable=R0914
except UnicodeError:
logger.exception("Unicode Error")

if with_headers:
headers = None
try:
headers_dict = {header[0]: header[1].decode(encoding) for header in msg.headers}
headers_json = json.dumps(headers_dict)
headers = json.loads(headers_json)
clyang82 marked this conversation as resolved.
Show resolved Hide resolved
except UnicodeError:
logger.exception("Unicode Error")

if headers:
event["headers"] = headers

if data:
await queue.put({"body": data})
event["body"] = data
await queue.put(event)

await asyncio.sleep(0)
finally:
logger.info("Stopping kafka consumer")
Expand Down