forked from rhettg/BlueOx
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Kafka shim #11
Open
alecraso
wants to merge
17
commits into
master
Choose a base branch
from
aaron/kafka-shim
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kafka shim #11
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5621377
Initial work on kafka shim
c255045
Add kafka recorder tests
420b35d
Squashed commit of the following:
fc753b7
Update recorder override functionality
18e43c7
Fix sundry syntax/formatting
080a128
Clean up more syntax, flip constant names
7317a36
Add kafka recorder defaults to contrib
870c56d
jk it's a pycernan shim now
3468293
Initial work on kafka shim
cd6782b
Add kafka recorder tests
eb57ad5
Update recorder override functionality
1e42922
Fix sundry syntax/formatting
8f5a69a
Clean up more syntax, flip constant names
9979383
Add kafka recorder defaults to contrib
ff06ac9
jk it's a pycernan shim now
344b2d1
Add changes and bump version
6123288
Add pycernan recorder
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Empty file.
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,101 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
blueox.kafka | ||
~~~~~~~~ | ||
|
||
This module provides the interface into Kafka | ||
|
||
:copyright: (c) 2018 by Aaron Biller?? | ||
:license: ISC, see LICENSE for more details. | ||
|
||
""" | ||
import atexit | ||
import logging | ||
import msgpack | ||
|
||
from kafka import KafkaProducer | ||
|
||
from .. import ports | ||
from .. import utils | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
# If we have pending outgoing messages, this is how long we'll wait after | ||
# being told to exit. | ||
LINGER_SHUTDOWN_MSECS = 2000 | ||
|
||
# Producer can be shared between threads | ||
_kafka_producer = None | ||
|
||
|
||
def init(host=None): | ||
"""Initialize the global kafka producer | ||
|
||
Supports a host arg with an overriding kafka host string | ||
in the format 'hostname:port' | ||
""" | ||
global _kafka_producer | ||
|
||
host = ports.default_kafka_host(host) | ||
|
||
_kafka_producer = KafkaProducer(bootstrap_servers=host) | ||
|
||
|
||
def _serialize_context(context): | ||
context_dict = context.to_dict() | ||
for key in ('host', 'type'): | ||
if len(context_dict.get(key, "")) > 64: | ||
raise ValueError("Value too long: %r" % key) | ||
|
||
context_dict = { | ||
k: v.encode('utf-8') if isinstance(v, unicode) | ||
else v for k, v in context_dict.items() | ||
} | ||
|
||
try: | ||
context_data = msgpack.packb(context_dict) | ||
except TypeError: | ||
try: | ||
# If we fail to serialize our context, we can try again with an | ||
# enhanced packer (it's slower though) | ||
context_data = msgpack.packb(context_dict, | ||
default=utils.msgpack_encode_default) | ||
except TypeError: | ||
log.exception("Serialization failure (not fatal, dropping data)") | ||
|
||
# One last try after dropping the body | ||
context_dict['body'] = None | ||
context_data = msgpack.packb(context_dict) | ||
|
||
return context_data | ||
|
||
|
||
def send(context): | ||
global _kafka_producer | ||
|
||
try: | ||
context_data = _serialize_context(context) | ||
except Exception: | ||
log.exception("Failed to serialize context") | ||
return | ||
|
||
if _kafka_producer: | ||
try: | ||
log.debug("Sending msg") | ||
_kafka_producer.send('events', context_data) | ||
except Exception: | ||
log.exception("Failed during publish to kafka.") | ||
else: | ||
log.info("Skipping sending event %s", context.name) | ||
|
||
|
||
def close(): | ||
global _kafka_producer | ||
|
||
if _kafka_producer: | ||
_kafka_producer.flush() | ||
_kafka_producer.close(timeout=LINGER_SHUTDOWN_MSECS) | ||
_kafka_producer = None | ||
|
||
|
||
atexit.register(close) |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ pyflakes | |
tornado==3.2 | ||
boto | ||
yapf | ||
kafka-python |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not gonna lie, not a fan of magical overrides via env vars. Why not just have it come in as a recorder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, fair. I was hoping to avoid a bunch of edits throughout postal changing how it's called, but it would be simple enough to call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh huh, wait, if thats the case no go back to your original way lol