Skip to content

Commit

Permalink
fix: Replace newrelic function_trace with edx_django_utils monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
julianajlk committed Jul 9, 2024
1 parent fd38b22 commit def0e61
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
3 changes: 1 addition & 2 deletions ecommerce/extensions/basket/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


import newrelic.agent
from edx_django_utils import monitoring as monitoring_utils
from oscar.apps.basket.middleware import BasketMiddleware as OscarBasketMiddleware
from oscar.core.loading import get_model
Expand Down Expand Up @@ -81,6 +80,6 @@ def get_basket(self, request):

return basket

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('apply_offers_to_basket')
def apply_offers_to_basket(self, request, basket):
apply_offers_on_basket(request, basket)
34 changes: 17 additions & 17 deletions ecommerce/extensions/basket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import logging
from urllib.parse import unquote, urlencode

import newrelic.agent
import pytz
import waffle
from django.conf import settings
from django.contrib import messages
from django.db import transaction
from django.utils.translation import ugettext_lazy as _
from edx_django_utils import monitoring as monitoring_utils
from oscar.apps.basket.signals import voucher_addition
from oscar.core.loading import get_class, get_model

Expand Down Expand Up @@ -100,7 +100,7 @@ def _use_payment_microfrontend(request):
)


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('add_utm_params_to_url')
def add_utm_params_to_url(url, params):
# utm_params is [(u'utm_content', u'course-v1:IDBx IDB20.1x 1T2017'),...
utm_params = [item for item in params if 'utm_' in item[0]]
Expand All @@ -113,15 +113,15 @@ def add_utm_params_to_url(url, params):
return url


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('add_invalid_code_message_to_url')
def add_invalid_code_message_to_url(url, code):
if code:
message = 'error_message=Code {code} is invalid.'.format(code=str(code))
url += '&' + message if '?' in url else '?' + message
return url


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('prepare_basket')
def prepare_basket(request, products, voucher=None):
"""
Create or get the basket, add products, apply a voucher, and record referral data.
Expand Down Expand Up @@ -223,7 +223,7 @@ def prepare_basket(request, products, voucher=None):
return basket


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('get_basket_switch_data')
def get_basket_switch_data(product):
"""
Given a seat or enrollment product, find the SKU of the related product of
Expand Down Expand Up @@ -251,7 +251,7 @@ def get_basket_switch_data(product):
return switch_link_text, partner_sku


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_find_seat_enrollment_toggle_sku')
def _find_seat_enrollment_toggle_sku(product, target_structure):
"""
Given a seat or enrollment code product, find the SKU of the related product of
Expand Down Expand Up @@ -290,7 +290,7 @@ def _find_seat_enrollment_toggle_sku(product, target_structure):
return None


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('attribute_cookie_data')
def attribute_cookie_data(basket, request):
try:
with transaction.atomic():
Expand All @@ -314,7 +314,7 @@ def attribute_cookie_data(basket, request):
logger.exception('Error while attributing cookies to basket.')


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_referral_from_basket_site')
def _referral_from_basket_site(basket, site):
try:
# There should be only 1 referral instance for one basket.
Expand All @@ -325,7 +325,7 @@ def _referral_from_basket_site(basket, site):
return referral


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_record_affiliate_basket_attribution')
def _record_affiliate_basket_attribution(referral, request):
"""
Attribute this user's basket to the referring affiliate, if applicable.
Expand All @@ -339,7 +339,7 @@ def _record_affiliate_basket_attribution(referral, request):
referral.affiliate_id = affiliate_id


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_record_utm_basket_attribution')
def _record_utm_basket_attribution(referral, request):
"""
Attribute this user's basket to UTM data, if applicable.
Expand All @@ -363,7 +363,7 @@ def _record_utm_basket_attribution(referral, request):
referral.utm_created_at = created_at_datetime


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('basket_add_organization_attribute')
def basket_add_organization_attribute(basket, request_data):
"""
Adds the organization, and purchased_on_behalf attribute on basket, if organization value is provided
Expand Down Expand Up @@ -395,7 +395,7 @@ def basket_add_organization_attribute(basket, request_data):
)


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('basket_add_dynamic_payment_methods_enabled')
def basket_add_dynamic_payment_methods_enabled(basket, payment_intent):
"""
Adds a boolean value which is True if there is more than
Expand All @@ -413,7 +413,7 @@ def basket_add_dynamic_payment_methods_enabled(basket, payment_intent):
basket_attribute.save()


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('basket_add_payment_intent_id_attribute')
def basket_add_payment_intent_id_attribute(basket, payment_intent_id):
"""
Adds the Stripe payment_intent_id attribute on basket.
Expand All @@ -435,7 +435,7 @@ def basket_add_payment_intent_id_attribute(basket, payment_intent_id):
basket_attribute.save()


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('basket_add_enterprise_catalog_attribute')
def basket_add_enterprise_catalog_attribute(basket, request_data):
"""
Add enterprise catalog UUID attribute on basket, if the catalog UUID value
Expand Down Expand Up @@ -465,7 +465,7 @@ def basket_add_enterprise_catalog_attribute(basket, request_data):
BasketAttribute.objects.filter(basket=basket, attribute_type=enterprise_catalog_attribute).delete()


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_set_basket_bundle_status')
def _set_basket_bundle_status(bundle, basket):
"""
Sets the basket's bundle status
Expand Down Expand Up @@ -494,7 +494,7 @@ def _set_basket_bundle_status(bundle, basket):
BasketAttribute.objects.filter(basket=basket, attribute_type__name=BUNDLE).delete()


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('validate_voucher')
def validate_voucher(voucher, user, basket, request_site):
"""
Validates if a voucher code can be used for user and basket.
Expand Down Expand Up @@ -566,7 +566,7 @@ def apply_offers_on_basket(request, basket):
Applicator().apply(basket, request.user, request)


@newrelic.agent.function_trace()
@monitoring_utils.function_trace('apply_voucher_on_basket_and_check_discount')
def apply_voucher_on_basket_and_check_discount(voucher, request, basket):
"""
Applies voucher on a product.
Expand Down
26 changes: 13 additions & 13 deletions ecommerce/extensions/basket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from decimal import Decimal

