|
1 | 1 | from collections import deque |
2 | 2 | from dataclasses import dataclass |
3 | | -from pathlib import Path |
4 | 3 | from threading import current_thread |
5 | 4 | from types import FrameType |
6 | 5 | from types import TracebackType |
|
15 | 14 | from ddtrace.debugging._uploader import SignalUploader |
16 | 15 | from ddtrace.debugging._uploader import UploaderProduct |
17 | 16 | from ddtrace.internal import core |
| 17 | +from ddtrace.internal.compat import Path |
18 | 18 | from ddtrace.internal.logger import get_logger |
19 | 19 | from ddtrace.internal.packages import is_user_code |
20 | 20 | from ddtrace.internal.rate_limiter import BudgetRateLimiterWithJitter as RateLimiter |
@@ -255,49 +255,49 @@ def _attach_tb_frame_snapshot_to_span( |
255 | 255 | only_user_code: bool = True, |
256 | 256 | cached_only: bool = False, |
257 | 257 | ) -> bool: |
258 | | - frame = tb.tb_frame |
259 | | - code = frame.f_code |
260 | | - if only_user_code and not is_user_code(Path(code.co_filename)): |
261 | | - return False |
262 | | - |
263 | | - snapshot = None |
264 | | - snapshot_id = frame.f_locals.get(SNAPSHOT_KEY, None) |
265 | | - if snapshot_id is None: |
266 | | - # We don't have a snapshot for the frame so we create one |
267 | | - if cached_only: |
268 | | - # If we only want a cached snapshot we return True as a signal |
269 | | - # that we would have captured, but we actually skip generating |
270 | | - # a new snapshot. |
271 | | - return True |
272 | | - |
273 | | - snapshot = SpanExceptionSnapshot( |
274 | | - probe=SpanExceptionProbe.build(exc_id, frame), |
275 | | - frame=frame, |
276 | | - thread=current_thread(), |
277 | | - trace_context=span, |
278 | | - exc_id=exc_id, |
279 | | - ) |
| 258 | + try: |
| 259 | + frame = tb.tb_frame |
| 260 | + code = frame.f_code |
| 261 | + if only_user_code and not is_user_code(Path(code.co_filename)): |
| 262 | + return False |
280 | 263 |
|
281 | | - # Capture |
282 | | - try: |
| 264 | + snapshot = None |
| 265 | + snapshot_id = frame.f_locals.get(SNAPSHOT_KEY, None) |
| 266 | + if snapshot_id is None: |
| 267 | + # We don't have a snapshot for the frame so we create one |
| 268 | + if cached_only: |
| 269 | + # If we only want a cached snapshot we return True as a signal |
| 270 | + # that we would have captured, but we actually skip generating |
| 271 | + # a new snapshot. |
| 272 | + return True |
| 273 | + |
| 274 | + snapshot = SpanExceptionSnapshot( |
| 275 | + probe=SpanExceptionProbe.build(exc_id, frame), |
| 276 | + frame=frame, |
| 277 | + thread=current_thread(), |
| 278 | + trace_context=span, |
| 279 | + exc_id=exc_id, |
| 280 | + ) |
| 281 | + |
| 282 | + # Capture |
283 | 283 | snapshot.do_line() |
284 | | - except Exception: |
285 | | - log.exception("Error capturing exception replay snapshot %r", snapshot) |
286 | | - return False |
287 | 284 |
|
288 | | - # Collect |
289 | | - self.__uploader__.get_collector().push(snapshot) |
| 285 | + # Collect |
| 286 | + self.__uploader__.get_collector().push(snapshot) |
290 | 287 |
|
291 | | - # Memoize |
292 | | - frame.f_locals[SNAPSHOT_KEY] = snapshot_id = snapshot.uuid |
| 288 | + # Memoize |
| 289 | + frame.f_locals[SNAPSHOT_KEY] = snapshot_id = snapshot.uuid |
293 | 290 |
|
294 | | - # Add correlation tags on the span |
295 | | - span.set_tag_str(FRAME_SNAPSHOT_ID_TAG % seq_nr, snapshot_id) |
296 | | - span.set_tag_str(FRAME_FUNCTION_TAG % seq_nr, code.co_name) |
297 | | - span.set_tag_str(FRAME_FILE_TAG % seq_nr, code.co_filename) |
298 | | - span.set_tag_str(FRAME_LINE_TAG % seq_nr, str(tb.tb_lineno)) |
| 291 | + # Add correlation tags on the span |
| 292 | + span.set_tag_str(FRAME_SNAPSHOT_ID_TAG % seq_nr, snapshot_id) |
| 293 | + span.set_tag_str(FRAME_FUNCTION_TAG % seq_nr, code.co_name) |
| 294 | + span.set_tag_str(FRAME_FILE_TAG % seq_nr, code.co_filename) |
| 295 | + span.set_tag_str(FRAME_LINE_TAG % seq_nr, str(tb.tb_lineno)) |
299 | 296 |
|
300 | | - return snapshot is not None |
| 297 | + return snapshot is not None |
| 298 | + except Exception: # noqa: F841 |
| 299 | + log.exception("Error capturing exception replay snapshot") |
| 300 | + return False |
301 | 301 |
|
302 | 302 | def on_span_exception( |
303 | 303 | self, span: Span, _exc_type: t.Type[BaseException], exc: BaseException, traceback: t.Optional[TracebackType] |
|
0 commit comments