Skip to content

5.0: native exception and log capture - #47

Open
aflanagan wants to merge 18 commits into
masterfrom
claude/sentry-exception-ingestion-8v0m74
Open

aflanagan wants to merge 18 commits into
masterfrom
claude/sentry-exception-ingestion-8v0m74

Conversation

@aflanagan

Copy link
Copy Markdown
Collaborator

Summary

Companion to cronitorio/cronitor's Sentry-compatible exception and log ingestion. No new dependencies.

  • cronitor.capture_exception() and cronitor.capture_message() send Sentry-format events (chained exceptions, frames with five lines of source context, no local variables) to {cronitor.ingest_url}/api/envelope.
  • cronitor.logger.trace/debug/info/warning/error/fatal(body, **attributes) sends structured log records with templates and parameters; stdlib-style calls (%s args, warn, critical, exception, log) keep working. cronitor.LogHandler bridges the logging module (default level INFO).
  • @cronitor.job binds captures to the monitor, run series and environment through a ContextVar, and on failure sends the exception event after the fail ping (cronitor.capture_exceptions = False turns that off). Pings are now best effort: a telemetry outage no longer raises into the job.
  • cronitor.service / CRONITOR_SERVICE binds events and logs to a service monitor; cronitor.ingest_url / CRONITOR_INGEST_URL moves pings and envelopes to another host (dev); cronitor.release.
  • Transport: one daemon thread, bounded queue, gzip, 5 s timeout, log batching, fork-safe, drops while rate limited, 2 s flush at exit, events trimmed under the server's 1 MB cap. Nothing raises into user code.
  • Version 5.0.0: cronitor.logger changes meaning (it was the package's stdlib logger). README documents the change and the Sentry SDK DSN alternative.

Verification

  • python3 -m pytest cronitor/tests -k "not test_endpoints and not integration": 136 passed on 3.13; the fix agent also ran the suite on 3.8 through 3.12.
  • Exercised end to end against the dev stack: a failing @cronitor.job stored one event with mechanism cronitor.job and the same series as its pings, plus log records from cronitor.logger and LogHandler.
  • Independent review findings fixed on the branch: fork safety, logger compatibility, event size cap, attribute caps and handler level, rate-limit handling.

Generated by Claude Code

claude added 18 commits October 29, 2026 20:56
cronitor/ingest.py builds Sentry-format envelopes and sends them to
{ingest_url}/api/envelope from one daemon thread with a bounded queue.
Events go out one envelope each; log records batch to 100 or 5 seconds.
Bodies are gzipped, sends time out after 5 seconds with one retry, a 429
pauses sending for Retry-After (capped at 60 seconds), and failures are
logged, never raised. The worker starts lazily, restarts after fork, and
queued items are flushed at exit with a 2 second cap.

cronitor.ingest_url (env CRONITOR_INGEST_URL, default https://cronitor.link)
now also sets the ping URL base, so a dev stack can receive everything.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
cronitor/capture.py builds exception and message events and structured
log records. Exceptions follow __cause__ then __context__, oldest first,
with frames oldest first, five lines of source context from linecache,
and in_app false for site-packages, dist-packages and the stdlib. Local
variables are never sent. Values, messages and log bodies are truncated
to 4000/4000/8000 characters and tags and attributes to 50 entries.

cronitor.logger mirrors sentry_sdk.logger: the body is formatted with
str.format when attributes and braces are present, and the template and
parameters are recorded as sentry.message.* attributes. It replaces the
stdlib logger that was exported under that name; its configuration
methods still delegate to logging.getLogger('cronitor').

Monitor.capture_exception and Monitor.capture_message bind the monitor
key, environment and API key. New settings: cronitor.release (env
CRONITOR_RELEASE) and cronitor.capture_exceptions.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
The decorator binds the monitor key, series and environment in a context
variable while the function runs, so capture_exception, capture_message
and cronitor.logger calls inside it are tagged with cronitor.monitor and
cronitor.series. When the function raises, the decorator sends the fail
ping as before and then an exception event bound to the same series,
with mechanism cronitor.job and handled false. Set
cronitor.capture_exceptions = False to send only the ping.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
LogHandler ships logging records as structured log items through the
same batching transport. Levels map DEBUG->debug, INFO->info,
WARNING->warn, ERROR->error, CRITICAL->fatal, and anything below DEBUG
to trace. Records carry logger.name, code.file.path, code.line.number
and code.function.name, the %-style template and its arguments, and the
cronitor.monitor/cronitor.series binding inside a job. A record with
exc_info gets its formatted traceback in exception.stacktrace; no
second event is sent. Records from the transport itself are ignored so
a failing send cannot feed itself.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
Covers the requests session, gzip body and headers on the wire, a real
429 with Retry-After, and a ping routed through the same ingest_url.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
Adds an "Exceptions and Logs" README section: configuration,
capture_exception, capture_message, cronitor.logger, LogHandler, flush,
the @job behavior, and the Sentry SDK DSN alternative. Documents
CRONITOR_INGEST_URL and CRONITOR_RELEASE.

5.0.0 because @cronitor.job now sends an exception event on failure by
default and cronitor.logger is now the log shipper. A test keeps the SDK
version reported on the wire equal to setup.py.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
Envelope payloads are now strict JSON: NaN and Infinity in extra or log
attributes become strings, circular extra data is cut instead of losing
the event, unencodable dict keys are skipped, and lone surrogates are
replaced. A missing API key is logged once per transport instead of on
every record, which matters with LogHandler on a busy logger. The test
envelope parser now rejects NaN like a strict backend would.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
cronitor.app (or CRONITOR_APP) names the app monitor that exception
events and log records belong to. A cronitor.app tag or attribute on a
single call overrides it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
The monitor type that owns exceptions and logs is called service. The
SDK setting is cronitor.service (CRONITOR_SERVICE) and the tag and log
attribute are cronitor.service.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
A telemetry outage used to raise the ping's ConnectionError into the
decorated function before it ran. Pings are now best effort and logged.
The README points Sentry SDK users at a service monitor's dsn instead
of an API key id, and restores apply_config() in two examples.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
A child that inherited the transport reused the parent's keep-alive
session, so both processes wrote to the same TLS socket, and a pool
lock held by a parent thread at fork time could deadlock the child.
When the pid changes the session and the rate limit window are reset
along with the queue and the worker thread.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
Before 5.0 cronitor.logger was logging.getLogger('cronitor'), so code
like cronitor.logger.info('x %s', 1) now raised TypeError. The logger
takes %-style arguments (falling back to the raw body when they do not
format), and adds warn, critical, exception (an error log carrying the
handled exception's type, message and stack trace) and log(level, ...)
for stdlib level numbers and names. Configuration calls still pass
through to the stdlib logger.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
The server drops event items over 1 MB, and a deep exception chain with
source context or a large extra could exceed it. An event over 900 KB
now loses every frame's source context, then extra, then all but the
last 20 frames of each exception, stopping as soon as it fits, and
extra['_trimmed'] records what was dropped.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
Attribute keys are cut to 64 characters and values to 400, except the
exception stack trace and message and the message template, which keep
8,000 like the body; a record keeps at most 50 attributes. LogHandler
now defaults to INFO, so a root logger at DEBUG no longer ships every
library's debug lines; LogHandler(level=logging.DEBUG) opts in.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
After a 429 the sender thread slept until the limit lifted, so every
item queued behind it waited too and was lost when the 2 second exit
flush gave up. Envelopes sent during the limit window are now dropped,
counted in Transport.dropped and logged once per window.
X-Sentry-Rate-Limits is read before Retry-After, as Sentry's SDKs do.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B4Z4DjN96ccpNoVRZuoCxo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants