<s> from . output import Output <EOL> import gevent <EOL> import gevent . monkey <EOL> gevent . monkey . patch_socket ( ) <EOL> import urllib2 <EOL> import json <EOL> class Elasticsearch ( Output ) : <EOL> """<STR_LIT>""" <EOL> RETRIES = <NUM_LIT:10> <EOL> def __init__ ( self , index , type , host = '<STR_LIT:localhost>' , port = <NUM_LIT> ) : <EOL> super ( Elasticsearch , self ) . __init__ ( ) <EOL> self . host = host <EOL> self . port = port <EOL> self . index = index <EOL> self . type = type <EOL> def process ( self , event ) : <EOL> data = event . to_json ( ) <EOL> index = event . format ( self . index ) <EOL> itype = event . format ( self . type ) <EOL> if not index : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not itype : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> url = '<STR_LIT>' % ( self . host , self . port , index , itype ) <EOL> success = False <EOL> delay = <NUM_LIT:1.0> <EOL> for retry in xrange ( self . RETRIES ) : <EOL> try : <EOL> res = urllib2 . urlopen ( url , data = data ) <EOL> success = True <EOL> result = json . load ( res ) <EOL> break <EOL> except urllib2 . HTTPError as ex : <EOL> if ex . getcode ( ) == <NUM_LIT> : <EOL> self . logger . error ( "<STR_LIT>" % ( ex , ) ) <EOL> break <EOL> else : <EOL> delay *= <NUM_LIT> <EOL> self . logger . warn ( '<STR_LIT>' % ( ex , delay ) ) <EOL> gevent . sleep ( delay ) <EOL> except urllib2 . URLError as ex : <EOL> delay *= <NUM_LIT> <EOL> self . logger . warn ( '<STR_LIT>' % ( ex , delay ) ) <EOL> gevent . sleep ( delay ) <EOL> if success : <EOL> self . logger . debug ( '<STR_LIT>' % ( index , itype , result [ '<STR_LIT>' ] ) ) </s>
