linearjeph 1.2.0__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.
linearjeph/health.py ADDED
@@ -0,0 +1,134 @@
1
+ """Health check registry. Register checks, expose them on an HTTP path, and LINEARJEPH monitors poll it.
2
+
3
+ from linearjeph import health
4
+ health.register("database", lambda: db_ping_ms(), warn=200, fail=1000) # a number is compared to the thresholds
5
+ health.register("cache", lambda: redis.ping()) # True/False
6
+ # Flask: health.flask_route(app, "/_lj/health", token="...") Django: path("_lj/health", health.django_view(token="..."))
7
+ # FastAPI/ASGI: app.mount("/_lj/health", health.asgi_app(token="...")) any WSGI: health.wsgi_app(token="...")
8
+
9
+ Then add a "SDK health check" monitor in the dashboard with that URL (and the same token). Response:
10
+ {"status": "ok|degraded|fail", "checks": {"database": {"status": "pass|warn|fail", "value": 12.5, "message": "..."}}}
11
+ HTTP 200 when ok or degraded, 503 when any check fails.
12
+ """
13
+ import hmac
14
+ import json
15
+ import threading
16
+ import time
17
+ from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeout
18
+
19
+ _checks = {}
20
+ _lock = threading.Lock()
21
+ _pool = ThreadPoolExecutor(max_workers=8, thread_name_prefix="linearjeph-health")
22
+ _outer = ThreadPoolExecutor(max_workers=4, thread_name_prefix="linearjeph-health-run") # separate: checks wait on _pool
23
+
24
+
25
+ def register(name, fn, warn=None, fail=None, timeout=5.0, below=False):
26
+ """Add or replace a check. `fn()` returns True/False, a number (value) or a dict {"status","value","message"}.
27
+ `warn`/`fail` thresholds apply to numbers (higher is worse; `below=True` flips it). Raising counts as fail."""
28
+ with _lock:
29
+ _checks[str(name)[:60]] = {"fn": fn, "warn": warn, "fail": fail, "timeout": float(timeout), "below": bool(below)}
30
+
31
+
32
+ def unregister(name):
33
+ with _lock:
34
+ _checks.pop(str(name), None)
35
+
36
+
37
+ def _grade(value, c):
38
+ worse = (lambda lim: value <= lim) if c["below"] else (lambda lim: value >= lim)
39
+ if c["fail"] is not None and worse(c["fail"]):
40
+ return "fail"
41
+ if c["warn"] is not None and worse(c["warn"]):
42
+ return "warn"
43
+ return "pass"
44
+
45
+
46
+ def _run_one(c):
47
+ t0 = time.perf_counter()
48
+ fut = _pool.submit(c["fn"])
49
+ try:
50
+ res = fut.result(timeout=c["timeout"])
51
+ except FutureTimeout:
52
+ return {"status": "fail", "message": "timed out after %gs" % c["timeout"]}
53
+ except Exception as exc: # noqa: BLE001
54
+ return {"status": "fail", "message": ("%s: %s" % (type(exc).__name__, exc))[:200]}
55
+ if isinstance(res, dict):
56
+ out = {k: res[k] for k in ("status", "value", "message") if k in res}
57
+ if out.get("status") not in ("pass", "warn", "fail"):
58
+ out["status"] = _grade(out["value"], c) if isinstance(out.get("value"), (int, float)) else "pass"
59
+ return out
60
+ if isinstance(res, bool) or res is None:
61
+ return {"status": "pass" if res is not False else "fail", "value": round((time.perf_counter() - t0) * 1000, 1)}
62
+ if isinstance(res, (int, float)):
63
+ return {"status": _grade(float(res), c), "value": float(res)}
64
+ return {"status": "pass", "message": str(res)[:200]}
65
+
66
+
67
+ def run_checks():
68
+ """Run every registered check (in parallel, each with its own timeout) and return the response body."""
69
+ with _lock:
70
+ items = list(_checks.items())
71
+ futs = {name: _outer.submit(_run_one, c) for name, c in items}
72
+ results = {}
73
+ for name, f in futs.items():
74
+ try:
75
+ results[name] = f.result(timeout=30)
76
+ except Exception: # noqa: BLE001
77
+ results[name] = {"status": "fail", "message": "check crashed"}
78
+ states = [r["status"] for r in results.values()]
79
+ top = "fail" if "fail" in states else ("degraded" if "warn" in states else "ok")
80
+ return {"status": top, "checks": results}
81
+
82
+
83
+ def respond(authorization=None, token=None):
84
+ """Framework-neutral: returns (http_status, body_dict). Pass the `Authorization` header and the expected token."""
85
+ if token and not hmac.compare_digest(str(authorization or ""), "Bearer " + token):
86
+ return 401, {"status": "unauthorized"}
87
+ body = run_checks()
88
+ return (503 if body["status"] == "fail" else 200), body
89
+
90
+
91
+ def wsgi_app(token=None):
92
+ def app(environ, start_response):
93
+ code, body = respond(environ.get("HTTP_AUTHORIZATION"), token)
94
+ data = json.dumps(body).encode()
95
+ start_response("%d %s" % (code, {200: "OK", 401: "Unauthorized", 503: "Service Unavailable"}[code]),
96
+ [("Content-Type", "application/json"), ("Content-Length", str(len(data))), ("Cache-Control", "no-store")])
97
+ return [data]
98
+ return app
99
+
100
+
101
+ def django_view(token=None):
102
+ def view(request):
103
+ from django.http import JsonResponse
104
+ code, body = respond(request.headers.get("Authorization"), token)
105
+ r = JsonResponse(body, status=code)
106
+ r["Cache-Control"] = "no-store"
107
+ return r
108
+ return view
109
+
110
+
111
+ def flask_route(app, path="/_lj/health", token=None):
112
+ from flask import jsonify, request
113
+
114
+ def _health():
115
+ code, body = respond(request.headers.get("Authorization"), token)
116
+ r = jsonify(body)
117
+ r.status_code = code
118
+ r.headers["Cache-Control"] = "no-store"
119
+ return r
120
+ app.add_url_rule(path, "linearjeph_health", _health)
121
+ return app
122
+
123
+
124
+ def asgi_app(token=None):
125
+ async def app(scope, receive, send):
126
+ if scope["type"] != "http":
127
+ return
128
+ auth = dict(scope.get("headers") or []).get(b"authorization", b"").decode("latin-1")
129
+ import asyncio
130
+ code, body = await asyncio.get_running_loop().run_in_executor(None, respond, auth, token)
131
+ data = json.dumps(body).encode()
132
+ await send({"type": "http.response.start", "status": code, "headers": [(b"content-type", b"application/json"), (b"cache-control", b"no-store")]})
133
+ await send({"type": "http.response.body", "body": data})
134
+ return app
File without changes
@@ -0,0 +1,41 @@
1
+ """Pure-ASGI middleware for FastAPI, Starlette and any ASGI app: `app.add_middleware(LinearjephASGIMiddleware)`."""
2
+ import time
3
+
4
+ from .. import client, telemetry
5
+
6
+
7
+ class LinearjephASGIMiddleware(object):
8
+ def __init__(self, app, ignore_paths=()):
9
+ self.app = app
10
+ self.ignore = tuple(ignore_paths or ())
11
+
12
+ async def __call__(self, scope, receive, send):
13
+ if scope["type"] != "http":
14
+ return await self.app(scope, receive, send)
15
+ t0, status = time.perf_counter(), {"code": 500}
16
+ tp = dict(scope.get("headers") or []).get(b"traceparent", b"").decode("latin-1")
17
+ sp = telemetry.start_span("request", "server", traceparent=tp or None)
18
+
19
+ async def _send(msg):
20
+ if msg["type"] == "http.response.start":
21
+ status["code"] = msg["status"]
22
+ await send(msg)
23
+
24
+ try:
25
+ await self.app(scope, receive, _send)
26
+ except Exception as exc:
27
+ client.capture_exception(exc)
28
+ raise
29
+ finally:
30
+ try:
31
+ path = scope.get("path", "/")
32
+ if not (self.ignore and path.startswith(self.ignore)):
33
+ route = scope.get("route") # FastAPI/Starlette set the matched route: low-cardinality names
34
+ name = getattr(route, "path", None) or path
35
+ telemetry.finish_span(sp, "ok" if status["code"] < 500 else "error", "%s %s" % (scope.get("method", "GET"), name))
36
+ client.capture_transaction("%s %s" % (scope.get("method", "GET"), name),
37
+ (time.perf_counter() - t0) * 1000,
38
+ "ok" if status["code"] < 500 else "error",
39
+ tags={"http.status_code": str(status["code"])})
40
+ except Exception: # noqa: BLE001
41
+ pass
@@ -0,0 +1,55 @@
1
+ """Django middleware: request timing (transactions) and unhandled view exceptions.
2
+
3
+ MIDDLEWARE = ["linearjeph.integrations.django.LinearjephMiddleware", ...] # near the top
4
+
5
+ Optional setting: LINEARJEPH_IGNORE_PATHS = ("/health", "/static/") (path prefixes that are not timed).
6
+ """
7
+ import time
8
+
9
+ from .. import client, telemetry
10
+
11
+
12
+ class LinearjephMiddleware(object):
13
+ sync_capable = True
14
+ async_capable = False
15
+
16
+ def __init__(self, get_response):
17
+ self.get_response = get_response
18
+ try:
19
+ from django.conf import settings
20
+ self.ignore = tuple(getattr(settings, "LINEARJEPH_IGNORE_PATHS", ()) or ())
21
+ except Exception: # noqa: BLE001
22
+ self.ignore = ()
23
+
24
+ def __call__(self, request):
25
+ t0 = time.perf_counter()
26
+ sp = telemetry.start_span("request", "server", traceparent=request.META.get("HTTP_TRACEPARENT"))
27
+ try:
28
+ response = self.get_response(request)
29
+ except Exception:
30
+ self._time(request, t0, 500, sp)
31
+ raise
32
+ self._time(request, t0, getattr(response, "status_code", 200), sp)
33
+ return response
34
+
35
+ def _time(self, request, t0, code, sp=None):
36
+ try:
37
+ if sp is not None:
38
+ m0 = getattr(request, "resolver_match", None)
39
+ telemetry.finish_span(sp, "ok" if code < 500 else "error", "%s %s" % (request.method, (getattr(m0, "route", None) or request.path)))
40
+ except Exception: # noqa: BLE001
41
+ pass
42
+ try:
43
+ if self.ignore and request.path.startswith(self.ignore):
44
+ return
45
+ m = getattr(request, "resolver_match", None)
46
+ name = (getattr(m, "route", None) or request.path) if m else request.path
47
+ name = name if name.startswith("/") else "/" + name
48
+ client.capture_transaction("%s %s" % (request.method, name), (time.perf_counter() - t0) * 1000,
49
+ "ok" if code < 500 else "error", tags={"http.status_code": str(code)})
50
+ except Exception: # noqa: BLE001
51
+ pass
52
+
53
+ def process_exception(self, request, exception):
54
+ client.capture_exception(exception)
55
+ return None
@@ -0,0 +1,38 @@
1
+ """Flask integration: `init_app(app)` after `linearjeph.init(...)`. Times requests and captures unhandled exceptions."""
2
+ import time
3
+
4
+ from .. import client, telemetry
5
+
6
+
7
+ def init_app(app, ignore_paths=()):
8
+ from flask import g, got_request_exception, request # imported lazily: Flask is the customer's dependency
9
+
10
+ ignore = tuple(ignore_paths or ())
11
+
12
+ @app.before_request
13
+ def _lj_start():
14
+ g._lj_t0 = time.perf_counter()
15
+ g._lj_span = telemetry.start_span("request", "server", traceparent=(getattr(request, "headers", None) or {}).get("traceparent"))
16
+
17
+ @app.after_request
18
+ def _lj_end(resp):
19
+ try:
20
+ t0 = getattr(g, "_lj_t0", None)
21
+ sp = getattr(g, "_lj_span", None)
22
+ if sp is not None:
23
+ telemetry.finish_span(sp, "ok" if resp.status_code < 500 else "error",
24
+ "%s %s" % (request.method, request.url_rule.rule if request.url_rule else request.path))
25
+ if t0 is not None and not (ignore and request.path.startswith(ignore)):
26
+ name = request.url_rule.rule if request.url_rule else request.path
27
+ client.capture_transaction("%s %s" % (request.method, name), (time.perf_counter() - t0) * 1000,
28
+ "ok" if resp.status_code < 500 else "error",
29
+ tags={"http.status_code": str(resp.status_code)})
30
+ except Exception: # noqa: BLE001
31
+ pass
32
+ return resp
33
+
34
+ def _lj_exc(sender, exception, **extra):
35
+ client.capture_exception(exception)
36
+
37
+ got_request_exception.connect(_lj_exc, app)
38
+ return app