Skip to content

Instantly share code, notes, and snippets.

@iromli
Last active December 15, 2015 19:09
Show Gist options
  • Save iromli/5308747 to your computer and use it in GitHub Desktop.
Save iromli/5308747 to your computer and use it in GitHub Desktop.
USE_FNORDMETRIC = False
FNORDMETRIC_REDIS_PREFIX = 'fnordmetric'
FNORDMETRIC_REDIS_HOST = '127.0.0.1'
FNORDMETRIC_REDIS_DB = 0
FNORDMETRIC_REDIS_PORT = 6379
FNORDMETRIC_REDIS_PASSWORD = None
class BaseResource(Resource):
def dispatch_request(self, *args, **kwargs):
resp = super(BaseResource, self).dispatch_request(*args, **kwargs)
_, code, _ = unpack(resp)
if code != 500 and current_app.config['USE_FNORDMETRIC']:
path = '{0} {1}'.format(request.method, request.path)
if request.query_string:
path = '{0}?{1}'.format(path, request.query_string)
extra = {'keyword': path}
fnord = fnordmetric.FnordMetric(
host=current_app.config['FNORDMETRIC_REDIS_HOST'],
port=current_app.config['FNORDMETRIC_REDIS_PORT'],
db=current_app.config['FNORDMETRIC_REDIS_DB'],
password=current_app.config['FNORDMETRIC_REDIS_PASSWORD'],
prefix=current_app.config['FNORDMETRIC_REDIS_PREFIX'],
)
fnord.event('search', extra=extra)
return resp
# -*- coding: utf-8 -*-
import os
import redis
import json
import base64
class FnordMetric:
def __init__(self, host="localhost", port=6379, db=0,
password=None, prefix='fnordmetric'):
self.redis = redis.Redis(
host=host, port=port, db=db, password=password)
self.prefix = prefix
def _queue_event(self, event):
event_id = base64.urlsafe_b64encode(os.urandom(33))
self.redis.set(
"{0}-event-{1}".format(self.prefix, event_id), json.dumps(event))
self.redis.expire(
"{0}-event-{1}".format(self.prefix, event_id), 60)
self.redis.lpush("{0}-queue".format(self.prefix), event_id)
def event(self, eventtype, extra=None):
event = {"_type": eventtype}
if isinstance(extra, dict):
event.update(extra)
self._queue_event(event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment