debugbundle-python 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
debugbundle/core.py CHANGED
@@ -2,9 +2,12 @@ from __future__ import annotations
2
2
 
3
3
  import asyncio
4
4
  import logging
5
+ import os
5
6
  import platform
7
+ import socket
6
8
  import sys
7
9
  import threading
10
+ import time
8
11
  import traceback
9
12
  import uuid
10
13
  from collections import deque
@@ -32,10 +35,16 @@ from .suppression import EventSuppressionTracker
32
35
  from .transport import HttpTransport, Transport, coerce_transport_response
33
36
  from .trigger_token import resolve_request_trigger_directives
34
37
 
38
+ try:
39
+ import resource
40
+ except ImportError: # pragma: no cover - resource is unavailable on some platforms.
41
+ resource = None # type: ignore[assignment]
42
+
35
43
  DEFAULT_BATCH_SIZE = 25
36
44
  DEFAULT_FLUSH_INTERVAL = 5.0
37
45
  DEFAULT_ENDPOINT = "https://api.debugbundle.com/v1/events"
38
46
  DEFAULT_LOG_LEVEL = "warning"
47
+ PROCESS_START_MONOTONIC = time.monotonic()
39
48
  SCHEMA_VERSION = "2026-03-01"
40
49
  LEVEL_RANKS = {
41
50
  "debug": 10,
@@ -44,6 +53,11 @@ LEVEL_RANKS = {
44
53
  "error": 40,
45
54
  "critical": 50,
46
55
  }
56
+ BALANCED_IMMEDIATE_REQUEST_STATUSES = {408, 423, 424, 425, 429}
57
+ INVESTIGATIVE_IMMEDIATE_REQUEST_STATUSES = BALANCED_IMMEDIATE_REQUEST_STATUSES | {409}
58
+ BALANCED_STANDARD_ANOMALY_STATUSES = {401, 403, 404, 409, 422}
59
+ BALANCED_HIGH_VOLUME_ANOMALY_STATUSES = {400, 410}
60
+ INVESTIGATIVE_ANOMALY_STATUSES = BALANCED_STANDARD_ANOMALY_STATUSES | BALANCED_HIGH_VOLUME_ANOMALY_STATUSES
47
61
 
48
62
 
49
63
  @dataclass
@@ -225,7 +239,7 @@ class DebugBundleSdk:
225
239
  "handled": handled,
226
240
  "request": request_payload,
227
241
  "response": response_payload,
228
- "runtime": {"version": platform.python_version()},
242
+ "runtime": _runtime_process_facts(),
229
243
  }
230
244
  if self._probe_flush_on_error:
231
245
  probe_data = self._build_probe_data()
@@ -595,7 +609,7 @@ class DebugBundleSdk:
595
609
  candidate = response.get("status_code") or response.get("response_status")
596
610
  if isinstance(candidate, int):
597
611
  status_code = candidate
598
- if status_code is not None and status_code >= 500:
612
+ if _is_immediate_request_incident_status(status_code, self._capture_policy.preset):
599
613
  return True
600
614
  if policy == "off":
601
615
  return False
@@ -606,7 +620,7 @@ class DebugBundleSdk:
606
620
  if status_code is None:
607
621
  return policy == "filtered"
608
622
  if policy == "failures_only":
609
- return False
623
+ return _is_request_anomaly_candidate_status(status_code, self._capture_policy.preset)
610
624
  if policy == "filtered":
611
625
  return False
612
626
  return True
@@ -699,6 +713,51 @@ def _redact_mapping(value: object, redact_fields: set[str]) -> Any:
699
713
  return value
700
714
 
701
715
 
