askrobots / foxray Public

Foxray: a light error tracker for the Sentry SDKs. Runs inside Django (the admin is the UI) or on its own; SQLite or Postgres, nothing else. Not affiliated with Sentry.

0 downloads

foxray/tests/sdk_client.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/foxray.git
HTTPS
https://gitstable.com/git/askrobots/foxray.git
SSH
ssh://git@gitstable.com:2222/askrobots/foxray.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 0.1.2: the first PyPI release (v0.1.1 is already tagged at 05cc354, before the PyPI metadata) 58507e3 4 weeks ago History
sdk_client.py · 2.3 KB · 68 lines
1"""A real app using the official Sentry SDK, pointed at Foxray.
2
3Run as a separate process (so the SDK's global state is clean):
4 python tests/sdk_client.py <dsn>
5Prints a JSON object of the ids the SDK reported, on the last line."""
6
7import json
8import logging
9import sys
10import time
11
12import sentry_sdk
13from sentry_sdk.crons import capture_checkin, monitor
14
15
16def checkout(n):
17 return 10 / n
18
19
20def main(dsn: str) -> None:
21 import os
22 experiments = {}
23 if os.environ.get("SDK_COMPRESSION"): # "gzip" or "br" (the SDK picks br itself when brotli is installed)
24 experiments["transport_compression_algo"] = os.environ["SDK_COMPRESSION"]
25 sentry_sdk.init(dsn=dsn, traces_sample_rate=0, send_default_pii=False, release="shop@1.2.3",
26 environment="test", server_name="client-1", _experiments=experiments)
27 ids: dict = {"exc": []}
28 for _ in range(2):
29 try:
30 checkout(0)
31 except ZeroDivisionError:
32 ids["exc"].append(sentry_sdk.capture_exception())
33 ids["msg"] = sentry_sdk.capture_message("Payment provider slow: 1234 ms", level="warning")
34 with sentry_sdk.new_scope() as scope:
35 scope.set_user({"id": "42", "email": "secret@example.com"})
36 scope.set_tag("tenant", "acme")
37 scope.set_extra("api_token", "tok_live_abcdef")
38 try:
39 {}["missing"]
40 except KeyError:
41 ids["keyerror"] = sentry_sdk.capture_exception()
42 log = logging.getLogger("shop.db")
43 log.error("db timeout on shard %s", 3)
44 log.error("db timeout on shard %s", 7)
45
46 @monitor(monitor_slug="nightly-report", monitor_config={
47 "schedule": {"type": "crontab", "value": "0 3 * * *"}, "checkin_margin": 10, "max_runtime": 30,
48 "timezone": "America/Chicago"})
49 def nightly():
50 time.sleep(0.05)
51 nightly()
52
53 @monitor(monitor_slug="broken-job")
54 def broken():
55 raise RuntimeError("job failed")
56 try:
57 broken()
58 except RuntimeError:
59 ids["job_error"] = sentry_sdk.capture_exception()
60
61 ids["checkin"] = capture_checkin(monitor_slug="hourly-sync", status="ok", duration=1.5, monitor_config={
62 "schedule": {"type": "interval", "value": 1, "unit": "hour"}})
63 sentry_sdk.flush(timeout=10)
64 print(json.dumps(ids))
65
66
67if __name__ == "__main__":
68 main(sys.argv[1])