From 378bad6253c70724c1bfb0efffbfefaaec235a4c Mon Sep 17 00:00:00 2001 From: Son Date: Thu, 30 Dec 2021 11:46:01 +0100 Subject: [PATCH] log "Custom/smtp_connection_time" metric in newrelic --- app/config.py | 7 +++++++ app/email_utils.py | 8 +++++++- email_handler.py | 8 +------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/config.py b/app/config.py index 575e5741..62851aa1 100644 --- a/app/config.py +++ b/app/config.py @@ -7,6 +7,7 @@ from ast import literal_eval from typing import Callable from urllib.parse import urlparse +import newrelic.agent from dotenv import load_dotenv SHA1 = subprocess.getoutput("git rev-parse HEAD") @@ -402,6 +403,12 @@ except Exception: HIBP_API_KEYS = sl_getenv("HIBP_API_KEYS", list) or [] NEWRELIC_CONFIG_PATH = os.environ.get("NEWRELIC_CONFIG_PATH") +newrelic_app = None +if NEWRELIC_CONFIG_PATH: + newrelic.agent.initialize(NEWRELIC_CONFIG_PATH) + + newrelic_app = newrelic.agent.register_application() + POSTMASTER = os.environ.get("POSTMASTER") diff --git a/app/email_utils.py b/app/email_utils.py index 6344047f..0cab7809 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -17,6 +17,7 @@ from typing import Tuple, List, Optional, Union import arrow import dkim +import newrelic.agent import re2 as re import spf from cachetools import cached, TTLCache @@ -53,6 +54,7 @@ from app.config import ( TEMP_DIR, ALIAS_AUTOMATIC_DISABLE, RSPAMD_SIGN_DKIM, + newrelic_app, ) from app.db import Session from app.dns_utils import get_mx_domains @@ -1307,7 +1309,11 @@ def sl_sendmail( if POSTFIX_SUBMISSION_TLS: smtp.starttls() - LOG.d("getting a smtp connection takes seconds %s", time.time() - start) + elapsed = time.time() - start + LOG.d("getting a smtp connection takes seconds %s", elapsed) + newrelic.agent.record_custom_metric( + "Custom/smtp_connection_time", elapsed, newrelic_app + ) # smtp.send_message has UnicodeEncodeError # encode message raw directly instead diff --git a/email_handler.py b/email_handler.py index 7fde9ecb..1ab62cfd 100644 --- a/email_handler.py +++ b/email_handler.py @@ -78,13 +78,13 @@ from app.config import ( TRANSACTIONAL_BOUNCE_SUFFIX, ENABLE_SPAM_ASSASSIN, BOUNCE_PREFIX_FOR_REPLY_PHASE, - NEWRELIC_CONFIG_PATH, POSTMASTER, ALERT_HOTMAIL_COMPLAINT, ALERT_YAHOO_COMPLAINT, ALERT_HOTMAIL_COMPLAINT_TRANSACTIONAL, ALERT_HOTMAIL_COMPLAINT_REPLY_PHASE, OLD_UNSUBSCRIBER, + newrelic_app, ) from app.db import Session from app.email import status, headers @@ -145,12 +145,6 @@ from app.utils import sanitize_email from init_app import load_pgp_public_keys from server import create_light_app -newrelic_app = None -if NEWRELIC_CONFIG_PATH: - newrelic.agent.initialize(NEWRELIC_CONFIG_PATH) - - newrelic_app = newrelic.agent.register_application() - def get_or_create_contact(from_header: str, mail_from: str, alias: Alias) -> Contact: """