716
+ def _runtime_process_facts() -> dict[str, object]:
717
+ return {
718
+ "version": platform.python_version(),
719
+ "platform": sys.platform,
720
+ "arch": platform.machine() or None,
721
+ "pid": os.getpid(),
722
+ "cwd": _safe_cwd(),
723
+ "uptime_sec": round(max(0.0, time.monotonic() - PROCESS_START_MONOTONIC), 3),
724
+ "hostname": _safe_hostname(),
725
+ "thread_id": threading.get_ident(),
726
+ "memory": _memory_facts(),
727
+ }
728
+
729
+
730
+ def _safe_cwd() -> str | None:
731
+ try:
732
+ return os.getcwd()
733
+ except OSError:
734
+ return None
735
+
736
+
737
+ def _safe_hostname() -> str | None:
738
+ try:
739
+ return socket.gethostname()
740
+ except OSError:
741
+ return None
742
+
743
+
744
+ def _memory_facts() -> dict[str, object]:
745
+ memory: dict[str, object] = {
746
+ "rss": None,
747
+ "heap_total": None,
748
+ "heap_used": None,
749
+ "external": None,
750
+ "peak": None,
751
+ }
752
+ if resource is None:
753
+ return memory
754
+
755
+ usage = resource.getrusage(resource.RUSAGE_SELF)
756
+ # ru_maxrss is KiB on Linux and bytes on macOS/BSD.
757
+ memory["peak"] = usage.ru_maxrss if sys.platform == "darwin" else usage.ru_maxrss * 1024
758
+ return memory
759
+
760
+
702
761
  def _backend_exception_request_payload(candidate: object | None) -> dict[str, object]:
703
762
  mapping = _dict_from_object(candidate)
