Skip to content

Commit 2c1127e

Browse files
jaygel179darinhoward
authored andcommitted
Added Unix Socket Domain Transport (#6)
Added Unix Socket Domain Transport
1 parent 6f368b3 commit 2c1127e

33 files changed

+2011
-130
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,6 @@ venv.bak/
107107

108108
# Intellij
109109
.idea
110+
111+
# Protobuf
112+
*.proto

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mock==2.0.0
2+
protobuf==3.9.1
23
pytest==4.3.0
34
pytest-cov==2.6.1
45
requests==2.21.0
6+
requests-unixsocket==0.2.0
57
retrying==1.3.3

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
keywords=['logging', 'stackify', 'exception'],
3333
classifiers=["Programming Language :: Python"],
3434
install_requires=[
35+
'protobuf>=3.9.1',
3536
'retrying>=1.2.3',
36-
'requests>=2.4.1'
37+
'requests>=2.4.1',
38+
'requests-unixsocket>=0.2.0'
3739
],
3840
test_suite='tests',
3941
tests_requires=[

stackify/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import inspect
88
import atexit
99

10-
from stackify.application import ApiConfiguration # noqa
1110
from stackify.constants import DEFAULT_LEVEL
1211
from stackify.handler import StackifyHandler
1312

stackify/application.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

stackify/constants.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
API_URL = 'https://api.stackify.com'
55
IDENTIFY_URL = '/Metrics/IdentifyApp'
66
LOG_SAVE_URL = '/Log/Save'
7+
8+
# using `%2F` instead of `/` as per package documentation
9+
DEFAULT_SOCKET_FILE = '%2Fusr%2Flocal%2Fstackify%2Fstackify.sock'
10+
SOCKET_URL = 'http+unix://' + DEFAULT_SOCKET_FILE
11+
SOCKET_LOG_URL = '/log'
12+
713
API_REQUEST_INTERVAL_IN_SEC = 30
814

915
MAX_BATCH = 100
@@ -19,3 +25,14 @@
1925
logging.NOTSET: 'NOTSET'
2026
}
2127
DEFAULT_LEVEL = logging.INFO
28+
29+
# this is used to separate builtin keys from user-specified keys
30+
RECORD_VARS = set(logging.LogRecord('', '', '', '', '', '', '', '').__dict__.keys())
31+
32+
# the "message" attribute is saved on the record object by a Formatter
33+
RECORD_VARS.add('message')
34+
RECORD_VARS.add('trans_id')
35+
RECORD_VARS.add('log_id')
36+
37+
TRANSPORT_TYPE_DEFAULT = 'default'
38+
TRANSPORT_TYPE_AGENT_SOCKET = 'agent_socket'

stackify/handler.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import logging
23
import atexit
34

@@ -11,13 +12,11 @@
1112
except ImportError: # pragma: no cover
1213
import queue
1314

14-
from stackify.application import get_configuration
1515
from stackify.constants import API_REQUEST_INTERVAL_IN_SEC
1616
from stackify.constants import MAX_BATCH
1717
from stackify.constants import QUEUE_SIZE
18-
from stackify.http import HTTPClient
19-
from stackify.log import LogMsg, LogMsgGroup
2018
from stackify.timer import RepeatedTimer
19+
from stackify.transport import Transport
2120

2221

2322
internal_logger = logging.getLogger(__name__)
@@ -56,6 +55,10 @@ def enqueue(self, record):
5655
self.queue.get_nowait()
5756
self.queue.put_nowait(record)
5857

58+
def prepare(self, record):
59+
record = copy.copy(record)
60+
return record
61+
5962

6063
class StackifyListener(QueueListener):
6164
'''
@@ -65,24 +68,15 @@ class StackifyListener(QueueListener):
6568
def __init__(self, queue_, max_batch=MAX_BATCH, config=None, **kwargs):
6669
super(StackifyListener, self).__init__(queue_)
6770

68-
if config is None:
69-
config = get_configuration(**kwargs)
70-
7171
self.max_batch = max_batch
7272
self.messages = []
73-
self.http = HTTPClient(config)
73+
self.transport = Transport(config, **kwargs)
7474
self.timer = RepeatedTimer(API_REQUEST_INTERVAL_IN_SEC, self.send_group)
7575

7676
self._started = False
7777

7878
def handle(self, record):
79-
if not self.http.identified:
80-
internal_logger.debug('Identifying application')
81-
self.http.identify_application()
82-
83-
msg = LogMsg()
84-
msg.from_record(record)
85-
self.messages.append(msg)
79+
self.messages.append(self.transport.create_message(record))
8680

8781
if len(self.messages) >= self.max_batch:
8882
self.send_group()
@@ -91,9 +85,9 @@ def send_group(self):
9185
if not self.messages:
9286
return
9387

94-
group = LogMsgGroup(self.messages)
88+
group_message = self.transport.create_group_message(self.messages)
9589
try:
96-
self.http.send_log_group(group)
90+
self.transport.send(group_message)
9791
except Exception:
9892
internal_logger.exception('Could not send {} log messages, discarding'.format(len(self.messages)))
9993
del self.messages[:]
@@ -103,8 +97,8 @@ def start(self):
10397

10498
if not self._started:
10599
super(StackifyListener, self).start()
106-
self.timer.start()
107100
self._started = True
101+
self.timer.start()
108102

109103
def stop(self):
110104
internal_logger.debug('Shutting down listener')

stackify/protos/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)