-
Notifications
You must be signed in to change notification settings - Fork 80
Add Async IO support #26
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
base: master
Are you sure you want to change the base?
Conversation
568df92
to
7b075cb
Compare
7b075cb
to
14fc194
Compare
sumologic/async.py
Outdated
|
||
class SumoLogic(object): | ||
|
||
def __init__(self, accessId, accessKey, endpoint=None, cookieFile='cookies.txt'): |
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.
there are a couple of rough edges here:
- cookies have to be added back if they are really needed
- remove explicitly setting the read timeout to infinity. I don't why but while fetching results for some search jobs I frequently got TimeOutErrors. I think this should be configurable at least.
sumologic/async.py
Outdated
self.endpoint = str(response.url).replace('/collectors', '') # dirty hack to sanitise URI and retain domain | ||
|
||
async def delete(self, method, params=None): | ||
await self._guard_endpoint() |
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.
I did it this way because I felt async constructors are unidiomatic. Are they?
@@ -0,0 +1,162 @@ | |||
from copy import copy |
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.
I don't like it that the whole file is basically copied, but I couldn't find a better way. Is there?
sumologic/async.py
Outdated
response = await self.session.get('https://api.sumologic.com/api/v1/collectors') # Dummy call to get endpoint | ||
self.endpoint = str(response.url).replace('/collectors', '') # dirty hack to sanitise URI and retain domain | ||
async def _set_endpoint(self): | ||
if self._endpoint is None: |
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.
double checked locking to avoid sending the request more than once
sumologic/async.py
Outdated
|
||
async def search_job(self, query, fromTime=None, toTime=None, timeZone='UTC'): | ||
params = {'query': query, 'from': fromTime, 'to': toTime, 'timeZone': timeZone} | ||
r = await self.post('/search/jobs', params) | ||
return json.loads(await r.text()) | ||
async with r: |
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.
prevents leaking response objects
sumologic/async.py
Outdated
return json.loads(await r.text()), r.headers['etag'] | ||
async with r: | ||
r = await self.get('/collectors/' + str(collector_id) + '/sources/' + str(source_id)) | ||
return json.loads(await r.text()), r.headers['etag'] | ||
|
||
async def create_source(self, collector_id, source): | ||
return await self.post('/collectors/' + str(collector_id) + '/sources', source) |
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.
i cannot prevent leaking here without changing the API, as this returns the request object itself.
sumologic/async.py
Outdated
SumoLogic REST API endpoint changes based on the geo location of the client. | ||
For example, If the client geolocation is Australia then the REST end point is | ||
https://api.au.sumologic.com/api/v1 | ||
async def __aenter__(self): |
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.
for safely closing the connection
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.
also it maybe wort exposing a close
method.
async def search_metrics(self, query, fromTime=None, toTime=None, requestedDataPoints=600, maxDataPoints=800): | ||
'''Perform a single Sumo metrics query''' | ||
|
||
def millisectimestamp(ts): |
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.
I believe the common functions should be extracted to a util namespace to reduce code duplications.
I added a proof of concept implementation for this lib whihc uses async http:
https://aiohttp.readthedocs.io/en/stable/index.html
I don't know yet how to include this code in a way that does not break interpreters below 3.5. I want to avoid putting it into a string and execing.
I am not very familiar with Python yet, so I appricieate any help on this. Thanks!