getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.81k stars 475 forks source link

Hardcoded transaction name for ARQ cron jobs #3235

Open rombr opened 1 week ago

rombr commented 1 week ago

How do you use Sentry?

Self-hosted/on-premise

Version

2.5.1

Steps to Reproduce

1) demo.py

import sentry_sdk
from sentry_sdk.integrations.arq import ArqIntegration
from arq import cron

sentry_sdk.init(...)

async def task(ctx):
    1/0 # raises an error!

class WorkerSettings:
    functions = [task]
    cron_jobs = [cron(task, minute=0)]

2) Run a worker with arq demo.WorkerSettings

Expected Result

Expected transaction name in Sentry is task

Actual Result

But got unknown arq task because it hardcoded in the integration https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/arq.py#L103

transaction = Transaction(
    name="unknown arq task",
    status="ok",
    op=OP.QUEUE_TASK_ARQ,
    source=TRANSACTION_SOURCE_TASK,
    origin=ArqIntegration.origin,
)

with sentry_sdk.start_transaction(transaction):
    return await old_run_job(self, job_id, score)

But the task name can be extracted fron job_id

ipdb> job_id
'cron:task:1719844800123'
ipdb> job_id.split(":")
['cron', 'task', '1719844800123']
ipdb> job_id.split(":")[1]
'task'
szokeasaurusrex commented 1 week ago

Sounds like a good idea! Will place this suggestion on our backlog, but I would expect this to be a relatively easy change, so we would happily accept PRs if you'd like to contribute.

szokeasaurusrex commented 19 hours ago

Hi @rombr, I looked into this issue a bit more today and attempted to reproduce it; however, I was unable to reproduce the problem. I was getting the transaction name cron:transaction for the code you provided.

While you are correct that we initially set the transaction name to "unknown arq task," the transaction name should be overwritten in the event processor before we send the transaction event to Sentry:

https://github.com/getsentry/sentry-python/blob/855c15f49f845f67e528a9fa63e5d15121de1ab9/sentry_sdk/integrations/arq.py#L142-L144

Could you please provide more information on your setup/how you are initializing Sentry? Perhaps, something in your configuration is preventing this event processor from running as expected.