trodo-python 3.0.3__tar.gz → 3.0.5__tar.gz

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.
Files changed (74) hide show
  1. {trodo_python-3.0.3 → trodo_python-3.0.5}/PKG-INFO +1 -1
  2. {trodo_python-3.0.3 → trodo_python-3.0.5}/pyproject.toml +1 -1
  3. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_sender.py +91 -0
  4. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/__init__.py +1 -1
  5. trodo_python-3.0.5/trodo/_version.py +1 -0
  6. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/delivery/sender.py +46 -14
  7. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo_python.egg-info/PKG-INFO +1 -1
  8. trodo_python-3.0.3/trodo/_version.py +0 -1
  9. {trodo_python-3.0.3 → trodo_python-3.0.5}/README.md +0 -0
  10. {trodo_python-3.0.3 → trodo_python-3.0.5}/setup.cfg +0 -0
  11. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_anon_distinct_id.py +0 -0
  12. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_auto_instrument_fixes.py +0 -0
  13. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_cross_process_session.py +0 -0
  14. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_datasets.py +0 -0
  15. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_end_run.py +0 -0
  16. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_end_run_order.py +0 -0
  17. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_error_enrichment.py +0 -0
  18. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_framework_deference.py +0 -0
  19. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_get_prompt_version_types.py +0 -0
  20. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_instrumentor_resolution.py +0 -0
  21. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_llm_usage_cost.py +0 -0
  22. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_long_runs.py +0 -0
  23. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_multi_agent_shape.py +0 -0
  24. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_processor_methods.py +0 -0
  25. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_prompt_fingerprint.py +0 -0
  26. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_prompt_trace.py +0 -0
  27. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_prompts.py +0 -0
  28. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_register_otel.py +0 -0
  29. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_start_run.py +0 -0
  30. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_user_traits.py +0 -0
  31. {trodo_python-3.0.3 → trodo_python-3.0.5}/tests/test_wrap_agent_unchanged.py +0 -0
  32. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/api/__init__.py +0 -0
  33. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/api/_retry.py +0 -0
  34. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/api/async_client.py +0 -0
  35. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/api/endpoints.py +0 -0
  36. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/api/http_client.py +0 -0
  37. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/api/management.py +0 -0
  38. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/auto/__init__.py +0 -0
  39. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/auto/auto_event_manager.py +0 -0
  40. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/client.py +0 -0
  41. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/delivery/__init__.py +0 -0
  42. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/managers/__init__.py +0 -0
  43. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/managers/dataset_manager.py +0 -0
  44. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/managers/group_manager.py +0 -0
  45. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/managers/people_manager.py +0 -0
  46. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/managers/prompt_manager.py +0 -0
  47. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/managers/user_manager.py +0 -0
  48. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/__init__.py +0 -0
  49. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/auto_instrument.py +0 -0
  50. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/context.py +0 -0
  51. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/helpers.py +0 -0
  52. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/processor.py +0 -0
  53. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/prompt_trace.py +0 -0
  54. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/register.py +0 -0
  55. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/transport.py +0 -0
  56. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/otel/wrap_agent.py +0 -0
  57. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/prompts/__init__.py +0 -0
  58. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/prompts/compile.py +0 -0
  59. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/prompts/template.py +0 -0
  60. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/prompts/types.py +0 -0
  61. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/queue/__init__.py +0 -0
  62. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/queue/batch_flusher.py +0 -0
  63. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/queue/event_queue.py +0 -0
  64. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/session/__init__.py +0 -0
  65. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/session/server_session.py +0 -0
  66. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/session/session_manager.py +0 -0
  67. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/types.py +0 -0
  68. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/user_context.py +0 -0
  69. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/util/__init__.py +0 -0
  70. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo/util/lru.py +0 -0
  71. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo_python.egg-info/SOURCES.txt +0 -0
  72. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo_python.egg-info/dependency_links.txt +0 -0
  73. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo_python.egg-info/requires.txt +0 -0
  74. {trodo_python-3.0.3 → trodo_python-3.0.5}/trodo_python.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trodo-python
3
- Version: 3.0.3
3
+ Version: 3.0.5
4
4
  Summary: Trodo Analytics SDK for Python — server-side event tracking
5
5
  License: ISC
6
6
  Keywords: analytics,tracking,trodo,server-side
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "trodo-python"
7
- version = "3.0.3"
7
+ version = "3.0.5"
8
8
  description = "Trodo Analytics SDK for Python — server-side event tracking"
9
9
  readme = "README.md"
10
10
  license = { text = "ISC" }
