Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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