def wsgi_app(self, env, start): path = env['PATH_INFO'].strip('/') tag = path.replace('/', '.') content_type = env.get('CONTENT_TYPE', '') params = dict(cgi.parse_qsl(env['QUERY_STRING'])) input = env['wsgi.input'] if content_type.startswith('application/x-www-form-urlencoded'): params.update(cgi.parse_qs(input.read())) elif content_type.startswith('multipart/form-data'): fs = cgi.FieldStorage(input, environ=env) for k in fs.keys(): params[k] = fs.getfirst(k) elif content_type.startswith('application/json'): params['json'] = input.read() if 'msgpack' in params: record = msgpack.unpackb(params['msgpack']) elif 'json' in params: record = json.loads(params['json']) else: record = params if 'time' in params: time_ = int(params['time']) else: time_ = int(time.time()) log.debug("Recieve message: tag=%r, record=%r", tag, record) Engine.emit(tag, time_, record) start("200 OK", [('Content-Type', 'text/plain')]) return [""]
def emit(self, line): tai, line = line.split(' ', 1) tai = int(tai[1:17], 16) if tai >= 2**62: tai -= 2**62 else: tai = 0 Engine.emit(self.tag, tai, {self.key: line})
def on_message(self, msg): tag = msg[0] log.debug("on_message: recieved message %s", tag) entries = msg[1] ent_type = type(entries) if ent_type is bytes: Engine.emit_stream(tag, UnpackStream(entries)) elif ent_type in (list, tuple): Engine.emit_stream( tag, [(e[0] or now(), e[1]) for e in entries], ) else: Engine.emit(tag, msg[1] or now(), msg[2])
def start(self): #todo: supervise Engine.read_config(self._opts.config) Engine.run()
def emit(self, line): Engine.emit(self.tag, int(time.time()), {self.key: line})
def receive_lines(self, lines): lines = map(self.parser.parse, lines) Engine.emit_stream(self.tag, lines)