-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- 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 | ||
import kafka | ||
|
||
# Import napalm-logs pkgs | ||
from napalm_logs.exceptions import NapalmLogsException | ||
from napalm_logs.transport.base import TransportBase | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class KafkaTransport(TransportBase): | ||
''' | ||
Kafka transport class. | ||
''' | ||
def __init__(self, bootstrap_servers, topic): | ||
self.bootstrap_servers = bootstrap_servers | ||
self.topic = 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() |