Skip to content
Closed
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
35 changes: 33 additions & 2 deletions fluent/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import socket
import threading
import time

import urllib2, urllib
import msgpack

import sys

_global_sender = None

Expand All @@ -22,6 +22,37 @@ def setup(tag, **kwargs):
def get_global_sender():
return _global_sender

class FluentHTTPSender(object):
def __init__(self, tag, host='localhost', port=9880, events_max=20):
tag_path = tag.replace('.', '/')
self.url = '%s:%i/%s'%(host, port, tag_path)
self.events = []
self.events_max = events_max
self.msgpack_packer = msgpack.Packer()

def emit(self, record):
if "time" not in record:
record["time"] = int(time.time())
self._buffer_or_send(time, record)

def _buffer_or_send(self, time, record):
self.events.append(record)
if len(self.events) > self.events_max:
self._send()

def _send(self):
request = urllib2.Request(self.url)
data = urllib.urlencode({"msgpack":self.msgpack_packer.pack(self.events)})
request.add_data(data)
response = urllib2.urlopen(request)
if response.getcode() == 200:
self.events = [] # flushing the buffer
else:
print('Failed to flush the buffer', sys.stderr)

def __del__(self):
self._send()


class FluentSender(object):
def __init__(self,
Expand Down
3 changes: 2 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from tests.test_event import *
from tests.test_handler import *
from tests.test_sender import *
from tests.test_unix_domain_socket_sender import *
# TODO: write a test for this
#from tests.test_unix_domain_socket_sender import *
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is most likely a forgotten file and I simply killed this __init__.py which is not useful for running unittests in MR #17.