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

updates the consumer loop to put messages in the s3 bucket #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions gcn_monitor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def host_port(host_port_str):
show_default=True,
help="Log level",
)
def main(prometheus, loglevel):
@click.option("--bucketarn", help="Bucket ARN")
def main(prometheus, loglevel, bucketarn):
"""Monitor connectivity of a Kafka client.

Specify the Kafka client configuration in environment variables using the
Expand All @@ -60,4 +61,4 @@ def main(prometheus, loglevel):
)
log.info("Prometheus listening on %s", prometheus.netloc)

kafka.run()
kafka.run(bucketarn)
74 changes: 73 additions & 1 deletion gcn_monitor/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

import json
import logging
import re

import boto3
import gcn_kafka

from . import metrics

log = logging.getLogger(__name__)
s3_client = boto3.client("s3")


def stats_cb(data):
Expand All @@ -23,7 +26,70 @@ def stats_cb(data):
metrics.broker_state.labels(broker["name"]).state(broker["state"])


def run():
def sanitize_topic(topic):
"""Replaces invalid characters in the topic string so it can be used as a file name

Args:
topic (string): Kafka message topic

Returns:
safe_name: A formatted version of the topic string with underscores(`_`) in place of illegal characters
"""
invalid_chars = r'[\/:*?"<>|]'
safe_name = re.sub(invalid_chars, "_", topic)
return safe_name.strip()


def parse_into_s3_object(message):
"""Parses a Kafka message into a key and body.

Args:
message (Message): Any Kafka message

Returns:
key (string): The Key for S3's put_object method, formatted as `topics/<topic>/<encodedPartition>/<topic>+<kafkaPartition>+<startOffset>.<format>` defined at https://docs.confluent.io/kafka-connectors/s3-sink/current/overview.html#s3-object-names

body (dict): The Kafka message converted to a dict to be JSON serializable to include all fields available, such as:
- error
- headers
- key
- latency
- leader_epoch
- offset
- partition
- timestamp
- topic
- value

"""
topic = sanitize_topic(message.topic())
offset = message.offset()
partition = message.partition()
key = f"topics/{topic}/partition={partition}/{topic}+{partition}+{offset}.json"
properties = [
method_name
for method_name in dir(message)
if not method_name.startswith("__") and not method_name.startswith("set_")
]
return key, dict(
[
(
messageKey,
(
getattr(message, messageKey)().decode()
if isinstance(attr_value := getattr(message, messageKey)(), bytes)
and callable(getattr(message, messageKey))
else attr_value()
if callable(attr_value)
else attr_value
),
)
for messageKey in properties
]
)


def run(bucketarn):
log.info("Creating consumer")
config = gcn_kafka.config_from_env()
config["stats_cb"] = stats_cb
Expand All @@ -38,6 +104,12 @@ def run():
while True:
for message in consumer.consume(timeout=1):
topic = message.topic()
key, body = parse_into_s3_object(message)
s3_client.put_object(
Bucket=bucketarn.replace("arn:aws:s3:::", ""),
Key=key,
Body=body,
)
if error := message.error():
log.error("topic %s: got error %s", topic, error)
else:
Expand Down
112 changes: 111 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ click = "^8.1.7"
gcn-kafka = "^0.3.3"
python = "^3.9"
prometheus-client = "^0.20.0"
boto3 = "^1.35.7"

[tool.poetry.scripts]
gcn-monitor = "gcn_monitor.cli:main"
Expand Down
Loading