def test(self): enabled = [] for _ in range(100): parent_context = blueox.Context('test', 5) with parent_context: sub_enabled = [] for _ in range(10): context = blueox.Context('.sub', sample=('..', 0.25)) sub_enabled.append(1 if context.enabled else 0) enabled.append(1 if context.enabled else 0) assert all(sub_enabled) or not any(sub_enabled) assert 400 > sum(enabled) > 150
def wrapped_callback(*args, **kwargs): with blueox.Context('.dynochemy') as ctx: # Rewrite our start time, this is async, remember ctx.start_time = start_time ctx.set('command', command) resp = args[0] err = kwargs.get('error') if 'Responses' in resp: for table_name, table_resp in resp['Responses'].iteritems( ): if 'ConsumedCapacityUnits' in table_resp: ctx.set('tables.%s.consumed_capacity' % table_name, table_resp['ConsumedCapacityUnits']) elif 'ConsumedCapacityUnits' in resp: req = json.loads(body) ctx.set( 'tables.%s.consumed_capacity' % req.get('TableName', ''), resp['ConsumedCapacityUnits']) if 'Count' in resp: ctx.set('count', resp['Count']) if err: ctx.set('error', str(err)) if callback: callback(*args, **kwargs)
def test(self): c = blueox.Context('test') c.start() blueox.clear_contexts() assert_equal(blueox.current_context(), None)
def test(self): enabled = [] for _ in range(100): context = blueox.Context('test', 5, sample=('test', 0.25)) enabled.append(1 if context.enabled else 0) assert 40 > sum(enabled) > 15
def on_task_sent(**kwargs): with blueox.Context('.celery.task_sent'): # Arguments for this signal are different than the worker signals. Sometimes # they are even different than what the documentation says. See also # https://github.com/celery/celery/issues/1606 blueox.set('task_id', kwargs.get('task_id', kwargs.get('id'))) blueox.set('task', str(kwargs['task'])) blueox.set('eta', kwargs['eta'])
def on_task_prerun(**kwargs): ctx = blueox.Context(".".join((getattr(settings, 'BLUEOX_NAME', ''), 'celery', 'task'))) ctx.start() ctx.set('task', kwargs['task'].name) ctx.set('retries', kwargs['task'].request.retries) ctx.set('expires', kwargs['task'].request.expires) ctx.set('delivery_info', kwargs['task'].request.delivery_info) ctx.set('task_id', str(kwargs['task_id'])) ctx.set('args', kwargs['args']) ctx.set('kwargs', kwargs['kwargs']) if revision: ctx.set('version', revision)
def before_request(self, *args, **kwargs): request.blueox = blueox.Context(".".join( (self.app.config.get('BLUEOX_NAME', ''), 'request'))) blueox.set('method', request.method) blueox.set('path', request.path) headers = {} for k, v in request.environ.iteritems(): if (k.startswith('HTTP_') or k in ('CONTENT_LENGTH', 'CONTENT_TYPE')): headers[k] = v blueox.set('headers', headers) blueox.set('url', request.url) blueox.set('client_ip', request.environ.get('REMOTE_ADDR'))
def process_request(self, request): request.blueox = blueox.Context(".".join( (getattr(settings, 'BLUEOX_NAME', ''), 'request'))) request.blueox.start() blueox.set('method', request.method) blueox.set('path', request.path) headers = {} for k, v in request.META.iteritems(): if k.startswith('HTTP_') or k in ('CONTENT_LENGTH', 'CONTENT_TYPE'): headers[k] = v blueox.set('headers', headers) blueox.set('uri', request.build_absolute_uri()) blueox.set('client_ip', request.META['REMOTE_ADDR']) return None
def on_task_sent(sender=None, body=None, **kwargs): with blueox.Context('.celery.task_sent'): blueox.set('task_id', body['id']) blueox.set('task', str(body['task'])) blueox.set('eta', body['eta'])
def fetch(self, request, callback=None, **kwargs): start_time = time.time() if isinstance(request, basestring): request = tornado.httpclient.HTTPRequest(url=request, **kwargs) ctx = blueox.Context(self.blueox_name) ctx.start() ctx.set('request.uri', request.url) ctx.set('request.method', request.method) ctx.set('request.size', len(request.body) if request.body else 0) ctx.stop() # I'd love to use the future to handle the completion step, BUT, we # need this to happen first. If the caller has provided a callback, we don't want them # to get called before we do. Rather than poke into the internal datastructures, we'll just # handle the callback explicitly def complete_context(response): ctx.start() ctx.set('response.code', response.code) ctx.set('response.size', len(response.body) if response.body else 0) ctx.done() if callback is None: def fetch_complete(future): # This error handling is just copied from tornado.httpclient as # we need to record a real HTTPError. httpclient might do the same thing # again if needs to deal with the caller's callbacks. exc = future.exception() if isinstance(exc, tornado.httpclient.HTTPError ) and exc.response is not None: response = exc.response elif exc is not None: response = tornado.httpclient.HTTPResponse( request, 599, error=exc, request_time=time.time() - start_time) else: response = future.result() complete_context(response) future = super(AsyncHTTPClient, self).fetch(request) future.add_done_callback(fetch_complete) else: def callback_wrapper(response): complete_context(response) callback(response) future = super(AsyncHTTPClient, self).fetch(request, callback=callback_wrapper) return future
def prepare(self): self.blueox_ctx = blueox.Context(self.blueox_name) self.blueox_ctx.start() super(BlueOxRequestHandlerMixin, self).prepare()
def broken_context(self): c = blueox.Context('test') assert not c.writable blueox.context._add_context(c)
def test(self): with blueox.Context('test', 5): blueox.set('foo', True)