Add main sentry trace to email handler

This commit is contained in:
Adrià Casajús 2025-06-18 12:34:43 +02:00 committed by Adrià Casajús
parent 76f72a0b17
commit fc12e5204d

View file

@ -2383,7 +2383,6 @@ class MailHandler:
return status.E404 return status.E404
@newrelic.agent.background_task() @newrelic.agent.background_task()
@sentry_sdk.trace
def _handle(self, envelope: Envelope, msg: Message): def _handle(self, envelope: Envelope, msg: Message):
start = time.time() start = time.time()
@ -2403,32 +2402,35 @@ class MailHandler:
send_version_event("email_handler") send_version_event("email_handler")
with create_light_app().app_context(): with create_light_app().app_context():
return_status = handle(envelope, msg) with sentry_sdk.start_transaction(op="email-handler", name="Process email"):
elapsed = time.time() - start return_status = handle(envelope, msg)
# Only bounce messages if the return-path passes the spf check. Otherwise black-hole it. elapsed = time.time() - start
spamd_result = SpamdResult.extract_from_headers(msg) # Only bounce messages if the return-path passes the spf check. Otherwise black-hole it.
if return_status[0] == "5": spamd_result = SpamdResult.extract_from_headers(msg)
if spamd_result and spamd_result.spf in ( if return_status[0] == "5":
SPFCheckResult.fail, if spamd_result and spamd_result.spf in (
SPFCheckResult.soft_fail, SPFCheckResult.fail,
): SPFCheckResult.soft_fail,
LOG.i( ):
"Replacing 5XX to 216 status because the return-path failed the spf check" LOG.i(
) "Replacing 5XX to 216 status because the return-path failed the spf check"
return_status = status.E216 )
return_status = status.E216
LOG.i( LOG.i(
"Finish mail_from %s, rcpt_tos %s, takes %s seconds with return code '%s'<<===", "Finish mail_from %s, rcpt_tos %s, takes %s seconds with return code '%s'<<===",
envelope.mail_from, envelope.mail_from,
envelope.rcpt_tos, envelope.rcpt_tos,
elapsed, elapsed,
return_status, return_status,
) )
SpamdResult.send_to_new_relic(msg) SpamdResult.send_to_new_relic(msg)
newrelic.agent.record_custom_metric("Custom/email_handler_time", elapsed) newrelic.agent.record_custom_metric(
newrelic.agent.record_custom_metric("Custom/number_incoming_email", 1) "Custom/email_handler_time", elapsed
return return_status )
newrelic.agent.record_custom_metric("Custom/number_incoming_email", 1)
return return_status
def main(port: int): def main(port: int):