Skip to content

Instantly share code, notes, and snippets.

@amjith
Created August 14, 2012 00:16
Show Gist options
  • Save amjith/3345036 to your computer and use it in GitHub Desktop.
Save amjith/3345036 to your computer and use it in GitHub Desktop.
instrument_webpy
import sys
import newrelic.api.transaction
import newrelic.api.function_trace
import newrelic.api.in_function
import newrelic.api.out_function
import newrelic.api.pre_function
import newrelic.api.name_transaction
from newrelic.api.object_wrapper import callable_name
from newrelic.api.web_transaction import WSGIApplicationWrapper
def _name_transaction(*args, **kwargs):
transaction = newrelic.api.transaction.current_transaction()
if transaction:
f = args[1] if isinstance(args[1], basestring) else (
callable_name(args[1]))
transaction.name_transaction(f)
return (args, kwargs)
def wrap_handle_exception(self):
transaction = newrelic.api.transaction.current_transaction()
if transaction:
transaction.record_exception(*sys.exc_info())
def instrument(module):
if module.__name__ == 'web.application':
newrelic.api.out_function.wrap_out_function(
module, 'application.wsgifunc', WSGIApplicationWrapper)
newrelic.api.in_function.wrap_in_function(
module, 'application._delegate', _name_transaction)
newrelic.api.pre_function.wrap_pre_function(
module, 'application.internalerror', wrap_handle_exception)
elif module.__name__ == 'web.template':
newrelic.api.function_trace.wrap_function_trace(
module, 'render._template')
""" Basic wiki using webpy 0.3 """
import web
import model
import markdown
### Url mappings
urls = (
'/', 'Index',
'/new', 'New',
'/edit/(\d+)', 'Edit',
'/delete/(\d+)', 'Delete',
'/(.*)', 'Page',
)
### Templates
t_globals = {
'datestr': web.datestr,
'markdown': markdown.markdown,
}
render = web.template.render('templates', base='base', globals=t_globals)
class Index:
def GET(self):
""" Show page """
pages = model.get_pages()
return render.index(pages)
class Page:
def GET(self, url):
""" View single page """
page = model.get_page_by_url(url)
if not page:
raise web.seeother('/new?url=%s' % web.websafe(url))
return render.view(page)
class New:
def not_page_exists(url):
return not bool(model.get_page_by_url(url))
page_exists_validator = web.form.Validator('Page already exists',
not_page_exists)
form = web.form.Form(
web.form.Textbox('url', web.form.notnull, page_exists_validator,
size=30,
description="Location:"),
web.form.Textbox('title', web.form.notnull,
size=30,
description="Page title:"),
web.form.Textarea('content', web.form.notnull,
rows=30, cols=80,
description="Page content:", post="Use markdown syntax"),
web.form.Button('Create page'),
)
def GET(self):
url = web.input(url='').url
form = self.form()
form.fill({'url': url})
return render.new(form)
def POST(self):
form = self.form()
if not form.validates():
return render.new(form)
model.new_page(form.d.url, form.d.title, form.d.content)
raise web.seeother('/' + form.d.url)
class Delete:
def POST(self, id):
model.del_page(int(id))
raise web.seeother('/')
class Edit:
form = web.form.Form(
web.form.Textbox('url', web.form.notnull,
size=30,
description="Location:"),
web.form.Textbox('title', web.form.notnull,
size=30,
description="Page title:"),
web.form.Textarea('content', web.form.notnull,
rows=30, cols=80,
description="Page content:", post="Use markdown syntax"),
web.form.Button('Update page'),
)
def GET(self, id):
page = model.get_page_by_id(int(id))
form = self.form()
form.fill(page)
return render.edit(page, form)
def POST(self, id):
form = self.form()
page = model.get_page_by_id(int(id))
if not form.validates():
return render.edit(page, form)
model.update_page(int(id), form.d.url, form.d.title, form.d.content)
raise web.seeother('/')
app = web.application(urls, globals())
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment