|
| 1 | +import uuid |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from sentry_sdk._types import MYPY |
| 5 | +from sentry_sdk.utils import format_timestamp |
| 6 | + |
| 7 | +if MYPY: |
| 8 | + from typing import Optional |
| 9 | + from typing import Union |
| 10 | + from typing import Any |
| 11 | + from typing import Dict |
| 12 | + |
| 13 | + from sentry_sdk._types import SessionStatus |
| 14 | + |
| 15 | + |
| 16 | +def _minute_trunc(ts): |
| 17 | + # type: (datetime) -> datetime |
| 18 | + return ts.replace(second=0, microsecond=0) |
| 19 | + |
| 20 | + |
| 21 | +def _make_uuid( |
| 22 | + val, # type: Union[str, uuid.UUID] |
| 23 | +): |
| 24 | + # type: (...) -> uuid.UUID |
| 25 | + if isinstance(val, uuid.UUID): |
| 26 | + return val |
| 27 | + return uuid.UUID(val) |
| 28 | + |
| 29 | + |
| 30 | +class Session(object): |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + sid=None, # type: Optional[Union[str, uuid.UUID]] |
| 34 | + did=None, # type: Optional[str] |
| 35 | + timestamp=None, # type: Optional[datetime] |
| 36 | + started=None, # type: Optional[datetime] |
| 37 | + duration=None, # type: Optional[float] |
| 38 | + status=None, # type: Optional[SessionStatus] |
| 39 | + release=None, # type: Optional[str] |
| 40 | + environment=None, # type: Optional[str] |
| 41 | + user_agent=None, # type: Optional[str] |
| 42 | + ip_address=None, # type: Optional[str] |
| 43 | + errors=None, # type: Optional[int] |
| 44 | + user=None, # type: Optional[Any] |
| 45 | + ): |
| 46 | + # type: (...) -> None |
| 47 | + if sid is None: |
| 48 | + sid = uuid.uuid4() |
| 49 | + if started is None: |
| 50 | + started = datetime.utcnow() |
| 51 | + if status is None: |
| 52 | + status = "ok" |
| 53 | + self.status = status |
| 54 | + self.did = None # type: Optional[str] |
| 55 | + self.started = started |
| 56 | + self.release = None # type: Optional[str] |
| 57 | + self.environment = None # type: Optional[str] |
| 58 | + self.duration = None # type: Optional[float] |
| 59 | + self.user_agent = None # type: Optional[str] |
| 60 | + self.ip_address = None # type: Optional[str] |
| 61 | + self.errors = 0 |
| 62 | + |
| 63 | + self.update( |
| 64 | + sid=sid, |
| 65 | + did=did, |
| 66 | + timestamp=timestamp, |
| 67 | + duration=duration, |
| 68 | + release=release, |
| 69 | + environment=environment, |
| 70 | + user_agent=user_agent, |
| 71 | + ip_address=ip_address, |
| 72 | + errors=errors, |
| 73 | + user=user, |
| 74 | + ) |
| 75 | + |
| 76 | + @property |
| 77 | + def truncated_started(self): |
| 78 | + # type: (...) -> datetime |
| 79 | + return _minute_trunc(self.started) |
| 80 | + |
| 81 | + def update( |
| 82 | + self, |
| 83 | + sid=None, # type: Optional[Union[str, uuid.UUID]] |
| 84 | + did=None, # type: Optional[str] |
| 85 | + timestamp=None, # type: Optional[datetime] |
| 86 | + started=None, # type: Optional[datetime] |
| 87 | + duration=None, # type: Optional[float] |
| 88 | + status=None, # type: Optional[SessionStatus] |
| 89 | + release=None, # type: Optional[str] |
| 90 | + environment=None, # type: Optional[str] |
| 91 | + user_agent=None, # type: Optional[str] |
| 92 | + ip_address=None, # type: Optional[str] |
| 93 | + errors=None, # type: Optional[int] |
| 94 | + user=None, # type: Optional[Any] |
| 95 | + ): |
| 96 | + # type: (...) -> None |
| 97 | + # If a user is supplied we pull some data form it |
| 98 | + if user: |
| 99 | + if ip_address is None: |
| 100 | + ip_address = user.get("ip_address") |
| 101 | + if did is None: |
| 102 | + did = user.get("id") or user.get("email") or user.get("username") |
| 103 | + |
| 104 | + if sid is not None: |
| 105 | + self.sid = _make_uuid(sid) |
| 106 | + if did is not None: |
| 107 | + self.did = str(did) |
| 108 | + if timestamp is None: |
| 109 | + timestamp = datetime.utcnow() |
| 110 | + self.timestamp = timestamp |
| 111 | + if started is not None: |
| 112 | + self.started = started |
| 113 | + if duration is not None: |
| 114 | + self.duration = duration |
| 115 | + if release is not None: |
| 116 | + self.release = release |
| 117 | + if environment is not None: |
| 118 | + self.environment = environment |
| 119 | + if ip_address is not None: |
| 120 | + self.ip_address = ip_address |
| 121 | + if user_agent is not None: |
| 122 | + self.user_agent = user_agent |
| 123 | + if errors is not None: |
| 124 | + self.errors = errors |
| 125 | + |
| 126 | + if status is not None: |
| 127 | + self.status = status |
| 128 | + |
| 129 | + def close( |
| 130 | + self, status=None # type: Optional[SessionStatus] |
| 131 | + ): |
| 132 | + # type: (...) -> Any |
| 133 | + if status is None and self.status == "ok": |
| 134 | + status = "exited" |
| 135 | + if status is not None: |
| 136 | + self.update(status=status) |
| 137 | + |
| 138 | + def get_json_attrs( |
| 139 | + self, with_user_info=True # type: Optional[bool] |
| 140 | + ): |
| 141 | + # type: (...) -> Any |
| 142 | + attrs = {} |
| 143 | + if self.release is not None: |
| 144 | + attrs["release"] = self.release |
| 145 | + if self.environment is not None: |
| 146 | + attrs["environment"] = self.environment |
| 147 | + if with_user_info: |
| 148 | + if self.ip_address is not None: |
| 149 | + attrs["ip_address"] = self.ip_address |
| 150 | + if self.user_agent is not None: |
| 151 | + attrs["user_agent"] = self.user_agent |
| 152 | + return attrs |
| 153 | + |
| 154 | + def to_json(self): |
| 155 | + # type: (...) -> Any |
| 156 | + rv = { |
| 157 | + "sid": str(self.sid), |
| 158 | + "init": True, |
| 159 | + "started": format_timestamp(self.started), |
| 160 | + "timestamp": format_timestamp(self.timestamp), |
| 161 | + "status": self.status, |
| 162 | + } # type: Dict[str, Any] |
| 163 | + if self.errors: |
| 164 | + rv["errors"] = self.errors |
| 165 | + if self.did is not None: |
| 166 | + rv["did"] = self.did |
| 167 | + if self.duration is not None: |
| 168 | + rv["duration"] = self.duration |
| 169 | + attrs = self.get_json_attrs() |
| 170 | + if attrs: |
| 171 | + rv["attrs"] = attrs |
| 172 | + return rv |
0 commit comments