704
763
  payload: dict[str, object] = {
@@ -792,6 +851,28 @@ def _iso_now(time_provider: Callable[[], float]) -> str:
792
851
  return datetime.fromtimestamp(time_provider(), tz=timezone.utc).isoformat().replace("+00:00", "Z")
793
852
 
794
853
 
854
+ def _is_immediate_request_incident_status(status_code: int | None, preset: str) -> bool:
855
+ if status_code is None:
856
+ return False
857
+ if status_code >= 500:
858
+ return True
859
+ if preset == "investigative":
860
+ return status_code in INVESTIGATIVE_IMMEDIATE_REQUEST_STATUSES
861
+ if preset == "balanced":
862
+ return status_code in BALANCED_IMMEDIATE_REQUEST_STATUSES
863
+ return False
864
+
865
+
866
+ def _is_request_anomaly_candidate_status(status_code: int | None, preset: str) -> bool:
867
+ if status_code is None or status_code < 400 or status_code >= 500:
868
+ return False
869
+ if preset == "investigative":
870
+ return status_code in INVESTIGATIVE_ANOMALY_STATUSES
871
+ if preset == "balanced":
872
+ return status_code in BALANCED_STANDARD_ANOMALY_STATUSES or status_code in BALANCED_HIGH_VOLUME_ANOMALY_STATUSES
873
+ return False
874
+
875
+
795
876
  def _time_now() -> float:
796
877
  return datetime.now(tz=timezone.utc).timestamp()
797
878
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: debugbundle-python
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: DebugBundle SDK for Python
5
5
  Author: DebugBundle
6
6
  License-Expression: AGPL-3.0-only
@@ -59,8 +59,28 @@ debugbundle.flush()
59
59
 
60
60
  ## Status
61
61
 
62
- This repository currently contains the full Phase 18 Python SDK scope in eleven implementation slices: core SDK surface, buffering, redaction, duplicate suppression, probe buffering, vanilla runtime hooks, framework integrations for Django, Flask, and FastAPI, remote config polling and capture-policy enforcement, optional `structlog` and `loguru` auto-detection when `capture_logging()` is enabled, contract-aligned `EventEnvelope` emission for log, request, exception, suppression, and probe payloads, explicit public wrapper signatures and a validated buildable typed package artifact, real HTTP integration coverage against a lightweight mock ingestion server, vendored machine-readable schema validation for all event types the Python SDK currently emits, a standalone CI workflow that validates Ruff, mypy, pytest, and package builds for the Python 3.10+ support floor actually used by the package, an enforced per-file coverage gate that keeps every shipped Python SDK module at or above the required 80% minimum, and request-local framework correlation binding so `X-DebugBundle-Trace-Id` flows through Django, Flask, and FastAPI into the emitted event correlation metadata for cross-context linking.
62
+ This repository currently contains the full Phase 18 Python SDK scope in eleven implementation slices: core SDK surface, buffering, redaction, duplicate suppression, probe buffering, vanilla runtime hooks, framework integrations for Django, Flask, and FastAPI, remote config polling and capture-policy enforcement, optional `structlog` and `loguru` auto-detection when `capture_logging()` is enabled, contract-aligned `EventEnvelope` emission for log, request, exception, suppression, and probe payloads, explicit public wrapper signatures and a validated buildable typed package artifact, real HTTP integration coverage against a lightweight mock ingestion server, vendored machine-readable schema validation for all event types the Python SDK currently emits, a standalone CI workflow that validates Ruff, mypy, pytest, and package builds for the Python 3.10+ support floor actually used by the package, an enforced per-file coverage gate that keeps every shipped Python SDK module at or above the required 80% minimum, request-local framework correlation binding so `X-DebugBundle-Trace-Id` flows through Django, Flask, and FastAPI into the emitted event correlation metadata for cross-context linking, and safe backend runtime process facts on exception payloads without reading environment variables.
63
+
64
+ ## Runtime Context
65
+
66
+ Backend exception events now include safe runtime process facts when the host exposes them, including:
67
+
68
+ - Python version
69
+ - platform
70
+ - architecture
71
+ - pid
72
+ - cwd
73
+ - uptime
74
+ - hostname
75
+ - thread id
76
+ - best-effort memory metadata
77
+
78
+ The SDK does not read or emit environment variables in this runtime block.
63
79
 
64
80
  ## Docs
65
81
 
66
82
  https://debugbundle.com/docs/sdk-python
83
+
84
+ ## License
85
+
86
+ AGPL-3.0-only
@@ -1,6 +1,6 @@
1
1
  debugbundle/__init__.py,sha256=mqibduUrdcr_fnb6k3Gwf93H7iqVBS8zd6SceX-U3Q4,5011
2
2
  debugbundle/config.py,sha256=2dLHEsh79mUaa6p7pkrQM849khwSBPg68Aq9RaeXf-8,6105
3
- debugbundle/core.py,sha256=WbPaZjfYvnaL-a2j38t_RiCuvXgY0zOZf3gA0qZojgY,32817
3
+ debugbundle/core.py,sha256=1_ZRoBwq3nSobJRopjlFqKfrfOtjkhgmlUB7fzo7Py4,35531
4
4
  debugbundle/logger_integrations.py,sha256=RuTNaD9RRVmiE-BBkksAXWVEGaMzLrWavVpQdgGZBpE,4564
5
5
  debugbundle/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  debugbundle/redaction.py,sha256=QTaPkSsYv54yR1mz8SB6Q4vWjvp7Ddcui4WXaH2RVz8,736
@@ -16,8 +16,8 @@ debugbundle/integrations/flask.py,sha256=cgyHbsAH96gpNPf_5IGJYzp2va28JdDgXROBsF-
16
16
  debugbundle/integrations/relay_django.py,sha256=_2TiH-oS0DOwThyfMJZ6ZJr5MkXEekEDaBUfPapMxt8,1523
17
17
  debugbundle/integrations/relay_fastapi.py,sha256=Z2spWy3wufVrBjYwRDDOL5Dsjgwcd5g72L23_sdulvo,1512
18
18
  debugbundle/integrations/relay_flask.py,sha256=WHk_ALOtT0xUuI1eINWFyLig9eRjSmIBpQ39xdkV608,1329
19
- debugbundle_python-0.1.1.dist-info/licenses/LICENSE,sha256=AKZZ5DQAHrOKGwt24VoRd-SXJLM9OloxlsX8ZgENdEY,735
20
- debugbundle_python-0.1.1.dist-info/METADATA,sha256=zl-nrFAiJBSsX9rKJI8pvTncCi3FU1i2pYFRn7kKLF4,3148
21
- debugbundle_python-0.1.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
22
- debugbundle_python-0.1.1.dist-info/top_level.txt,sha256=RCB9STTFnl1OKdojxz-xhaks2zkRFs1meZXsKnm18LM,12
23
- debugbundle_python-0.1.1.dist-info/RECORD,,
19
+ debugbundle_python-0.1.3.dist-info/licenses/LICENSE,sha256=AKZZ5DQAHrOKGwt24VoRd-SXJLM9OloxlsX8ZgENdEY,735
20
+ debugbundle_python-0.1.3.dist-info/METADATA,sha256=IcLUwzuq5mgPPqmmMVTrLX70Tl2ZVeGnK4kdSA_fxGA,3589
21
+ debugbundle_python-0.1.3.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
22
+ debugbundle_python-0.1.3.dist-info/top_level.txt,sha256=RCB9STTFnl1OKdojxz-xhaks2zkRFs1meZXsKnm18LM,12
23
+ debugbundle_python-0.1.3.dist-info/RECORD,,