import dateutil.parser
import newrelic.agent
import waffle
from django.http import HttpResponseBadRequest, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.utils.html import escape
from django.utils.translation import ugettext as _
from edx_django_utils import monitoring as monitoring_utils
from edx_rest_framework_extensions.permissions import LoginRedirectIfUnauthenticated
from opaque_keys.edx.keys import CourseKey
from oscar.apps.basket.signals import voucher_removal
Expand Down Expand Up @@ -98,7 +98,7 @@ class BasketLogicMixin:
Business logic for determining basket contents and checkout/payment options.
"""

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('process_basket_lines')
def process_basket_lines(self, lines):
"""
Processes the basket lines and extracts information for the view's context.
Expand Down Expand Up @@ -244,7 +244,7 @@ def _redirect_for_enterprise_data_sharing_consent(self, basket):
response=HttpResponseRedirect(redirect_url)
)

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_get_course_data')
def _get_course_data(self, product):
"""
Return course data.
Expand Down Expand Up @@ -294,7 +294,7 @@ def _get_course_data(self, product):

return course_data, course

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_get_order_details_message')
def _get_order_details_message(self, product):
if product.is_course_entitlement_product:
return _(
Expand Down Expand Up @@ -347,7 +347,7 @@ def _get_order_details_message(self, product):
else:
return None

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_set_single_enrollment_code_warning_if_needed')
def _set_single_enrollment_code_warning_if_needed(self, product, course):
assert product.is_enrollment_code_product

Expand Down Expand Up @@ -377,7 +377,7 @@ def _set_single_enrollment_code_warning_if_needed(self, product, course):
)
message_utils.add_message_data(message_code, 'course_about_url', course_about_url)

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_get_benefit_value')
def _get_benefit_value(self, line):
if line.has_discount:
applied_offer_values = list(self.request.basket.applied_offers().values())
Expand All @@ -386,22 +386,22 @@ def _get_benefit_value(self, line):
return format_benefit_value(benefit)
return None

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_get_certificate_type')
def _get_certificate_type(self, product):
if product.is_seat_product or product.is_course_entitlement_product:
return product.attr.certificate_type
elif product.is_enrollment_code_product:
return product.attr.seat_type
return None

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_get_certificate_type_display_value')
def _get_certificate_type_display_value(self, product):
certificate_type = self._get_certificate_type(product)
if certificate_type:
return get_certificate_type_display_value(certificate_type)
return None

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_deserialize_date')
def _deserialize_date(self, date_string):
try:
return dateutil.parser.parse(date_string)
Expand Down Expand Up @@ -507,12 +507,12 @@ def _redirect_response_to_basket_or_payment(self, request, invalid_code=None):


class BasketSummaryView(BasketLogicMixin, BasketView):
@newrelic.agent.function_trace()
@monitoring_utils.function_trace('get_context_data')
def get_context_data(self, **kwargs):
context = super(BasketSummaryView, self).get_context_data(**kwargs)
return self._add_to_context_data(context)

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('get')
def get(self, request, *args, **kwargs):
basket = request.basket

Expand Down Expand Up @@ -540,7 +540,7 @@ def _redirect_to_payment_microfrontend_if_configured(self, request):
redirect_response = HttpResponseRedirect(microfrontend_url)
raise RedirectException(response=redirect_response)

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_add_to_context_data')
def _add_to_context_data(self, context):
formset = context.get('formset', [])
lines = context.get('line_list', [])
Expand Down Expand Up @@ -577,7 +577,7 @@ def _add_to_context_data(self, context):
})
return context

@newrelic.agent.function_trace()
@monitoring_utils.function_trace('_get_payment_processors_data')
def _get_payment_processors_data(self, payment_processors):
"""Retrieve information about payment processors for the client side checkout basket.
Expand Down

0 comments on commit def0e61

Please sign in to comment.