Getting HTTPError after upgrading splunk
hi, I recently took over a legacy project and upgraded the splunk python library due to an existing error (#131). the old version was v1.0.0 and I upgrade to v1.6.5(latest)
after the upgrade I started seeing 2 more new error message, one of which being an HTTPError:
File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 861, in login
cookie="1") # In Splunk 6.2+, passing "cookie=1" will return the "set-cookie" header
File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 1208, in post
return self.request(url, message)
File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 1228, in request
raise HTTPError(response)
splunklib.binding.HTTPError: HTTP 504 GATEWAY_TIMEOUT --
Nothing else was changed for this project or the splunk server I used, so I figured this error message showing up may be related to the library upgrade. I was wondering if there's any request retry / default time value related changes between 1.0.0 to 1.6.5 that may have caused this.
suggestions on how to make the library more fault tolerant is appreciated. I'd like to know if it's possible the configure the timeout value used for requests made from splunk library
Hi @duxing,
Which version of Splunk are you on, and what does the environment look like (clustering, load balanced, etc)?
I believe the version of splunk I'm on is 6.6.3 I don't manage the splunk server so I'm not 100% sure about this, but the splunk server should have a big cluster with load balancer.
it's definitely very likely that this issue is caused by splunk server overloaded.
the error I posted here is from a minutely cron job, which did not report any http error in the past until the upgrade of the python sdk. I'm wondering if you know anything in the client lib may result in an early timeout that can help me explain this.
Can you share your code either here or shoot us an email [email protected]? I can't think of anything obvious, but happy to take a look
I've looked at the client python code we had and nothing seems wrong to me.
the code on the client:
import requests
from splunklib.binding import ResponseReader
from splunklib.client import Service
import splunklib.results as results
from StringIO import StringIO
class RequestHandler(object):
def __init__(self, verify=False):
self.session = requests.Session()
self.session.verify = verify
def __call__(self, url, message, **kwargs):
method = message.get('method', 'GET')
data = message.get('body', None)
headers = message.get('headers', None)
if headers is not None:
headers = dict(headers)
rsp = self.session.request(method, url, data=data, headers=headers)
rpy_headers = rsp.headers.items()
return {
'status': rsp.status_code,
'reason': rsp.reason,
'headers': rpy_headers,
'body': ResponseReader(StringIO(rsp.content))
}
def splunk_connect(**kwargs):
svc = Service(handler=RequestHandler(), **kwargs)
svc.login()
return svc
and example for usage:
splunk_connect(host='<my_splunk_host>', port=8089, username='<my_user>', password='<my_password>')
@duxing which version of Python are you using?
I was able to get your code to work by changing 2 things:
from StringIO import StringIOtofrom io import BytesIO'body': ResponseReader(StringIO(rsp.content))to'body': ResponseReader(BytesIO(rsp.content))
@shakeelmohamed thanks for taking a look. we're using python 2.7
it's been a while and I've been monitoring the same job. seems like the error happens more often during the busy hours for the destination splunk server.
to me it doesn't seem like anything wrong with the client python lib. probably the error reporting in old version (1.0.0) was bad and has been silencing the same error before the upgrade.