@@ -388,3 +388,94 @@ def test_feedback_waits_while_its_run_is_in_flight_in_another_request():
388
388
  assert sender.stats()["sent"] == 3
389
389
  by = {("fb" if "feedback" in c["items"][0] else c["items"][0]["run"]["run_id"]): c for c in s.calls}
390
390
  assert by["fb"]["start"] >= by["a"]["end"]
391
+
392
+
393
+
394
+ # ── Exit hand-off (3.0.4) ─────────────────────────────────────────────────────
395
+
396
+ def test_requests_at_exit_are_marked_exiting_and_others_are_not():
397
+ s = Server()
398
+ sender = Sender(s, **fast())
399
+ sender.enqueue(run("live"))
400
+ sender.flush(timeout_s=2)
401
+ sender.enqueue(run("late"))
402
+ sender._atexit()
403
+ assert "exiting" not in s.posts[0]["body"]
404
+ assert s.posts[1]["body"]["exiting"] is True
405
+ assert s.posts[1]["body"]["items"] == [run("late")]
406
+
407
+
408
+ def test_shutdown_marks_its_requests_exiting():
409
+ s = Server()
410
+ sender = Sender(s, **fast(flush_interval_s=60.0))
411
+ sender.enqueue(run("a"))
412
+ sender.shutdown()
413
+ assert s.posts[0]["body"]["exiting"] is True
414
+
415
+
416
+ def test_an_exit_hand_off_is_sent_and_not_called_rate_limiting(capsys):
417
+ handed = (200, None, {"results": [{"status": 202, "queued": True, "exit": True}]})
418
+ s = Server([handed])
419
+ sender = Sender(s, **fast())
420
+ sender.enqueue(run("a"))
421
+ sender._atexit()
422
+ assert sender.stats()["sent"] == 1
423
+ assert "rate limit" not in capsys.readouterr().err
424
+
425
+
426
+ # ── Exit beside slow requests (3.0.5) ─────────────────────────────────────────
427
+
428
+ class SlowLive:
429
+ """Ordinary requests take seconds (stored while they wait); exit hand-offs are instant."""
430
+
431
+ def __init__(self, live_s: float) -> None:
432
+ self.live_s = live_s
433
+ self.calls: List[Dict[str, Any]] = []
434
+ self.lock = threading.Lock()
435
+
436
+ def post_raw(self, path, body, timeout=0, gzip_body=True):
437
+ start = time.time()
438
+ if not body.get("exiting"):
439
+ time.sleep(self.live_s)
440
+ with self.lock:
441
+ self.calls.append({"start": start, "end": time.time(), "exiting": bool(body.get("exiting")), "n": len(body["items"])})
442
+ return 200, None, {"results": [{"status": 202 if body.get("exiting") else 200} for _ in body["items"]]}
443
+
444
+
445
+ def test_exit_hand_off_does_not_wait_behind_slow_requests(capsys):
446
+ s = SlowLive(1.5)
447
+ sender = Sender(s, **fast(flush_at=50, exit_flush_s=0.5))
448
+ for i in range(150):
449
+ sender.enqueue(run(f"live{i}"))
450
+ assert wait_for(lambda: sender.stats()["in_flight"] == 3)
451
+ for i in range(100):
452
+ sender.enqueue(run(f"late{i}"))
453
+ t0 = time.time()
454
+ sender._atexit()
455
+ assert time.time() - t0 < 0.7
456
+ handed = [c for c in s.calls if c["exiting"]]
457
+ assert sum(c["n"] for c in handed) == 100
458
+ err = capsys.readouterr().err
459
+ assert "NOT delivered" not in err
460
+ assert "150 trace item(s) were sent, but Trodo had not confirmed them" in err
461
+
462
+
463
+ def test_exit_warning_counts_items_not_requests(capsys):
464
+ s = SlowLive(1.5)
465
+ sender = Sender(s, **fast(flush_at=50, max_in_flight=1, exit_flush_s=0.2))
466
+ for i in range(50):
467
+ sender.enqueue(run(f"live{i}"))
468
+ assert wait_for(lambda: sender.stats()["in_flight"] == 1)
469
+ sender._paused_until = time.time() + 60 # nothing else can go out
470
+ for i in range(7):
471
+ sender.enqueue(run(f"late{i}"))
472
+ sender._atexit()
473
+ err = capsys.readouterr().err
474
+ assert "7 trace item(s), 14 units, were NOT delivered" in err
475
+ assert "50 more were sent, but Trodo had not confirmed them yet." in err
476
+
477
+
478
+ def test_version_matches_the_package():
479
+ import trodo
480
+ from trodo._version import SDK_VERSION
481
+ assert trodo.__version__ == SDK_VERSION
@@ -41,7 +41,7 @@ Downstream microservice (join the caller's run instead of making a new one):
41
41
 
42
42
  from __future__ import annotations
43
43
 
44
- __version__ = "2.26.2"
44
+ from ._version import SDK_VERSION as __version__
45
45
 
46
46
  from typing import Any, Callable, Dict, List, Optional, Union
47
47
 
@@ -0,0 +1 @@
1
+ SDK_VERSION = "3.0.5"
@@ -27,7 +27,14 @@ and PostHog have converged on (1 Oct 2026 survey):
27
27
  oversized item is dropped as ``too_large``; any other 4xx is a bad item:
28
28
  dropped as ``rejected``, reported once.
29
29
  - exit: ``atexit`` flushes (bounded by ``exit_flush_s``) and says on stderr
30
- how many traces were still undelivered, and why, if any were.
30
+ how many traces were still undelivered, and why, if any were. Requests
31
+ sent while exiting (or in ``shutdown``) are marked ``exiting`` (3.0.4):
32
+ Trodo takes the whole batch into its backlog at once instead of storing
33
+ each trace while the request waits, so a short script hands over
34
+ hundreds of traces in its last seconds. At exit the sender opens as many
35
+ requests again as ``max_in_flight`` (3.0.5): ordinary requests sent just
36
+ before the exit can take seconds each, and the hand-off must not wait
37
+ behind them.
31
38
  - the server holds over-rate items itself (202 ``queued``) and stores them
32
39
  at the team's rate; the sender says so on stderr once per 10 minutes.
33
40
  - ``immediate`` mode for platforms that freeze between requests (Lambda,
@@ -130,13 +137,16 @@ class Sender:
130
137
  self._not_before: float = 0.0 # backoff after a failed batch
131
138
  self._stopped = False
132
139
  self._flush_requested = False
133
- self._in_flight = 0
140
+ self._in_flight = 0 # requests
141
+ self._in_flight_items = 0
142
+ self._in_flight_units = 0
134
143
  self._stats = {"sent": 0, "dropped": 0, "dropped_by_reason": {}}
135
144
  self._unreported = {"count": 0, "by_reason": {}}
136
145
  self._warned_drop = False
137
146
  self._unit_cap: Optional[int] = None # the team's ceiling, once a 429 names it
138
147
  self._last_rate: Optional[Dict[str, Any]] = None
139
148
  self._warned_paced_at = 0.0
149
+ self._exiting = False # set by the exit flush and shutdown: the server holds the batch
140
150
  self._threads: List[threading.Thread] = []
141
151
  self._runs_in_flight: Dict[str, int] = {} # run_id -> requests carrying it
142
152
  if self.mode == "background" and register_atexit:
@@ -184,6 +194,7 @@ class Sender:
184
194
  self._flush_requested = False
185
195
 
186
196
  def shutdown(self, timeout_s: Optional[float] = None) -> None:
197
+ self._exiting = True
187
198
  self.flush(self._o["exit_flush_s"] if timeout_s is None else timeout_s)
188
199
  with self._lock:
189
200
  self._stopped = True
@@ -209,7 +220,10 @@ class Sender:
209
220
 
210
221
  def _ensure_thread_locked(self) -> None:
211
222
  self._threads = [t for t in self._threads if t.is_alive()]
212
- while len(self._threads) < max(1, int(self._o["max_in_flight"])):
223
+ slots = max(1, int(self._o["max_in_flight"]))
224
+ if self._exiting:
225
+ slots *= 2 # the hand-off goes out beside requests still in flight
226
+ while len(self._threads) < slots:
213
227
  t = threading.Thread(target=self._run, name=f"trodo-sender-{len(self._threads) + 1}", daemon=True)
214
228
  self._threads.append(t)
215
229
  t.start()
@@ -243,12 +257,17 @@ class Sender:
243
257
  runs = _run_ids(batch)
244
258
  for r in runs:
245
259
  self._runs_in_flight[r] = self._runs_in_flight.get(r, 0) + 1
260
+ units = sum(_units_of(q.item) for q in batch)
246
261
  self._in_flight += 1
262
+ self._in_flight_items += len(batch)
263
+ self._in_flight_units += units
247
264
  try:
248
265
  self._send_batch(batch)
249
266
  finally:
250
267
  with self._lock:
251
268
  self._in_flight -= 1
269
+ self._in_flight_items -= len(batch)
270
+ self._in_flight_units -= units
252
271
  for r in runs:
253
272
  left = self._runs_in_flight.get(r, 1) - 1
254
273
  if left > 0:
@@ -338,8 +357,8 @@ class Sender:
338
357
  first_error = first_error or f"HTTP {st}: {r.get('error', '')}"
339
358
  if rejected:
340
359
  self._drop(rejected, "rejected", f"{rejected} item(s) rejected by the server ({first_error})")
341
- if queued:
342
- self._warn_paced(body.get("paced") if isinstance(body.get("paced"), dict) else {})
360
+ if queued and isinstance(body.get("paced"), dict): # held for rate, not an exit hand-off
361
+ self._warn_paced(body["paced"])
343
362
  ra = body.get("retry_after_ms")
344
363
  if ra:
345
364
  self._pause(float(ra) / 1000.0)
@@ -470,11 +489,13 @@ class Sender:
470
489
  self._drop(n, "queue_full", f"the delivery queue is full ({self._o['max_queue_items']} items / {self._o['max_queue_bytes'] // 1048576} MB); the {n} oldest were dropped")
471
490
 
472
491
  def _envelope(self, batch: List[_Queued]) -> Dict[str, Any]:
473
- items = [q.item for q in batch]
492
+ env: Dict[str, Any] = {"items": [q.item for q in batch]}
493
+ if self._exiting:
494
+ env["exiting"] = True
474
495
  with self._lock:
475
- if not self._unreported["count"]:
476
- return {"items": items}
477
- return {"items": items, "dropped": {"count": self._unreported["count"], "by_reason": dict(self._unreported["by_reason"])}}
496
+ if self._unreported["count"]:
497
+ env["dropped"] = {"count": self._unreported["count"], "by_reason": dict(self._unreported["by_reason"])}
498
+ return env
478
499
 
479
500
  def _reported(self) -> None:
480
501
  with self._lock:
@@ -561,17 +582,19 @@ class Sender:
561
582
  self._sleep(wait)
562
583
 
563
584
  def _atexit(self) -> None:
585
+ self._exiting = True
564
586
  try:
565
587
  self.flush(self._o["exit_flush_s"])
566
588
  except Exception:
567
589
  pass
568
590
  with self._lock:
569
- left = len(self._queue) + self._in_flight
591
+ left = len(self._queue)
570
592
  units = sum(_units_of(q.item) for q in self._queue)
571
- if left:
572
- self._warn_undelivered(left, units)
593
+ unconfirmed = self._in_flight_items
594
+ if left or unconfirmed:
595
+ self._warn_undelivered(left, units, unconfirmed)
573
596
 
574
- def _warn_undelivered(self, items: int, units: int) -> None:
597
+ def _warn_undelivered(self, items: int, units: int, unconfirmed: int = 0) -> None:
575
598
  """Always on stderr, whatever was said before: data is about to be lost."""
576
599
  with self._lock:
577
600
  rate = self._last_rate if self._paused_until and self._paused_until > self._now() else None
@@ -581,9 +604,18 @@ class Sender:
581
604
  elif rate:
582
605
  c = rate.get("ceiling")
583
606
  why = f" The team is over its rate limit{f' ({c} units/min)' if c else ''} and Trodo could not hold more."
607
+ waited = f"(waited {self._o['exit_flush_s']:g} s)"
608
+ # Sent but unanswered: the request reached Trodo, which most likely stored it.
609
+ sent = f" {unconfirmed} more were sent, but Trodo had not confirmed them yet." if unconfirmed else ""
610
+ if not items:
611
+ sys.stderr.write(
612
+ f"[trodo-python] {unconfirmed} trace item(s) were sent, but Trodo had not confirmed them when the "
613
+ f"process exited {waited}. Call trodo.flush() before exiting to wait for the answer.\n"
614
+ )
615
+ return
584
616
  sys.stderr.write(
585
617
  f"[trodo-python] {items} trace item(s), {units} units, were NOT delivered before the process exited "
586
- f"(waited {self._o['exit_flush_s']:g} s).{why} Call trodo.flush() before exiting (it waits until they are "
618
+ f"{waited}.{sent}{why} Call trodo.flush() before exiting (it waits until they are "
587
619
  "delivered), or raise delivery_options exit_flush_s.\n"
588
620
  )
589
621
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trodo-python
3
- Version: 3.0.3
3
+ Version: 3.0.5
4
4
  Summary: Trodo Analytics SDK for Python — server-side event tracking
5
5
  License: ISC
6
6
  Keywords: analytics,tracking,trodo,server-side
@@ -1 +0,0 @@
1
- SDK_VERSION = "3.0.3"
File without changes
File without changes