Skip to content
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

Add proxy support for SSE transport #79

Merged
merged 3 commits into from
Apr 17, 2019
Merged
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
3 changes: 3 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r requirements.txt

httmock
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
certifi
httmock
protobuf>=3.0.0
pyformance>=0.3.1
requests>=2.7.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
long_description=long_description,
zip_safe=True,
packages=find_packages(),
test_requires=test_requirements,
install_requires=requirements,
tests_require=test_requirements,
classifiers=[
'Operating System :: OS Independent',
'Programming Language :: Python',
Expand Down
5 changes: 3 additions & 2 deletions signalfx/signalflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class SignalFlowClient(object):
def __init__(self, token, endpoint=constants.DEFAULT_STREAM_ENDPOINT,
timeout=constants.DEFAULT_TIMEOUT,
transport=ws.WebSocketTransport,
compress=True):
self._transport = transport(token, endpoint, timeout, compress)
compress=True, proxy_url=None):
self._transport = transport(token, endpoint, timeout, compress,
proxy_url)
self._computations = set([])

def __enter__(self):
Expand Down
16 changes: 13 additions & 3 deletions signalfx/signalflow/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class SignalFlowException(Exception):
"""A generic error encountered when interacting with the SignalFx
SignalFlow API."""

def __init__(self, code, message=None):
def __init__(self, code, message=None, error_type=None):
self._code = code
self._message = message
self._error_type = error_type

@property
def code(self):
Expand All @@ -19,10 +20,19 @@ def message(self):
"""Returns an optional error message attached to this error."""
return self._message

@property
def error_type(self):
"""Returns an optional error type attached to this error."""
return self._error_type

def __str__(self):
err = self._code
if self._error_type:
err = '{0} ({1})'.format(self._code, self._error_type)

if self._message:
return '{0}: {1}'.format(self._code, self._message)
return 'Error {0}'.format(self._code)
return '{0}: {1}'.format(err, self._message)
return 'Error {0}'.format(err)


class ComputationAborted(Exception):
Expand Down
13 changes: 10 additions & 3 deletions signalfx/signalflow/sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class SSETransport(transport._SignalFlowTransport):
_SIGNALFLOW_ENDPOINT = 'v2/signalflow'

def __init__(self, token, endpoint=constants.DEFAULT_STREAM_ENDPOINT,
timeout=constants.DEFAULT_TIMEOUT, compress=True):
timeout=constants.DEFAULT_TIMEOUT, compress=True,
proxy_url=None):
super(SSETransport, self).__init__(token, endpoint, timeout)
pool_args = {
'url': self._endpoint,
Expand All @@ -43,7 +44,12 @@ def __init__(self, token, endpoint=constants.DEFAULT_STREAM_ENDPOINT,
'ca_certs': certifi.where() # Path to the Certifi bundle.
})

self._http = urllib3.connectionpool.connection_from_url(**pool_args)
if proxy_url:
proxy_manager = urllib3.poolmanager.proxy_from_url(proxy_url)
endpoint = pool_args.pop('url')
self._http = proxy_manager.connection_from_url(endpoint, pool_kwargs=pool_args)
else:
self._http = urllib3.connectionpool.connection_from_url(**pool_args)

def __str__(self):
return 'sse+{0}'.format(self._endpoint)
Expand All @@ -58,7 +64,8 @@ def _post(self, url, fields=None, body=None):
if r.status != 200:
try:
if r.headers['Content-Type'] == 'application/json':
raise errors.SignalFlowException(**json.loads(r.read()))
rbody = json.loads(r.read())
raise errors.SignalFlowException(r.status, rbody.get('message'), rbody.get('errorType'))
raise errors.SignalFlowException(r.status)
finally:
r.close()
Expand Down
6 changes: 5 additions & 1 deletion signalfx/signalflow/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class WebSocketTransport(transport._SignalFlowTransport, WebSocketClient):
_SIGNALFLOW_WEBSOCKET_ENDPOINT = 'v2/signalflow/connect'

def __init__(self, token, endpoint=constants.DEFAULT_STREAM_ENDPOINT,
timeout=constants.DEFAULT_TIMEOUT, compress=True):
timeout=constants.DEFAULT_TIMEOUT, compress=True,
proxy_url=None):
if proxy_url:
raise NotImplementedError('Websocket transport cannot be proxied!')

ws_endpoint = '{0}/{1}'.format(
endpoint.replace('http', 'ws', 1),
WebSocketTransport._SIGNALFLOW_WEBSOCKET_ENDPOINT)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ envlist = py27,py35,flake8
[testenv]
usedevelop = True
passenv = ORG_NAME_FOR_SIGNALFX_API_TOKEN_IS_SFx_Test SIGNALFX_API_TOKEN
deps = -r{toxinidir}/requirements.txt
deps = -r{toxinidir}/requirements-test.txt
ignore_errors = True
commands = python {toxinidir}/tests/test_data_reporting.py
python {toxinidir}/tests/live_tests.py --metric_name 'MET' --tag_name 'TG' --key 'K' --value 'V'
Expand Down