Skip to content

Commit

Permalink
Merge pull request #165 from marwoodandrew/1.3
Browse files Browse the repository at this point in the history
Add Newrelic transaction per enpoint call
  • Loading branch information
akintolga authored Nov 11, 2016
2 parents b35650c + 8dfec26 commit 2bce8c8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
80 changes: 80 additions & 0 deletions server/instrumentation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2014 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

import newrelic.agent
import logging

logger = logging.getLogger(__name__)


def init_app(app):
try:
app.on_pre_GET += on_pre_request_get_callback
app.on_post_GET += on_post_request_get_callback
app.on_pre_POST += on_pre_request_post_callback
app.on_post_POST += on_post_request_post_callback
app.on_pre_PATCH += on_pre_request_patch_callback
app.on_post_PATCH += on_post_request_patch_callback
app.on_pre_DELETE += on_pre_request_delete_callback
app.on_post_DELETE += on_post_request_delete_callback
except Exception:
logger.error('Failed to start instrumentation.')


def on_pre_request_get_callback(resource, request, lookup):
newrelic_start_transaction(resource, request, lookup, 'Get')


def on_post_request_get_callback(resource, request, lookup):
newrelic_end_transaction(resource, request, lookup, 'Get')


def on_pre_request_post_callback(resource, request):
newrelic_start_transaction(resource, request, None, 'Post')


def on_post_request_post_callback(resource, request, payload):
newrelic_end_transaction(resource, request, payload, 'Post')


def on_pre_request_patch_callback(resource, request, payload):
newrelic_start_transaction(resource, request, payload, 'Patch')


def on_post_request_patch_callback(resource, request, payload):
newrelic_end_transaction(resource, request, payload, 'Patch')


def on_pre_request_delete_callback(resource, request, lookup):
newrelic_start_transaction(resource, request, lookup, 'Delete')


def on_post_request_delete_callback(resource, request, lookup):
newrelic_end_transaction(resource, request, lookup, 'Delete')


def newrelic_start_transaction(resource, request, lookup, method):
name = resource
group = 'Python/eve.endpoints/{}'.format(method)
if not resource:
return
try:
newrelic.agent.set_transaction_name(name, group=group)
except Exception:
logger.error('failed to start transaction name. {} --- {}'.format(resource, method))


def newrelic_end_transaction(resource, request, lookup, method):
try:
if not resource:
return
newrelic.agent.end_of_transaction()
except Exception:
logger.error('failed to end transaction name. {} --- {}'.format(resource, method))
1 change: 1 addition & 0 deletions server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ boto3==1.4.1
websockets==3.2
mongolock==1.3.4
PyYAML==3.12
newrelic==2.72.1.53

git+git://github.com/superdesk/superdesk-core.git@530b7250d#egg=Superdesk-Core
3 changes: 2 additions & 1 deletion server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def env(variable, fallback_value=None):
'aap_mm',
'aap.io.feed_parsers',
'aap.data_consistency',
'aap.io.iptc_extension'
'aap.io.iptc_extension',
'instrumentation'
])

RENDITIONS = {
Expand Down

0 comments on commit 2bce8c8

Please sign in to comment.