Skip to content

Instantly share code, notes, and snippets.

@matagus
Last active May 24, 2016 20:18
Show Gist options
  • Save matagus/ecec9db26db2143e196f659161bc5918 to your computer and use it in GitHub Desktop.
Save matagus/ecec9db26db2143e196f659161bc5918 to your computer and use it in GitHub Desktop.

Revisions

  1. matagus revised this gist May 24, 2016. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. matagus revised this gist May 24, 2016. No changes.
  3. matagus revised this gist May 24, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion opbeat_middleware.py
    Original file line number Diff line number Diff line change
    @@ -29,5 +29,10 @@ def process_response(self, req, resp, resource):

    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, req.status)
    self.client.end_transaction(rule, status_code)
  4. matagus revised this gist May 24, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion opbeat_middleware.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    opbeat.instrumentation.control.instrument()


    class OpbeatAPM(object):
    class OpbeatAPMMiddleware(object):
    """
    Falcon Framework middleware to track resources performance @ Opbeat
    """
  5. matagus created this gist May 24, 2016.
    30 changes: 30 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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)
    33 changes: 33 additions & 0 deletions opbeat_middleware.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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 OpbeatAPM(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)

    if not opbeat_is_debug_mode:
    self.client.end_transaction(rule, req.status)