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

Added Kafka transport #68

Merged
merged 1 commit into from
May 2, 2017
Merged
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
1 change: 1 addition & 0 deletions napalm_logs/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
LOG_FORMAT = '%(asctime)s,%(msecs)03.0f [%(name)-17s][%(levelname)-8s] %(message)s'
LOG_FILE = os.path.join(ROOT_DIR, 'var', 'log', 'napalm', 'logs')
LOG_FILE_CLI_OPTIONS = ('cli', 'screen')
KAFKA_TOPIC = "napalm-logs"

LOGGING_LEVEL = {
'debug': logging.DEBUG,
Expand Down
5 changes: 4 additions & 1 deletion napalm_logs/transport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# Import napalm-logs pkgs
from napalm_logs.transport.base import TransportBase
from napalm_logs.transport.zeromq import ZMQTransport
from napalm_logs.transport.kafka import KafkaTransport
from napalm_logs.transport.kafka import HAS_KAFKA
from napalm_logs.transport.cli import CLITransport
from napalm_logs.transport.log import LogTransport
# from napalm_logs.transport.kafka import KafkaTransport
Expand All @@ -20,12 +22,13 @@
'print': CLITransport,
'console': CLITransport,
'log': LogTransport,
# 'kafka': KafkaTransport,
# 'rmq': RabbitMQransport,
# 'rabbitmq': RabbitMQransport,
'*': ZMQTransport # default transport
}

if HAS_KAFKA:
TRANSPORT_LOOKUP['kafka'] = KafkaTransport

def get_transport(name):
'''
Expand Down
47 changes: 47 additions & 0 deletions napalm_logs/transport/kafka.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
'''
Kafka transport for napalm-logs.
'''
from __future__ import absolute_import
from __future__ import unicode_literals

# Import stdlib
import json
import logging

# Import third party libs
try:
import kafka
HAS_KAFKA = True
except ImportError as err:
HAS_KAFKA = False

# Import napalm-logs pkgs
from napalm_logs.exceptions import NapalmLogsException
from napalm_logs.transport.base import TransportBase
from napalm_logs.config import KAFKA_TOPIC

log = logging.getLogger(__name__)


class KafkaTransport(TransportBase):
'''
Kafka transport class.
'''
def __init__(self, addr, port):
self.bootstrap_servers = '{addr}:{port}'.format(addr=addr, port=port)
self.topic = KAFKA_TOPIC

def start(self):
try:
self.producer = kafka.KafkaProducer(bootstrap_servers=self.bootstrap_servers)
except kafka.errors.NoBrokersAvailable as err:
log.error(err, exc_info=True)
raise NapalmLogsException(err)

def publish(self, obj):
self.producer.send(self.topic, obj)

def stop(self):
if hasattr(self, 'producer'):
self.producer.close()