|
| 1 | +from functools import wraps |
| 2 | +import sys |
| 3 | +import uuid |
| 4 | + |
| 5 | +from sentry_sdk import Hub |
| 6 | +from sentry_sdk._compat import reraise |
| 7 | +from sentry_sdk._types import TYPE_CHECKING |
| 8 | +from sentry_sdk.utils import nanosecond_time |
| 9 | + |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from typing import Any, Callable, Dict, Optional |
| 13 | + |
| 14 | + |
| 15 | +class MonitorStatus: |
| 16 | + IN_PROGRESS = "in_progress" |
| 17 | + OK = "ok" |
| 18 | + ERROR = "error" |
| 19 | + |
| 20 | + |
| 21 | +def _create_checkin_event( |
| 22 | + monitor_slug=None, check_in_id=None, status=None, duration=None |
| 23 | +): |
| 24 | + # type: (Optional[str], Optional[str], Optional[str], Optional[float]) -> Dict[str, Any] |
| 25 | + options = Hub.current.client.options if Hub.current.client else {} |
| 26 | + check_in_id = check_in_id or uuid.uuid4().hex # type: str |
| 27 | + # convert nanosecond to millisecond |
| 28 | + duration = int(duration * 0.000001) if duration is not None else duration |
| 29 | + |
| 30 | + checkin = { |
| 31 | + "type": "check_in", |
| 32 | + "monitor_slug": monitor_slug, |
| 33 | + # TODO: Add schedule and schedule_type to monitor config |
| 34 | + # "monitor_config": { |
| 35 | + # "schedule": "*/10 0 0 0 0", |
| 36 | + # "schedule_type": "cron", |
| 37 | + # }, |
| 38 | + "check_in_id": check_in_id, |
| 39 | + "status": status, |
| 40 | + "duration": duration, |
| 41 | + "environment": options["environment"], |
| 42 | + "release": options["release"], |
| 43 | + } |
| 44 | + |
| 45 | + return checkin |
| 46 | + |
| 47 | + |
| 48 | +def capture_checkin(monitor_slug=None, check_in_id=None, status=None, duration=None): |
| 49 | + # type: (Optional[str], Optional[str], Optional[str], Optional[float]) -> str |
| 50 | + hub = Hub.current |
| 51 | + |
| 52 | + check_in_id = check_in_id or uuid.uuid4().hex |
| 53 | + checkin_event = _create_checkin_event( |
| 54 | + monitor_slug=monitor_slug, |
| 55 | + check_in_id=check_in_id, |
| 56 | + status=status, |
| 57 | + duration=duration, |
| 58 | + ) |
| 59 | + hub.capture_event(checkin_event) |
| 60 | + |
| 61 | + return checkin_event["check_in_id"] |
| 62 | + |
| 63 | + |
| 64 | +def monitor(monitor_slug=None, app=None): |
| 65 | + # type: (Optional[str], Any) -> Callable[..., Any] |
| 66 | + """ |
| 67 | + Decorator to capture checkin events for a monitor. |
| 68 | +
|
| 69 | + Usage: |
| 70 | + ``` |
| 71 | + import sentry_sdk |
| 72 | +
|
| 73 | + app = Celery() |
| 74 | +
|
| 75 | + @app.task |
| 76 | + @sentry_sdk.monitor(monitor_slug='my-fancy-slug') |
| 77 | + def test(arg): |
| 78 | + print(arg) |
| 79 | + ``` |
| 80 | +
|
| 81 | + This does not have to be used with Celery, but if you do use it with celery, |
| 82 | + put the `@sentry_sdk.monitor` decorator below Celery's `@app.task` decorator. |
| 83 | + """ |
| 84 | + |
| 85 | + def decorate(func): |
| 86 | + # type: (Callable[..., Any]) -> Callable[..., Any] |
| 87 | + if not monitor_slug: |
| 88 | + return func |
| 89 | + |
| 90 | + @wraps(func) |
| 91 | + def wrapper(*args, **kwargs): |
| 92 | + # type: (*Any, **Any) -> Any |
| 93 | + start_timestamp = nanosecond_time() |
| 94 | + check_in_id = capture_checkin( |
| 95 | + monitor_slug=monitor_slug, status=MonitorStatus.IN_PROGRESS |
| 96 | + ) |
| 97 | + |
| 98 | + try: |
| 99 | + result = func(*args, **kwargs) |
| 100 | + except Exception: |
| 101 | + duration = nanosecond_time() - start_timestamp |
| 102 | + capture_checkin( |
| 103 | + monitor_slug=monitor_slug, |
| 104 | + check_in_id=check_in_id, |
| 105 | + status=MonitorStatus.ERROR, |
| 106 | + duration=duration, |
| 107 | + ) |
| 108 | + exc_info = sys.exc_info() |
| 109 | + reraise(*exc_info) |
| 110 | + |
| 111 | + duration = nanosecond_time() - start_timestamp |
| 112 | + capture_checkin( |
| 113 | + monitor_slug=monitor_slug, |
| 114 | + check_in_id=check_in_id, |
| 115 | + status=MonitorStatus.OK, |
| 116 | + duration=duration, |
| 117 | + ) |
| 118 | + |
| 119 | + return result |
| 120 | + |
| 121 | + return wrapper |
| 122 | + |
| 123 | + return decorate |
0 commit comments