From 22f5934f342c86af2145973e5831e6b574b3801d Mon Sep 17 00:00:00 2001 From: Jake Moshenko Date: Wed, 5 Apr 2017 11:25:29 -0400 Subject: [PATCH] Add error logging to Marketo calls --- endpoints/api/user.py | 17 +++++++++++++---- endpoints/common.py | 12 ++++++++++-- endpoints/web.py | 4 +++- util/saas/useranalytics.py | 10 ++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/endpoints/api/user.py b/endpoints/api/user.py index 06e0250f7..e9afd30f0 100644 --- a/endpoints/api/user.py +++ b/endpoints/api/user.py @@ -35,6 +35,7 @@ from oauth.oidc import DiscoveryFailureException from util.useremails import (send_confirmation_email, send_recovery_email, send_change_email, send_password_changed, send_org_recovery_email) from util.names import parse_single_urn +from util.saas.useranalytics import build_error_callback REPOS_PER_PAGE = 100 @@ -345,14 +346,20 @@ class User(ApiResource): code = model.user.create_confirm_email_code(user, new_email=new_email) send_change_email(user.username, user_data['email'], code.code) else: - user_analytics.change_email(user.email, new_email) + ua_future = user_analytics.change_email(user.email, new_email) + ua_future.add_done_callback(build_error_callback('Change email failed')) model.user.update_email(user, new_email, auto_verify=not features.MAILING) if 'given_name' in user_data or 'family_name' in user_data or 'company' in user_data: model.user.update_user_metadata(user, user_data.get('given_name'), user_data.get('family_name'), user_data.get('company')) - user_analytics.change_metadata(user.email, user_data.get('given_name'), - user_data.get('family_name'), user_data.get('company')) + ua_mdata_future = user_analytics.change_metadata( + user.email, + user_data.get('given_name'), + user_data.get('family_name'), + user_data.get('company'), + ) + ua_mdata_future.add_done_callback(build_error_callback('Change metadata failed')) # Check for username rename. A username can be renamed if the feature is enabled OR the user # currently has a confirm_username prompt. @@ -370,7 +377,9 @@ class User(ApiResource): raise request_error(message='Username is already in use') user = model.user.change_username(user.id, new_username) - user_analytics.change_username(user.email, new_username) + username_future = user_analytics.change_username(user.email, new_username) + username_future.add_done_callback(build_error_callback('Change username failed')) + elif confirm_username: model.user.remove_user_prompt(user, 'confirm_username') diff --git a/endpoints/common.py b/endpoints/common.py index ee046f326..d6c895c37 100644 --- a/endpoints/common.py +++ b/endpoints/common.py @@ -23,6 +23,7 @@ from config import frontend_visible_config from external_libraries import get_external_javascript, get_external_css from util.names import parse_namespace_repository from util.secscan import PRIORITY_LEVELS +from util.saas.useranalytics import build_error_callback from util.timedeltastring import convert_to_timedelta @@ -122,8 +123,15 @@ def common_login(db_user, permanent_session=True): session.permanent_session_lifetime = convert_to_timedelta(session_timeout_str) # Inform our user analytics that we have a new "lead" - user_analytics.create_lead(db_user.email, db_user.username, db_user.given_name, - db_user.family_name, db_user.company) + create_lead_future = user_analytics.create_lead( + db_user.email, + db_user.username, + db_user.given_name, + db_user.family_name, + db_user.company, + ) + create_lead_future.add_done_callback(build_error_callback('Create lead failed')) + return True else: logger.debug('User could not be logged in, inactive?.') diff --git a/endpoints/web.py b/endpoints/web.py index b91e8242e..72027ea5c 100644 --- a/endpoints/web.py +++ b/endpoints/web.py @@ -34,6 +34,7 @@ from health.healthcheck import get_healthchecker from util.cache import no_cache from util.headers import parse_basic_auth from util.invoice import renderInvoiceToPdf +from util.saas.useranalytics import build_error_callback from util.systemlogs import build_logs_archive from util.useremails import send_email_changed @@ -407,7 +408,8 @@ def confirm_email(): if new_email: send_email_changed(user.username, old_email, new_email) - user_analytics.change_email(old_email, new_email) + change_email_future = user_analytics.change_email(old_email, new_email) + change_email_future.add_done_callback(build_error_callback('Change email failed')) common_login(user) if model.user.has_user_prompts(user): diff --git a/util/saas/useranalytics.py b/util/saas/useranalytics.py index 541a1a9bc..82b7bac1c 100644 --- a/util/saas/useranalytics.py +++ b/util/saas/useranalytics.py @@ -14,6 +14,16 @@ class LeadNotFoundException(Exception): pass +def build_error_callback(message_when_exception): + def maybe_log_error(response_future): + try: + response_future.result() + except Exception: + logger.exception('User analytics: %s', message_when_exception) + + return maybe_log_error + + class _MarketoAnalyticsClient(object): """ User analytics implementation which will report user changes to the Marketo API.