Last active
May 24, 2016 20:18
-
-
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 :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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