Skip to content

Commit 4f95356

Browse files
committed
Fix broken transport instantiation if callable is used
The transport option should be an importable module path or a callable (class or factory) of a suitable transport. But it can also be an already setup instance of a transport. Fix code and documentation to reflect this. Closes #42.
1 parent ff2877a commit 4f95356

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

docs/config.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Options for configuring the log handler
4747
a transport via Redis or the Beats protocol.
4848
If you pass a string, it should be a path to a
4949
class which can be imported.
50-
If you pass anything else, it should be an instance of a class
50+
If you pass anything else, it should be a callable or an instance of a class
5151
with a similar interface as `logstash_async.transport.TcpTransport`.
5252
Especially it should provide a `close()` and a `send()` method.
5353

logstash_async/handler.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,26 @@ def _setup_transport(self):
8585
if self._transport is not None:
8686
return
8787

88+
transport_args = dict(
89+
host=self._host,
90+
port=self._port,
91+
timeout=constants.SOCKET_TIMEOUT,
92+
ssl_enable=self._ssl_enable,
93+
ssl_verify=self._ssl_verify,
94+
keyfile=self._keyfile,
95+
certfile=self._certfile,
96+
ca_certs=self._ca_certs)
8897
if isinstance(self._transport_path, string_types):
8998
transport_class = import_string(self._transport_path)
90-
self._transport = transport_class(
91-
host=self._host,
92-
port=self._port,
93-
timeout=constants.SOCKET_TIMEOUT,
94-
ssl_enable=self._ssl_enable,
95-
ssl_verify=self._ssl_verify,
96-
keyfile=self._keyfile,
97-
certfile=self._certfile,
98-
ca_certs=self._ca_certs)
99-
else:
99+
self._transport = transport_class(**transport_args)
100+
elif callable(self._transport_path):
101+
self._transport = self._transport_path(**transport_args)
102+
elif hasattr(self._transport_path, 'send'):
100103
self._transport = self._transport_path
104+
else:
105+
raise RuntimeError(
106+
'Invalid transport path: must be an importable module path, '
107+
'a class or factory function or an instance.')
101108

102109
# ----------------------------------------------------------------------
103110
def _start_worker_thread(self):

0 commit comments

Comments
 (0)