errorlens 0.2.0__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.
- errorlens-0.2.0/.gitignore +4 -0
- errorlens-0.2.0/LICENSE +21 -0
- errorlens-0.2.0/PKG-INFO +60 -0
- errorlens-0.2.0/README.md +40 -0
- errorlens-0.2.0/pyproject.toml +33 -0
- errorlens-0.2.0/src/errorlens/__init__.py +133 -0
- errorlens-0.2.0/src/errorlens/_version.py +1 -0
- errorlens-0.2.0/tests/test_sdk.py +109 -0
errorlens-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ErrorLens contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
errorlens-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: errorlens
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Python SDK for ErrorLens, the open-source, self-hosted error tracker.
|
|
5
|
+
Project-URL: Homepage, https://errorlens.io
|
|
6
|
+
Project-URL: Documentation, https://errorlens.io/docs/python/
|
|
7
|
+
Project-URL: Issues, https://github.com/errorlens-io/errorlens/issues
|
|
8
|
+
Project-URL: Source, https://github.com/errorlens-io/errorlens
|
|
9
|
+
Author: ErrorLens contributors
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: error tracking,errorlens,exceptions,monitoring,self-hosted
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Software Development :: Bug Tracking
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Requires-Dist: sentry-sdk<4,>=2.0
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# errorlens — Python SDK for ErrorLens
|
|
22
|
+
|
|
23
|
+
Report errors from Python apps to your own [ErrorLens](https://errorlens.io) server.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install errorlens
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import errorlens
|
|
31
|
+
|
|
32
|
+
errorlens.init(
|
|
33
|
+
dsn="https://<key>@errors.example.com/1", # from your ErrorLens project page
|
|
34
|
+
release="my-app@1.0.0",
|
|
35
|
+
environment="production",
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Unhandled exceptions are reported automatically. You can also report manually:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
try:
|
|
43
|
+
risky()
|
|
44
|
+
except Exception:
|
|
45
|
+
errorlens.capture_exception()
|
|
46
|
+
|
|
47
|
+
errorlens.capture_message("Payment provider slow", level="warning")
|
|
48
|
+
errorlens.set_user({"id": "42"})
|
|
49
|
+
errorlens.set_tag("region", "eu-west")
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Configuration can also come from environment variables: `ERRORLENS_DSN`,
|
|
53
|
+
`ERRORLENS_RELEASE`, `ERRORLENS_ENVIRONMENT`.
|
|
54
|
+
|
|
55
|
+
Privacy defaults: personal data (IP addresses, cookies, request bodies) is **not** sent unless
|
|
56
|
+
you pass `send_default_pii=True`, and your ErrorLens server scrubs secrets again before storage.
|
|
57
|
+
|
|
58
|
+
Django, Flask, FastAPI, Celery and other frameworks are detected automatically.
|
|
59
|
+
|
|
60
|
+
License: MIT.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# errorlens — Python SDK for ErrorLens
|
|
2
|
+
|
|
3
|
+
Report errors from Python apps to your own [ErrorLens](https://errorlens.io) server.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install errorlens
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import errorlens
|
|
11
|
+
|
|
12
|
+
errorlens.init(
|
|
13
|
+
dsn="https://<key>@errors.example.com/1", # from your ErrorLens project page
|
|
14
|
+
release="my-app@1.0.0",
|
|
15
|
+
environment="production",
|
|
16
|
+
)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Unhandled exceptions are reported automatically. You can also report manually:
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
try:
|
|
23
|
+
risky()
|
|
24
|
+
except Exception:
|
|
25
|
+
errorlens.capture_exception()
|
|
26
|
+
|
|
27
|
+
errorlens.capture_message("Payment provider slow", level="warning")
|
|
28
|
+
errorlens.set_user({"id": "42"})
|
|
29
|
+
errorlens.set_tag("region", "eu-west")
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Configuration can also come from environment variables: `ERRORLENS_DSN`,
|
|
33
|
+
`ERRORLENS_RELEASE`, `ERRORLENS_ENVIRONMENT`.
|
|
34
|
+
|
|
35
|
+
Privacy defaults: personal data (IP addresses, cookies, request bodies) is **not** sent unless
|
|
36
|
+
you pass `send_default_pii=True`, and your ErrorLens server scrubs secrets again before storage.
|
|
37
|
+
|
|
38
|
+
Django, Flask, FastAPI, Celery and other frameworks are detected automatically.
|
|
39
|
+
|
|
40
|
+
License: MIT.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "errorlens"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Python SDK for ErrorLens, the open-source, self-hosted error tracker."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [{ name = "ErrorLens contributors" }]
|
|
13
|
+
keywords = ["error tracking", "exceptions", "monitoring", "self-hosted", "errorlens"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Topic :: Software Development :: Bug Tracking",
|
|
19
|
+
]
|
|
20
|
+
# The transport/instrumentation layer is the MIT-licensed open-source error SDK.
|
|
21
|
+
dependencies = ["sentry-sdk>=2.0,<4"]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://errorlens.io"
|
|
25
|
+
Documentation = "https://errorlens.io/docs/python/"
|
|
26
|
+
Issues = "https://github.com/errorlens-io/errorlens/issues"
|
|
27
|
+
Source = "https://github.com/errorlens-io/errorlens"
|
|
28
|
+
|
|
29
|
+
[tool.hatch.version]
|
|
30
|
+
path = "src/errorlens/_version.py"
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.wheel]
|
|
33
|
+
packages = ["src/errorlens"]
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""ErrorLens Python SDK.
|
|
2
|
+
|
|
3
|
+
Reports errors to a self-hosted ErrorLens server. Event capture and transport
|
|
4
|
+
are provided by the MIT-licensed open-source error SDK this package builds on;
|
|
5
|
+
this module adds ErrorLens defaults (privacy-first, quiet console) and a
|
|
6
|
+
ErrorLens-branded API.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
from typing import Any, Callable, Optional
|
|
13
|
+
|
|
14
|
+
# The underlying library prints setup hints mentioning its own product to the
|
|
15
|
+
# console. ErrorLens users should never see those.
|
|
16
|
+
os.environ.setdefault("SENTRY_AGENT_HINTS", "0")
|
|
17
|
+
|
|
18
|
+
import sentry_sdk as _impl # noqa: E402
|
|
19
|
+
from sentry_sdk import consts as _consts # noqa: E402
|
|
20
|
+
|
|
21
|
+
from ._version import __version__ # noqa: E402
|
|
22
|
+
|
|
23
|
+
SDK_NAME = "errorlens.python"
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"__version__",
|
|
27
|
+
"init",
|
|
28
|
+
"is_enabled",
|
|
29
|
+
"capture_exception",
|
|
30
|
+
"capture_message",
|
|
31
|
+
"capture_event",
|
|
32
|
+
"add_breadcrumb",
|
|
33
|
+
"set_user",
|
|
34
|
+
"set_tag",
|
|
35
|
+
"set_tags",
|
|
36
|
+
"set_context",
|
|
37
|
+
"set_extra",
|
|
38
|
+
"flush",
|
|
39
|
+
"last_event_id",
|
|
40
|
+
"new_scope",
|
|
41
|
+
"isolation_scope",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
Event = dict
|
|
45
|
+
Hint = dict
|
|
46
|
+
BeforeSend = Callable[[Event, Hint], Optional[Event]]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _supported(option: str) -> bool:
|
|
50
|
+
defaults = getattr(_consts, "DEFAULT_OPTIONS", None)
|
|
51
|
+
return defaults is None or option in defaults
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _brand(user_hook: Optional[BeforeSend]) -> BeforeSend:
|
|
55
|
+
"""Wraps before_send so stored events identify this SDK as ErrorLens."""
|
|
56
|
+
|
|
57
|
+
def hook(event: Event, hint: Hint) -> Optional[Event]:
|
|
58
|
+
sdk = event.get("sdk") or {}
|
|
59
|
+
sdk["name"] = SDK_NAME
|
|
60
|
+
sdk["version"] = __version__
|
|
61
|
+
# Report our package plus the core library version under a neutral name.
|
|
62
|
+
sdk["packages"] = [
|
|
63
|
+
{"name": "pypi:errorlens", "version": __version__},
|
|
64
|
+
{"name": "pypi:core", "version": getattr(_impl, "VERSION", "")},
|
|
65
|
+
]
|
|
66
|
+
event["sdk"] = sdk
|
|
67
|
+
return user_hook(event, hint) if user_hook else event
|
|
68
|
+
|
|
69
|
+
return hook
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def init(
|
|
73
|
+
dsn: Optional[str] = None,
|
|
74
|
+
*,
|
|
75
|
+
release: Optional[str] = None,
|
|
76
|
+
environment: Optional[str] = None,
|
|
77
|
+
before_send: Optional[BeforeSend] = None,
|
|
78
|
+
**options: Any,
|
|
79
|
+
) -> None:
|
|
80
|
+
"""Start reporting errors to ErrorLens.
|
|
81
|
+
|
|
82
|
+
dsn, release and environment default to the ERRORLENS_DSN,
|
|
83
|
+
ERRORLENS_RELEASE and ERRORLENS_ENVIRONMENT environment variables. With no
|
|
84
|
+
DSN the SDK is a no-op, so it is safe to call in tests and local dev.
|
|
85
|
+
|
|
86
|
+
Any other keyword argument is passed to the underlying client; options it
|
|
87
|
+
does not support are ignored rather than raising.
|
|
88
|
+
"""
|
|
89
|
+
opts: dict = {
|
|
90
|
+
"dsn": dsn or os.environ.get("ERRORLENS_DSN") or None,
|
|
91
|
+
"release": release or os.environ.get("ERRORLENS_RELEASE") or None,
|
|
92
|
+
"environment": environment or os.environ.get("ERRORLENS_ENVIRONMENT") or None,
|
|
93
|
+
"before_send": _brand(before_send),
|
|
94
|
+
# Privacy-first defaults (callers may override).
|
|
95
|
+
"send_default_pii": False,
|
|
96
|
+
# Performance tracing is opt-in: pass traces_sample_rate to enable it.
|
|
97
|
+
"traces_sample_rate": None,
|
|
98
|
+
}
|
|
99
|
+
opts.update(options)
|
|
100
|
+
opts = {k: v for k, v in opts.items() if _supported(k)}
|
|
101
|
+
_impl.init(**opts)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def is_enabled() -> bool:
|
|
105
|
+
"""True if init() was called with a DSN."""
|
|
106
|
+
client = _impl.get_client()
|
|
107
|
+
return bool(client and client.is_active() and client.dsn)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _export(name: str):
|
|
111
|
+
fn = getattr(_impl, name, None)
|
|
112
|
+
if fn is None:
|
|
113
|
+
|
|
114
|
+
def missing(*_a: Any, **_k: Any) -> None:
|
|
115
|
+
raise NotImplementedError(f"errorlens.{name} is not available with the installed core library")
|
|
116
|
+
|
|
117
|
+
return missing
|
|
118
|
+
return fn
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
capture_exception = _export("capture_exception")
|
|
122
|
+
capture_message = _export("capture_message")
|
|
123
|
+
capture_event = _export("capture_event")
|
|
124
|
+
add_breadcrumb = _export("add_breadcrumb")
|
|
125
|
+
set_user = _export("set_user")
|
|
126
|
+
set_tag = _export("set_tag")
|
|
127
|
+
set_tags = _export("set_tags")
|
|
128
|
+
set_context = _export("set_context")
|
|
129
|
+
set_extra = _export("set_extra")
|
|
130
|
+
flush = _export("flush")
|
|
131
|
+
last_event_id = _export("last_event_id")
|
|
132
|
+
new_scope = _export("new_scope")
|
|
133
|
+
isolation_scope = _export("isolation_scope")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.0"
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Tests run the SDK against a tiny in-process HTTP server that records envelopes."""
|
|
2
|
+
|
|
3
|
+
import gzip
|
|
4
|
+
import http.server
|
|
5
|
+
import json
|
|
6
|
+
import threading
|
|
7
|
+
import unittest
|
|
8
|
+
|
|
9
|
+
import errorlens
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class _Recorder(http.server.BaseHTTPRequestHandler):
|
|
13
|
+
bodies: list = []
|
|
14
|
+
|
|
15
|
+
def do_POST(self): # noqa: N802
|
|
16
|
+
raw = self.rfile.read(int(self.headers.get("Content-Length", 0)))
|
|
17
|
+
if self.headers.get("Content-Encoding") == "gzip":
|
|
18
|
+
raw = gzip.decompress(raw)
|
|
19
|
+
_Recorder.bodies.append(raw)
|
|
20
|
+
self.send_response(200)
|
|
21
|
+
self.send_header("Content-Type", "application/json")
|
|
22
|
+
self.end_headers()
|
|
23
|
+
self.wfile.write(b"{}")
|
|
24
|
+
|
|
25
|
+
def log_message(self, *args):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _events():
|
|
30
|
+
out = []
|
|
31
|
+
for body in _Recorder.bodies:
|
|
32
|
+
lines = body.split(b"\n")
|
|
33
|
+
for i, line in enumerate(lines[1:], start=1):
|
|
34
|
+
try:
|
|
35
|
+
hdr = json.loads(line)
|
|
36
|
+
except ValueError:
|
|
37
|
+
continue
|
|
38
|
+
if isinstance(hdr, dict) and hdr.get("type") == "event" and i + 1 < len(lines):
|
|
39
|
+
out.append(json.loads(lines[i + 1]))
|
|
40
|
+
return out
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class SDKTest(unittest.TestCase):
|
|
44
|
+
@classmethod
|
|
45
|
+
def setUpClass(cls):
|
|
46
|
+
cls.server = http.server.HTTPServer(("127.0.0.1", 0), _Recorder)
|
|
47
|
+
threading.Thread(target=cls.server.serve_forever, daemon=True).start()
|
|
48
|
+
port = cls.server.server_address[1]
|
|
49
|
+
cls.dsn = f"http://{'a' * 32}@127.0.0.1:{port}/7"
|
|
50
|
+
|
|
51
|
+
@classmethod
|
|
52
|
+
def tearDownClass(cls):
|
|
53
|
+
cls.server.shutdown()
|
|
54
|
+
|
|
55
|
+
def setUp(self):
|
|
56
|
+
_Recorder.bodies.clear()
|
|
57
|
+
|
|
58
|
+
def test_no_dsn_is_noop(self):
|
|
59
|
+
errorlens.init(dsn=None)
|
|
60
|
+
self.assertFalse(errorlens.is_enabled())
|
|
61
|
+
errorlens.capture_message("ignored")
|
|
62
|
+
|
|
63
|
+
def test_reports_branded_event_with_privacy_defaults(self):
|
|
64
|
+
seen = []
|
|
65
|
+
|
|
66
|
+
def my_hook(event, hint):
|
|
67
|
+
seen.append(event["sdk"]["name"])
|
|
68
|
+
return event
|
|
69
|
+
|
|
70
|
+
errorlens.init(self.dsn, release="app@1.2.3", environment="test", before_send=my_hook, unknown_option=1)
|
|
71
|
+
self.assertTrue(errorlens.is_enabled())
|
|
72
|
+
errorlens.set_user({"id": "7"})
|
|
73
|
+
errorlens.set_tag("region", "eu")
|
|
74
|
+
try:
|
|
75
|
+
1 / 0
|
|
76
|
+
except ZeroDivisionError:
|
|
77
|
+
errorlens.capture_exception()
|
|
78
|
+
errorlens.flush(5)
|
|
79
|
+
|
|
80
|
+
events = _events()
|
|
81
|
+
self.assertEqual(len(events), 1)
|
|
82
|
+
ev = events[0]
|
|
83
|
+
self.assertEqual(ev["sdk"]["name"], "errorlens.python")
|
|
84
|
+
self.assertEqual(ev["sdk"]["version"], errorlens.__version__)
|
|
85
|
+
self.assertEqual(seen, ["errorlens.python"]) # user hook still runs
|
|
86
|
+
self.assertEqual(ev["release"], "app@1.2.3")
|
|
87
|
+
self.assertEqual(ev["environment"], "test")
|
|
88
|
+
self.assertEqual(ev["tags"]["region"], "eu")
|
|
89
|
+
self.assertEqual(ev["exception"]["values"][-1]["type"], "ZeroDivisionError")
|
|
90
|
+
self.assertNotIn("ip_address", ev.get("user", {}))
|
|
91
|
+
self.assertNotIn("sentry", json.dumps(ev["sdk"]).lower())
|
|
92
|
+
|
|
93
|
+
def test_env_vars(self):
|
|
94
|
+
import os
|
|
95
|
+
|
|
96
|
+
os.environ.update(ERRORLENS_DSN=self.dsn, ERRORLENS_RELEASE="env@9", ERRORLENS_ENVIRONMENT="ci")
|
|
97
|
+
try:
|
|
98
|
+
errorlens.init()
|
|
99
|
+
errorlens.capture_message("hello from env")
|
|
100
|
+
errorlens.flush(5)
|
|
101
|
+
finally:
|
|
102
|
+
for k in ("ERRORLENS_DSN", "ERRORLENS_RELEASE", "ERRORLENS_ENVIRONMENT"):
|
|
103
|
+
os.environ.pop(k, None)
|
|
104
|
+
ev = _events()[-1]
|
|
105
|
+
self.assertEqual((ev["release"], ev["environment"]), ("env@9", "ci"))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == "__main__":
|
|
109
|
+
unittest.main()
|