1
1
from __future__ import absolute_import
2
2
3
3
import sys
4
+ import time
4
5
5
6
from sentry_sdk .consts import OP
6
7
from sentry_sdk ._compat import reraise
15
16
capture_internal_exceptions ,
16
17
event_from_exception ,
17
18
logger ,
18
- now ,
19
19
)
20
20
21
21
if TYPE_CHECKING :
@@ -114,6 +114,16 @@ def sentry_build_tracer(name, task, *args, **kwargs):
114
114
ignore_logger ("celery.redirected" )
115
115
116
116
117
+ def _now_seconds_since_epoch ():
118
+ # type: () -> float
119
+ # We cannot use `time.perf_counter()` when dealing with the duration
120
+ # of a Celery task, because the start of a Celery task and
121
+ # the end are recorded in different processes.
122
+ # Start happens in the Celery Beat process,
123
+ # the end in a Celery Worker process.
124
+ return time .time ()
125
+
126
+
117
127
def _wrap_apply_async (f ):
118
128
# type: (F) -> F
119
129
@wraps (f )
@@ -130,7 +140,8 @@ def apply_async(*args, **kwargs):
130
140
if integration .monitor_beat_tasks :
131
141
headers .update (
132
142
{
133
- "sentry-monitor-start-timestamp-s" : "%.9f" % now (),
143
+ "sentry-monitor-start-timestamp-s" : "%.9f"
144
+ % _now_seconds_since_epoch (),
134
145
}
135
146
)
136
147
@@ -449,7 +460,7 @@ def crons_task_success(sender, **kwargs):
449
460
monitor_slug = headers ["sentry-monitor-slug" ],
450
461
monitor_config = monitor_config ,
451
462
check_in_id = headers ["sentry-monitor-check-in-id" ],
452
- duration = now () - start_timestamp_s ,
463
+ duration = _now_seconds_since_epoch () - start_timestamp_s ,
453
464
status = MonitorStatus .OK ,
454
465
)
455
466
@@ -470,7 +481,7 @@ def crons_task_failure(sender, **kwargs):
470
481
monitor_slug = headers ["sentry-monitor-slug" ],
471
482
monitor_config = monitor_config ,
472
483
check_in_id = headers ["sentry-monitor-check-in-id" ],
473
- duration = now () - start_timestamp_s ,
484
+ duration = _now_seconds_since_epoch () - start_timestamp_s ,
474
485
status = MonitorStatus .ERROR ,
475
486
)
476
487
@@ -491,6 +502,6 @@ def crons_task_retry(sender, **kwargs):
491
502
monitor_slug = headers ["sentry-monitor-slug" ],
492
503
monitor_config = monitor_config ,
493
504
check_in_id = headers ["sentry-monitor-check-in-id" ],
494
- duration = now () - start_timestamp_s ,
505
+ duration = _now_seconds_since_epoch () - start_timestamp_s ,
495
506
status = MonitorStatus .ERROR ,
496
507
)
0 commit comments