def wrap(module): try: # Wrap putrequest. This marks the beginning of the request, and is also # where wrapper_putrequest = oboe.log_method( HTTPLIB_LAYER, before_callback=wrap_request_putrequest, send_exit_event=False, store_backtrace=oboe._collect_backtraces('httplib')) setattr(module.HTTPConnection, 'putrequest', wrapper_putrequest(module.HTTPConnection.putrequest)) wrapper_endheaders = oboe.log_method( HTTPLIB_LAYER, before_callback=wrap_request_endheaders, send_entry_event=False, send_exit_event=False) setattr(module.HTTPConnection, 'endheaders', wrapper_endheaders(module.HTTPConnection.endheaders)) wrapper_getresponse = oboe.log_method( HTTPLIB_LAYER, callback=wrap_request_getresponse, send_entry_event=False) setattr(module.HTTPConnection, 'getresponse', wrapper_getresponse(module.HTTPConnection.getresponse)) except Exception, e: print >> sys.stderr, "Oboe error:", str(e)
def wrap(module): try: # Wrap putrequest. This marks the beginning of the request, and is also # where wrapper_putrequest = oboe.log_method(HTTPLIB_LAYER, before_callback=wrap_request_putrequest, send_exit_event=False, store_backtrace=oboe._collect_backtraces('httplib')) setattr(module.HTTPConnection, 'putrequest', wrapper_putrequest(module.HTTPConnection.putrequest)) wrapper_endheaders = oboe.log_method(HTTPLIB_LAYER, before_callback=wrap_request_endheaders, send_entry_event=False, send_exit_event=False) setattr(module.HTTPConnection, 'endheaders', wrapper_endheaders(module.HTTPConnection.endheaders)) wrapper_getresponse = oboe.log_method(HTTPLIB_LAYER, callback=wrap_request_getresponse, send_entry_event=False) setattr(module.HTTPConnection, 'getresponse', wrapper_getresponse(module.HTTPConnection.getresponse)) except Exception, e: print >> sys.stderr, "Oboe error:", str(e)
def wrap(layer_name, module): try: # first get the basic client methods; common point of processing is execute_command # client is StrictRedis for >= 2.4.10, or Redis for < 2.4.10 cls = getattr(module, 'StrictRedis', getattr(module, 'Redis', None)) if cls: execute_command = cls.execute_command wrapper = oboe.log_method(layer_name, callback=wrap_execute_command) setattr(cls, 'execute_command', wrapper(execute_command)) else: oboe._log.error("Oboe error: couldn't find redis.StrictRedis nor redis.Redis class to"\ " instrument, redis coverage may be partial.") # RemoteHost cls = getattr(module, 'Connection', None) if cls: wrapper = wrap_send_packed_command(layer_name, cls.send_packed_command) setattr(cls, 'send_packed_command', wrapper) else: oboe._log.error("Oboe error: couldn't find redis.Connection class to instrument, "\ "redis coverage may be partial.") # pipeline/multi cls = getattr(module.client, 'BasePipeline', getattr(module.client, 'Pipeline', None)) if cls: wrapper = oboe.log_method(layer_name, callback=wrap_execute_pipeline) execute_pipeline = cls._execute_pipeline setattr(cls, '_execute_pipeline', wrapper(execute_pipeline)) execute_transaction = cls._execute_transaction setattr(cls, '_execute_transaction', wrapper(execute_transaction)) else: oboe._log.error("Oboe error: couldn't find redis.client.BasePipeline nor "\ "redis.client.Pipeline class to instrument, "\ "redis coverage may be partial.") # pubsub cls = getattr(module.client, 'PubSub', None) if cls: execute_command = cls.execute_command wrapper = oboe.log_method(layer_name, callback=wrap_execute_command) setattr(cls, 'execute_command', wrapper(execute_command)) else: oboe._log.error("Oboe error: couldn't find redis.client.PubSub class to instrument, "\ "redis coverage may be partial.") except Exception, e: print >> sys.stderr, "Oboe error:", str(e)
def wrap(module): try: # profile on specific template name (inner wrap) profile_wrapper_render = oboe.log_method(None, before_callback=before_render_cb, callback=after_render_cb) # overall template layer (outer wrap) layer_wrapper_render = oboe.log_method(TEMPLATE_LAYER) setattr(module.Template, 'render', layer_wrapper_render(profile_wrapper_render(module.Template.render))) except Exception, e: print >> sys.stderr, "Oboe error:", str(e)
def wrap(layer_name, module): try: # wrap middleware callables we want to wrap cls = getattr(module, 'Client', None) if not cls: return for method in MC_COMMANDS: # delete_multi delegates to delete in pylibmc, so don't instrument it if method == 'delete_multi' and module.__name__ == 'pylibmc': continue fn = getattr(cls, method, None) if not fn: # this method does not exist for this module/version continue kvs = {'Class': layer_name + '.Client', 'Function': method} wrapfn = fn if hasattr(fn, 'im_func') else dynamic_wrap(fn) wrapper = oboe.log_method(layer_name, # XXX Not Python2.4-friendly callback=partial(wrap_mc_method, funcname=method), entry_kvs=kvs) setattr(cls, method, wrapper(wrapfn)) # per-key memcache host hook if hasattr(cls, '_get_server'): fn = getattr(cls, '_get_server', None) setattr(cls, '_get_server', wrap_get_server(layer_name, fn)) except Exception, e: print >> sys.stderr, "Oboe error:", str(e)
def wrap(module): try: cursor_method = module.BaseDatabaseWrapper.cursor if getattr(cursor_method, '_oboe_wrapped', False): return oboe_wrapper = oboe.log_method( 'djangoORM', callback=wrap_execute, store_backtrace=oboe._collect_backtraces('django_orm')) setattr(CursorOboeWrapper, 'execute', oboe_wrapper(CursorOboeWrapper.execute)) setattr(CursorOboeWrapper, 'executemany', oboe_wrapper(CursorOboeWrapper.executemany)) def cursor_wrap(self): try: return CursorOboeWrapper(cursor_method(self), self) except Exception, e: print >> sys.stderr, "[oboe] Error in cursor_wrap", e raise cursor_wrap._oboe_wrapped = True setattr(module.BaseDatabaseWrapper, 'cursor', cursor_wrap)
def wrap_methods(cls, mappings): for name, fn in mappings.items(): try: base_method = getattr(cls, name) except AttributeError: log.warn('Failed to patch %s on %s', name, cls.__name__) else: oboe_fn = oboe.log_method( 'sqlalchemy', store_backtrace=oboe._collect_backtraces('sqlalchemy'), callback=fn)(base_method) setattr(cls, name, oboe_fn)
def wrap_class(cls, class_name, class_method_inst): """ wrap class methods with instrumentation calls """ if not cls: return for (method, method_log_args) in class_method_inst.iteritems(): fn = getattr(cls, method, None) if not fn: # Not all methods may be in all versions of pymongo... continue kvs = { 'Class': '%s.%s' % (cls.__module__, cls.__name__), 'Function': method, 'Action': '%s.%s' % (class_name, method), } # XXX Not Python2.4-friendly setattr(cls, method, oboe.log_method(PYMONGO_LAYER, entry_kvs=kvs, **method_log_args)(fn))
def wrap(module): try: cursor_method = module.BaseDatabaseWrapper.cursor if getattr(cursor_method, '_oboe_wrapped', False): return oboe_wrapper = oboe.log_method('djangoORM', callback=wrap_execute, store_backtrace=oboe._collect_backtraces('django_orm')) setattr(CursorOboeWrapper, 'execute', oboe_wrapper(CursorOboeWrapper.execute)) setattr(CursorOboeWrapper, 'executemany', oboe_wrapper(CursorOboeWrapper.executemany)) def cursor_wrap(self): try: return CursorOboeWrapper(cursor_method(self), self) except Exception, e: print >> sys.stderr, "[oboe] Error in cursor_wrap", e raise cursor_wrap._oboe_wrapped = True setattr(module.BaseDatabaseWrapper, 'cursor', cursor_wrap)
"""Wraps the publish method in the current oboe context. """ def add_context(request, *args, **kwargs): ctx = None xtr = request.get_header('X-TRACE') if xtr: md = oboe.Metadata.fromString(xtr) ctx = oboe.Context(md) ctx.set_as_default() try: res = meth(request, *args, **kwargs) if oboe.Context.get_default().is_valid(): oboe.Context.clear_default() return res except: raise return add_context from ZPublisher import Publish Publish.orig_publish = Publish.publish publish_wrapper = oboe.log_method('zope_publish') wrapped_publish = publish_wrapper(Publish.orig_publish) Publish.publish = context_wrapper(wrapped_publish)
return f_args, f_kwargs, kv def get_wrapper(meth): def extract(self, key): kv = {'KVOp': 'get', 'KVKey': key} oboe.log('entry', 'memoize', keys=kv, store_backtrace=False) try: val = meth(self, key) kv = {'KVHit': True} oboe.log('exit', 'memoize', keys=kv, store_backtrace=False) return val except: kv = {'KVHit': False} oboe.log('exit', 'memoize', keys=kv, store_backtrace=False) raise return extract from plone.memoize.ram import RAMCacheAdapter orig___setitem__ = RAMCacheAdapter.__setitem__ cache_wrapper = oboe.log_method('memoize', before_callback=extract_cache_key) RAMCacheAdapter.__setitem__ = cache_wrapper(orig___setitem__) orig___getitem__ = RAMCacheAdapter.__getitem__ RAMCacheAdapter.__getitem__ = get_wrapper(orig___getitem__)
import oboe from plone.transformchain import zpublisher orig_applyTransform = zpublisher.applyTransform transform_wrapper = oboe.log_method('plone_tchain', store_args=False, store_backtrace=False) zpublisher.applyTransform = transform_wrapper(orig_applyTransform)
'Collection': obj.__class__.__name__, 'Query': query } return f_args, f_kwargs, kv def extract_obj_info(func, f_args, f_kwargs, res): kv = {} obj = f_args[1] if hasattr(obj, 'id'): kv['obj.id'] = obj.id if hasattr(obj, 'title'): kv['obj.title'] = obj.title title = obj.title if isinstance(title, unicode): title = title.encode('utf-8', 'replace') kv['obj.title'] = title return kv Connection.orig_setstate = Connection.setstate ss_wrapper = oboe.log_method('zodb', entry_kvs={'QueryOp': 'setstate'}, before_callback=build_query, callback=extract_obj_info) Connection.setstate = ss_wrapper(Connection.orig_setstate) commit_wrapper = oboe.log_method('zodb', entry_kvs={'QueryOp': 'commit'}) Connection.orig_commit = Connection.commit Connection.commit = commit_wrapper(Connection.orig_commit)
def add_context(request, *args, **kwargs): ctx = None xtr = request.get_header('X-TRACE') if xtr: md = oboe.Metadata.fromString(xtr) ctx = oboe.Context(md) ctx.set_as_default() try: res = meth(request, *args, **kwargs) if oboe.Context.get_default().is_valid(): oboe.Context.clear_default() return res except: raise return add_context from ZPublisher import Publish Publish.orig_publish = Publish.publish publish_wrapper = oboe.log_method('zope_publish') wrapped_publish = publish_wrapper(Publish.orig_publish) Publish.publish = context_wrapper(wrapped_publish)
import oboe def get_template_filename(func, f_args, f_kwargs, res): kv = {} template_obj = f_args[0] if template_obj and hasattr(template_obj, 'filename'): kv['filename'] = template_obj.filename return kv from chameleon.zpt.template import PageTemplate orig_render = PageTemplate.render template_wrapper = oboe.log_method('chameleon', store_args=False, store_backtrace=False, callback=get_template_filename) PageTemplate.render = template_wrapper(orig_render)
import oboe def extract_count(func, f_args, f_kwargs, res): if res: return {'Count': len(res)} else: return {'Count': 0} from Products.CMFPlone.CatalogTool import CatalogTool CatalogTool.orig_searchResults = CatalogTool.searchResults zc_wrapper = oboe.log_method('plone_zcatalog', store_args=True, store_backtrace=True, callback=extract_count) CatalogTool.searchResults = zc_wrapper(CatalogTool.orig_searchResults)
import oboe def extract_template(func, f_args, f_kwargs): self = f_args[0] kv = {'Template': self.program[2][1]} return f_args, f_kwargs, kv from zope.tal.talinterpreter import TALInterpreter TALInterpreter.orig_call = TALInterpreter.__call__ template_wrapper = oboe.log_method('zope_template', before_callback=extract_template) TALInterpreter.__call__ = template_wrapper(TALInterpreter.orig_call)
} return f_args, f_kwargs, kv def extract_obj_info(func, f_args, f_kwargs, res): kv = {} obj = f_args[1] if hasattr(obj, 'id'): kv['obj.id'] = obj.id if hasattr(obj, 'title'): kv['obj.title'] = obj.title title = obj.title if isinstance(title, unicode): title = title.encode('utf-8', 'replace') kv['obj.title'] = title return kv Connection.orig_setstate = Connection.setstate ss_wrapper = oboe.log_method('zodb', entry_kvs={'QueryOp': 'setstate'}, before_callback=build_query, callback=extract_obj_info) Connection.setstate = ss_wrapper(Connection.orig_setstate) commit_wrapper = oboe.log_method('zodb', entry_kvs={'QueryOp': 'commit'}) Connection.orig_commit = Connection.commit Connection.commit = commit_wrapper(Connection.orig_commit)