Skip to content

Instantly share code, notes, and snippets.

@matagus
Last active May 24, 2016 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matagus/ecec9db26db2143e196f659161bc5918 to your computer and use it in GitHub Desktop.
Save matagus/ecec9db26db2143e196f659161bc5918 to your computer and use it in GitHub Desktop.
How to integrate a Falcon Framework (python) app with Opbeat, tracking errors and performance! Tested on pypy 5.0.1 and pypy-3.2 :)
import opbeat.instrumentation.control
from opbeat.utils import build_name_with_http_method_prefix
from settings import DEBUG
opbeat_is_debug_mode = DEBUG
opbeat.instrumentation.control.instrument()
class OpbeatAPMMiddleware(object):
"""
Falcon Framework middleware to track resources performance @ Opbeat
"""
def __init__(self, client):
self.client = client
def process_request(self, req, resp):
if not opbeat_is_debug_mode:
self.client.begin_transaction("web.falcon")
def process_response(self, req, resp, resource):
name = "{}.{}".format(
resource.__class__.__module__,
resource.__class__.__name__
)
rule = build_name_with_http_method_prefix(name, req)
try:
status_code = int(resp.status.split(" ")[0])
except (IndexError, TypeError):
status_code = 200
if not opbeat_is_debug_mode:
self.client.end_transaction(rule, status_code)
import falcon
from opbeat import Client
from opbeat.middleware import Opbeat
from apps.my_resource import MyResource
from opbeat_middleware import OpbeatAPMMiddleware
from settings import OPBEAT_ORGANIZATION_ID, OPBEAT_APP_ID, OPBEAT_SECRET_TOKEN, DEBUG
client = Client(
organization_id=OPBEAT_ORGANIZATION_ID,
app_id=OPBEAT_APP_ID,
secret_token=OPBEAT_SECRET_TOKEN,
DEBUG=DEBUG,
TIMEOUT=5
)
app = falcon.API(
middleware=[
OpbeatAPMMiddleware(client),
...
]
)
app.add_route('/path/to/some/resource/', MyResource())
app = Opbeat(app, client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment