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/__init__.py +13 -0
- linearjeph/client.py +632 -0
- linearjeph/health.py +134 -0
- linearjeph/integrations/__init__.py +0 -0
- linearjeph/integrations/asgi.py +41 -0
- linearjeph/integrations/django.py +55 -0
- linearjeph/integrations/flask.py +38 -0
- linearjeph/prophecy/__init__.py +519 -0
- linearjeph/prophecy/__main__.py +67 -0
- linearjeph/prophecy/django.py +63 -0
- linearjeph/prophecy/middleware.py +151 -0
- linearjeph/prophecy/scanner.py +507 -0
- linearjeph/py.typed +0 -0
- linearjeph/telemetry.py +452 -0
- linearjeph-1.2.0.dist-info/METADATA +107 -0
- linearjeph-1.2.0.dist-info/RECORD +19 -0
- linearjeph-1.2.0.dist-info/WHEEL +5 -0
- linearjeph-1.2.0.dist-info/licenses/LICENSE +21 -0
- linearjeph-1.2.0.dist-info/top_level.txt +1 -0
linearjeph/telemetry.py
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
"""Metrics, structured logs, trace spans, cron check-ins and release/deploy markers.
|
|
2
|
+
|
|
3
|
+
Same rules as the core: nothing here raises into the host app, nothing blocks (everything goes onto the bounded sender
|
|
4
|
+
queue), and personal data is scrubbed on the client before anything leaves the process.
|
|
5
|
+
"""
|
|
6
|
+
import contextlib
|
|
7
|
+
import contextvars
|
|
8
|
+
import functools
|
|
9
|
+
import logging
|
|
10
|
+
import math
|
|
11
|
+
import os
|
|
12
|
+
import random
|
|
13
|
+
import re
|
|
14
|
+
import threading
|
|
15
|
+
import time
|
|
16
|
+
import uuid
|
|
17
|
+
from datetime import datetime, timezone
|
|
18
|
+
|
|
19
|
+
from . import client
|
|
20
|
+
from .client import (CHECKIN_PATH, DEPLOYS_PATH, LOGS_PATH, METRICS_PATH, RELEASES_PATH, SPANS_PATH, _safe, _state)
|
|
21
|
+
|
|
22
|
+
log = logging.getLogger("linearjeph")
|
|
23
|
+
|
|
24
|
+
NAME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_.:/-]{0,99}$")
|
|
25
|
+
MAX_TAGS = 10
|
|
26
|
+
RESERVOIR = 100
|
|
27
|
+
LEVELS = {"debug": 10, "info": 20, "warning": 30, "warn": 30, "error": 40, "critical": 50, "fatal": 50}
|
|
28
|
+
|
|
29
|
+
# ------------------------------------------------------------------ PII scrubbing (emails, cards, tokens)
|
|
30
|
+
|
|
31
|
+
_EMAIL = re.compile(r"[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9-]{1,63}(?:\.[A-Za-z0-9-]{1,63})+")
|
|
32
|
+
_CARD = re.compile(r"(?<![\d-])(?:\d[ -]?){12,18}\d(?![\d-])")
|
|
33
|
+
_JWT = re.compile(r"\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]*")
|
|
34
|
+
_PREFIXED = re.compile(r"\b(?:lj_[a-z]{2,5}_[A-Za-z0-9_-]{8,}|ljp_[A-Za-z0-9_-]{8,}|ljr_[A-Za-z0-9_-]{8,}|sk-[A-Za-z0-9_-]{16,}"
|
|
35
|
+
r"|AKIA[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AIza[0-9A-Za-z_-]{30,})\b")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _luhn(digits):
|
|
39
|
+
total, alt = 0, False
|
|
40
|
+
for ch in reversed(digits):
|
|
41
|
+
d = int(ch)
|
|
42
|
+
if alt:
|
|
43
|
+
d = d * 2 - 9 if d * 2 > 9 else d * 2
|
|
44
|
+
total += d
|
|
45
|
+
alt = not alt
|
|
46
|
+
return total % 10 == 0
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _card(m):
|
|
50
|
+
digits = re.sub(r"\D", "", m.group(0))
|
|
51
|
+
return "[card]" if 13 <= len(digits) <= 19 and _luhn(digits) else m.group(0)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def scrub_pii(text):
|
|
55
|
+
"""Replace emails, payment card numbers (Luhn-valid), JWTs, API keys and bearer tokens, plus `password=...` style text."""
|
|
56
|
+
text = client.scrub(str(text)) # password=..., Bearer ...
|
|
57
|
+
text = _JWT.sub("[token]", text)
|
|
58
|
+
text = _PREFIXED.sub("[token]", text)
|
|
59
|
+
text = _EMAIL.sub("[email]", text)
|
|
60
|
+
return _CARD.sub(_card, text)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _clean(text, n):
|
|
64
|
+
opts = _state["opts"]
|
|
65
|
+
t = str(text)[:n * 2]
|
|
66
|
+
if opts is None or opts.pii_scrub:
|
|
67
|
+
t = scrub_pii(t)
|
|
68
|
+
return t[:n]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _now_iso():
|
|
72
|
+
return datetime.now(timezone.utc).isoformat()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# ------------------------------------------------------------------ metrics
|
|
76
|
+
|
|
77
|
+
_mlock = threading.Lock()
|
|
78
|
+
_metrics = {}
|
|
79
|
+
_metric_drops = [0]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _mtags(tags):
|
|
83
|
+
out = {}
|
|
84
|
+
for k, v in list((tags or {}).items())[:MAX_TAGS]:
|
|
85
|
+
k = str(k)[:50]
|
|
86
|
+
out[k] = client.FILTERED if client._is_sensitive(k) else _clean(v, 100)
|
|
87
|
+
return out
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _record(typ, name, value, tags):
|
|
91
|
+
opts = _state["opts"]
|
|
92
|
+
if opts is None or _state["sender"] is None:
|
|
93
|
+
return
|
|
94
|
+
if not isinstance(name, str) or not NAME_RE.match(name):
|
|
95
|
+
return
|
|
96
|
+
v = float(value)
|
|
97
|
+
if not math.isfinite(v):
|
|
98
|
+
return
|
|
99
|
+
t = _mtags(tags)
|
|
100
|
+
_state["sender"]._ensure_thread() # the sender thread also flushes aggregates on its timer
|
|
101
|
+
key = (typ, name, tuple(sorted(t.items())))
|
|
102
|
+
with _mlock:
|
|
103
|
+
a = _metrics.get(key)
|
|
104
|
+
if a is None:
|
|
105
|
+
if len(_metrics) >= opts.max_metric_series:
|
|
106
|
+
_metric_drops[0] += 1 # bounded memory: new series beyond the cap are dropped and counted
|
|
107
|
+
return
|
|
108
|
+
a = _metrics[key] = {"tags": t, "count": 0, "sum": 0.0, "min": v, "max": v, "last": v, "values": []}
|
|
109
|
+
a["count"] += 1
|
|
110
|
+
a["sum"] += v
|
|
111
|
+
a["min"], a["max"], a["last"] = min(a["min"], v), max(a["max"], v), v
|
|
112
|
+
if typ == "histogram":
|
|
113
|
+
if len(a["values"]) < RESERVOIR:
|
|
114
|
+
a["values"].append(v)
|
|
115
|
+
else:
|
|
116
|
+
j = random.randrange(a["count"])
|
|
117
|
+
if j < RESERVOIR:
|
|
118
|
+
a["values"][j] = v
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@_safe
|
|
122
|
+
def incr(name, value=1, tags=None):
|
|
123
|
+
"""Counter: add `value` (default 1). Sent as one summed point per flush interval."""
|
|
124
|
+
_record("counter", name, value, tags)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@_safe
|
|
128
|
+
def gauge(name, value, tags=None):
|
|
129
|
+
"""Gauge: the latest value wins within a flush interval."""
|
|
130
|
+
_record("gauge", name, value, tags)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@_safe
|
|
134
|
+
def histogram(name, value, tags=None):
|
|
135
|
+
"""Histogram: count, sum, min, max and a bounded sample for p50/p95/p99."""
|
|
136
|
+
_record("histogram", name, value, tags)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class timer(contextlib.ContextDecorator):
|
|
140
|
+
"""`with linearjeph.timer("db.query", tags={"table": "users"}):` or `@linearjeph.timer("job")`. Records milliseconds."""
|
|
141
|
+
|
|
142
|
+
def __init__(self, name, tags=None):
|
|
143
|
+
self.name, self.tags, self._t0 = name, tags, None
|
|
144
|
+
|
|
145
|
+
def __enter__(self):
|
|
146
|
+
self._t0 = time.perf_counter()
|
|
147
|
+
return self
|
|
148
|
+
|
|
149
|
+
def __exit__(self, et, ev, tb):
|
|
150
|
+
try:
|
|
151
|
+
histogram(self.name, (time.perf_counter() - self._t0) * 1000.0, self.tags)
|
|
152
|
+
except Exception: # noqa: BLE001
|
|
153
|
+
pass
|
|
154
|
+
return False
|
|
155
|
+
|
|
156
|
+
def _recreate_cm(self):
|
|
157
|
+
return type(self)(self.name, self.tags)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def flush_metrics():
|
|
161
|
+
"""Move the in-process aggregates onto the sender queue. Called every `metrics_interval` s and by flush()/close()."""
|
|
162
|
+
s = _state["sender"]
|
|
163
|
+
with _mlock:
|
|
164
|
+
if not _metrics or s is None:
|
|
165
|
+
return 0
|
|
166
|
+
snap, _metrics_copy = dict(_metrics), None
|
|
167
|
+
_metrics.clear()
|
|
168
|
+
ts, items = _now_iso(), []
|
|
169
|
+
for (typ, name, _), a in snap.items():
|
|
170
|
+
it = {"name": name, "type": typ, "tags": a["tags"], "timestamp": ts}
|
|
171
|
+
if typ == "counter":
|
|
172
|
+
it["value"] = a["sum"]
|
|
173
|
+
elif typ == "gauge":
|
|
174
|
+
it["value"] = a["last"]
|
|
175
|
+
else:
|
|
176
|
+
it.update(count=a["count"], sum=a["sum"], min=a["min"], max=a["max"], values=a["values"])
|
|
177
|
+
items.append(it)
|
|
178
|
+
for i in range(0, len(items), 200):
|
|
179
|
+
s.put(METRICS_PATH, {"metrics": items[i:i + 200]})
|
|
180
|
+
return len(items)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def dropped_metric_series():
|
|
184
|
+
return _metric_drops[0]
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def attach(sender):
|
|
188
|
+
"""Called by init(): wire metric flushing into the sender thread and flush()."""
|
|
189
|
+
sender.on_tick = flush_metrics
|
|
190
|
+
sender.pre_flush = flush_metrics
|
|
191
|
+
with _mlock:
|
|
192
|
+
_metrics.clear()
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
# ------------------------------------------------------------------ structured logs
|
|
196
|
+
|
|
197
|
+
def _level_no(level):
|
|
198
|
+
if isinstance(level, int):
|
|
199
|
+
return level
|
|
200
|
+
return LEVELS.get(str(level).lower(), 20)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@_safe
|
|
204
|
+
def log_event(message, level="info", logger=None, **context):
|
|
205
|
+
"""Send one structured log line. `context` becomes searchable fields. Trace ids are added inside a span."""
|
|
206
|
+
opts, s = _state["opts"], _state["sender"]
|
|
207
|
+
if opts is None or s is None:
|
|
208
|
+
return
|
|
209
|
+
no = _level_no(level)
|
|
210
|
+
if no < _level_no(opts.log_level):
|
|
211
|
+
return
|
|
212
|
+
if opts.sample_logs < 1.0 and no < 40 and random.random() >= opts.sample_logs:
|
|
213
|
+
return
|
|
214
|
+
ctx = {}
|
|
215
|
+
for k, v in list(context.items())[:30]:
|
|
216
|
+
k = str(k)[:50]
|
|
217
|
+
ctx[k] = client.FILTERED if client._is_sensitive(k) else (v if isinstance(v, (int, float, bool)) else _clean(v, 300))
|
|
218
|
+
cur = _span_ctx.get()
|
|
219
|
+
item = {"timestamp": _now_iso(), "level": {10: "debug", 20: "info", 30: "warning", 40: "error", 50: "critical"}.get(no // 10 * 10, "info"),
|
|
220
|
+
"message": _clean(message, 4000), "logger": str(logger or "")[:100], "context": ctx,
|
|
221
|
+
"release": _state["release"] or "", "environment": _state["environment"]}
|
|
222
|
+
if cur is not None:
|
|
223
|
+
item["trace_id"], item["span_id"] = cur.trace_id, cur.span_id
|
|
224
|
+
if opts.before_send_log is not None:
|
|
225
|
+
item = opts.before_send_log(item) # inspect, edit or drop (return None) each log line before it is queued
|
|
226
|
+
if not isinstance(item, dict):
|
|
227
|
+
return
|
|
228
|
+
s.put(LOGS_PATH, item)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class LinearjephHandler(logging.Handler):
|
|
232
|
+
"""`logging` handler: `logging.getLogger().addHandler(linearjeph.LinearjephHandler())`. Extra fields become context."""
|
|
233
|
+
_STD = set(logging.LogRecord("", 0, "", 0, "", (), None).__dict__) | {"message", "asctime", "taskName"}
|
|
234
|
+
|
|
235
|
+
def emit(self, record):
|
|
236
|
+
try:
|
|
237
|
+
if record.name.split(".")[0] in ("linearjeph", "urllib3"):
|
|
238
|
+
return # never log our own transport (no feedback loop)
|
|
239
|
+
msg = record.getMessage()
|
|
240
|
+
if record.exc_info and record.exc_info[0] is not None:
|
|
241
|
+
import traceback
|
|
242
|
+
msg += "\n" + "".join(traceback.format_exception(*record.exc_info))[-1500:]
|
|
243
|
+
ctx = {k: v for k, v in record.__dict__.items() if k not in self._STD and not k.startswith("_")}
|
|
244
|
+
log_event(msg, record.levelno, record.name, **ctx)
|
|
245
|
+
except Exception: # noqa: BLE001
|
|
246
|
+
pass
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
_handlers = []
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def install_logging(level=logging.INFO, logger=None):
|
|
253
|
+
"""Attach a LinearjephHandler to `logger` (default: the root logger). Idempotent per logger. Returns the handler."""
|
|
254
|
+
target = logger or logging.getLogger()
|
|
255
|
+
for h, t in _handlers:
|
|
256
|
+
if t is target:
|
|
257
|
+
h.setLevel(level)
|
|
258
|
+
return h
|
|
259
|
+
h = LinearjephHandler(level)
|
|
260
|
+
target.addHandler(h)
|
|
261
|
+
_handlers.append((h, target))
|
|
262
|
+
return h
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
# ------------------------------------------------------------------ spans and tracing
|
|
266
|
+
|
|
267
|
+
_span_ctx = contextvars.ContextVar("linearjeph_span", default=None)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class Span(object):
|
|
271
|
+
def __init__(self, name, kind, trace_id, parent_id, sampled, tags):
|
|
272
|
+
self.name, self.kind, self.trace_id, self.parent_id, self.sampled = name, kind, trace_id, parent_id, sampled
|
|
273
|
+
self.span_id = uuid.uuid4().hex[:16]
|
|
274
|
+
self.tags = dict(tags or {})
|
|
275
|
+
self.status = "ok"
|
|
276
|
+
self.start = _now_iso()
|
|
277
|
+
self._t0 = time.perf_counter()
|
|
278
|
+
self._token = None
|
|
279
|
+
self.done = False
|
|
280
|
+
|
|
281
|
+
def set_tag(self, key, value):
|
|
282
|
+
self.tags[str(key)] = value
|
|
283
|
+
|
|
284
|
+
def traceparent(self):
|
|
285
|
+
return "00-%s-%s-%s" % (self.trace_id.rjust(32, "0"), self.span_id, "01" if self.sampled else "00")
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def current_span():
|
|
289
|
+
return _span_ctx.get()
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def parse_traceparent(value):
|
|
293
|
+
"""W3C `traceparent` header -> (trace_id, parent_span_id, sampled) or None."""
|
|
294
|
+
m = re.match(r"^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$", str(value or "").strip().lower())
|
|
295
|
+
return (m.group(1), m.group(2), bool(int(m.group(3), 16) & 1)) if m else None
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def start_span(name, kind="internal", tags=None, traceparent=None):
|
|
299
|
+
"""Begin a span and make it current. Pair with finish_span(). Prefer the `span()` context manager."""
|
|
300
|
+
try:
|
|
301
|
+
opts = _state["opts"]
|
|
302
|
+
parent = _span_ctx.get()
|
|
303
|
+
if parent is not None:
|
|
304
|
+
trace, pid, sampled = parent.trace_id, parent.span_id, parent.sampled
|
|
305
|
+
else:
|
|
306
|
+
tp = parse_traceparent(traceparent)
|
|
307
|
+
if tp:
|
|
308
|
+
trace, pid, sampled = tp
|
|
309
|
+
else:
|
|
310
|
+
trace, pid = uuid.uuid4().hex, ""
|
|
311
|
+
sampled = opts is not None and random.random() < opts.traces_sample_rate
|
|
312
|
+
sp = Span(str(name)[:200], kind, trace, pid, sampled, tags)
|
|
313
|
+
sp._token = _span_ctx.set(sp)
|
|
314
|
+
return sp
|
|
315
|
+
except Exception: # noqa: BLE001
|
|
316
|
+
return Span("noop", "internal", uuid.uuid4().hex, "", False, None)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def finish_span(sp, status=None, name=None):
|
|
320
|
+
try:
|
|
321
|
+
if sp.done:
|
|
322
|
+
return
|
|
323
|
+
sp.done = True
|
|
324
|
+
if name:
|
|
325
|
+
sp.name = str(name)[:200]
|
|
326
|
+
if status:
|
|
327
|
+
sp.status = status
|
|
328
|
+
if sp._token is not None:
|
|
329
|
+
try:
|
|
330
|
+
_span_ctx.reset(sp._token)
|
|
331
|
+
except ValueError: # finished from another context (async hand-off): just clear
|
|
332
|
+
_span_ctx.set(None)
|
|
333
|
+
s = _state["sender"]
|
|
334
|
+
if sp.sampled and s is not None:
|
|
335
|
+
s.put(SPANS_PATH, {"trace_id": sp.trace_id, "span_id": sp.span_id, "parent_id": sp.parent_id, "name": sp.name, "kind": sp.kind,
|
|
336
|
+
"start": sp.start, "duration_ms": (time.perf_counter() - sp._t0) * 1000.0, "status": sp.status,
|
|
337
|
+
"tags": _mtags(sp.tags), "release": _state["release"] or "", "environment": _state["environment"]})
|
|
338
|
+
except Exception: # noqa: BLE001
|
|
339
|
+
pass
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
class span(contextlib.ContextDecorator):
|
|
343
|
+
"""`with linearjeph.span("db.query", tags={...}) as sp:` or `@linearjeph.span("job")`. Marks the span failed on exceptions."""
|
|
344
|
+
|
|
345
|
+
def __init__(self, name, kind="internal", tags=None, traceparent=None):
|
|
346
|
+
self.name, self.kind, self.tags, self.traceparent = name, kind, tags, traceparent
|
|
347
|
+
self.sp = None
|
|
348
|
+
|
|
349
|
+
def __enter__(self):
|
|
350
|
+
self.sp = start_span(self.name, self.kind, self.tags, self.traceparent)
|
|
351
|
+
return self.sp
|
|
352
|
+
|
|
353
|
+
def __exit__(self, et, ev, tb):
|
|
354
|
+
finish_span(self.sp, "error" if et is not None else None)
|
|
355
|
+
return False
|
|
356
|
+
|
|
357
|
+
def _recreate_cm(self):
|
|
358
|
+
return type(self)(self.name, self.kind, self.tags, self.traceparent)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def inject_headers(headers):
|
|
362
|
+
"""Add a `traceparent` header for an outgoing call so the downstream service continues this trace."""
|
|
363
|
+
sp = _span_ctx.get()
|
|
364
|
+
if sp is not None:
|
|
365
|
+
headers["traceparent"] = sp.traceparent()
|
|
366
|
+
return headers
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
# ------------------------------------------------------------------ cron check-ins
|
|
370
|
+
|
|
371
|
+
@_safe
|
|
372
|
+
def checkin(name, status="ok", run_id=None, duration_ms=None, message=None, schedule=None, tz=None, grace_s=None,
|
|
373
|
+
interval_s=None, max_runtime_s=None):
|
|
374
|
+
"""Report a job run: status "start", "ok" or "fail". Send start then ok/fail with the same run_id to get the duration.
|
|
375
|
+
`schedule` (cron expression) with `grace_s` lets LINEARJEPH alert when a run is missed; `max_runtime_s` alerts on stuck runs."""
|
|
376
|
+
if not client.HEARTBEAT_NAME_RE.match(str(name)):
|
|
377
|
+
log.warning("linearjeph: check-in name must match [A-Za-z0-9._-]{1,64}; ignored")
|
|
378
|
+
return
|
|
379
|
+
body = {"monitor": str(name), "status": status if status in ("start", "ok", "fail") else "fail"}
|
|
380
|
+
for k, v in (("run_id", run_id), ("duration_ms", duration_ms), ("message", message and _clean(message, 300)), ("schedule", schedule),
|
|
381
|
+
("tz", tz), ("grace_s", grace_s), ("interval_s", interval_s), ("max_runtime_s", max_runtime_s)):
|
|
382
|
+
if v not in (None, ""):
|
|
383
|
+
body[k] = v
|
|
384
|
+
s = _state["sender"]
|
|
385
|
+
if s:
|
|
386
|
+
s.put(CHECKIN_PATH, body)
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
class monitor(contextlib.ContextDecorator):
|
|
390
|
+
"""Wrap a cron job or worker: sends start, then ok (or fail with the error) with the run duration. Re-raises errors.
|
|
391
|
+
|
|
392
|
+
@linearjeph.monitor("nightly-backup", schedule="0 2 * * *", grace_s=600)
|
|
393
|
+
def run(): ...
|
|
394
|
+
|
|
395
|
+
with linearjeph.monitor("sync", interval_s=900, capture=True): ...
|
|
396
|
+
|
|
397
|
+
`capture=True` also reports the exception as an error event.
|
|
398
|
+
"""
|
|
399
|
+
|
|
400
|
+
def __init__(self, name, interval_s=None, capture=False, schedule=None, tz=None, grace_s=None, max_runtime_s=None):
|
|
401
|
+
self.name, self.capture = name, capture
|
|
402
|
+
self.opts = dict(interval_s=interval_s, schedule=schedule, tz=tz, grace_s=grace_s, max_runtime_s=max_runtime_s)
|
|
403
|
+
self.run_id = self._t0 = None
|
|
404
|
+
|
|
405
|
+
def __enter__(self):
|
|
406
|
+
self.run_id, self._t0 = uuid.uuid4().hex, time.perf_counter()
|
|
407
|
+
checkin(self.name, "start", run_id=self.run_id, **self.opts)
|
|
408
|
+
return self
|
|
409
|
+
|
|
410
|
+
def __exit__(self, et, ev, tb):
|
|
411
|
+
try:
|
|
412
|
+
if et is not None and self.capture and not issubclass(et, (SystemExit, KeyboardInterrupt)):
|
|
413
|
+
client.capture_exception((et, ev, tb))
|
|
414
|
+
dur = (time.perf_counter() - self._t0) * 1000.0
|
|
415
|
+
failed = et is not None and not (issubclass(et, SystemExit) and not ev.code)
|
|
416
|
+
checkin(self.name, "fail" if failed else "ok", run_id=self.run_id, duration_ms=dur,
|
|
417
|
+
message=("%s: %s" % (et.__name__, ev)) if failed else None, **self.opts)
|
|
418
|
+
except Exception: # noqa: BLE001
|
|
419
|
+
pass
|
|
420
|
+
return False
|
|
421
|
+
|
|
422
|
+
def _recreate_cm(self):
|
|
423
|
+
return type(self)(self.name, self.opts["interval_s"], self.capture, self.opts["schedule"], self.opts["tz"],
|
|
424
|
+
self.opts["grace_s"], self.opts["max_runtime_s"])
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
# ------------------------------------------------------------------ releases and deploys
|
|
428
|
+
|
|
429
|
+
def _release_version(version):
|
|
430
|
+
return version or _state["release"] or os.environ.get("LINEARJEPH_RELEASE") or ""
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
@_safe
|
|
434
|
+
def release(version=None, commit=None, notes=None):
|
|
435
|
+
"""Record a release marker (shown on charts; errors are linked to the release that introduced them)."""
|
|
436
|
+
v = _release_version(version)
|
|
437
|
+
s = _state["sender"]
|
|
438
|
+
if s and v:
|
|
439
|
+
s.put(RELEASES_PATH, {k: x for k, x in (("version", v), ("commit", commit), ("notes", notes)) if x})
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
@_safe
|
|
443
|
+
def deploy(version=None, environment=None, status="success", url=None, commit=None, notify=False):
|
|
444
|
+
"""Record a deploy of `version` (default: the release passed to init). `notify=True` also tells your alert channels."""
|
|
445
|
+
v = _release_version(version)
|
|
446
|
+
s = _state["sender"]
|
|
447
|
+
if s and v:
|
|
448
|
+
body = {"version": v, "environment": environment or _state["environment"], "status": status, "notify": bool(notify)}
|
|
449
|
+
for k, x in (("url", url), ("commit", commit)):
|
|
450
|
+
if x:
|
|
451
|
+
body[k] = x
|
|
452
|
+
s.put(DEPLOYS_PATH, body)
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: linearjeph
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: Revenue forecasting and growth analytics (Prophecy: MRR/ARR projection, churn, feature impact) plus error tracking, performance, logs and cron monitoring for Python apps. Zero dependencies.
|
|
5
|
+
Author-email: LINEARJEPH <support@linearjeph.cc>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://linearjeph.cc/prophecy
|
|
8
|
+
Project-URL: Documentation, https://linearjeph.cc/docs/sdk/python
|
|
9
|
+
Project-URL: Prophecy guide, https://linearjeph.cc/docs/sdk/prophecy
|
|
10
|
+
Project-URL: Source, https://github.com/kingtechies/linearjeph-sdk/tree/main/python
|
|
11
|
+
Project-URL: Changelog, https://github.com/kingtechies/linearjeph-sdk/blob/main/python/CHANGELOG.md
|
|
12
|
+
Keywords: revenue-forecasting,mrr,arr,saas-metrics,churn,cohort-analysis,product-analytics,growth-analytics,feature-analytics,forecasting,error-tracking,apm,monitoring,django,flask,fastapi,prophecy,linearjeph
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
25
|
+
Classifier: Framework :: Django
|
|
26
|
+
Classifier: Framework :: Flask
|
|
27
|
+
Classifier: Framework :: FastAPI
|
|
28
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
29
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
30
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware
|
|
31
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
32
|
+
Classifier: Topic :: System :: Logging
|
|
33
|
+
Classifier: Topic :: System :: Monitoring
|
|
34
|
+
Classifier: Typing :: Typed
|
|
35
|
+
Requires-Python: >=3.8
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
License-File: LICENSE
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# linearjeph: revenue forecasting, growth analytics and monitoring for Python apps
|
|
41
|
+
|
|
42
|
+
The official Python SDK for [LINEARJEPH](https://linearjeph.cc). One package, no runtime dependencies, Python 3.8+.
|
|
43
|
+
|
|
44
|
+
- **Prophecy** (growth intelligence): track signups, payments, subscriptions and feature use, scan your codebase locally,
|
|
45
|
+
import your history, and get a 12-month **MRR/ARR forecast with P10-P90 ranges**, churn and cohort retention, LTV,
|
|
46
|
+
feature impact (which features earn and which are dead weight), API health, security checks, an indicative valuation
|
|
47
|
+
and a ranked 90-day plan. Statistics and simulation, not guesswork. [See a sample report](https://linearjeph.cc/prophecy).
|
|
48
|
+
- **Maintenance**: error tracking, performance timing, logs, cron heartbeats and dependency reporting.
|
|
49
|
+
- Works with Django, Flask, FastAPI/Starlette and any WSGI or ASGI app.
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
pip install linearjeph
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Prophecy quick start
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from linearjeph import prophecy
|
|
59
|
+
|
|
60
|
+
prophecy.init(ingest_key="lj_app_YOUR_KEY", secret_key="ljp_sk_YOUR_SECRET") # secret: server side only
|
|
61
|
+
prophecy.signup(user.id, plan="free", channel="google")
|
|
62
|
+
prophecy.feature("export-pdf", user.id)
|
|
63
|
+
prophecy.payment(user.id, 49.00, "USD", plan="pro")
|
|
64
|
+
|
|
65
|
+
report = prophecy.report()["report"] # the latest projection
|
|
66
|
+
print(report["headline"]["verdict"])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Scan the codebase (sends a manifest of routes, dependencies and infrastructure, never your source code):
|
|
70
|
+
`python -m linearjeph.prophecy scan . --send`. Full guide: https://linearjeph.cc/docs/sdk/prophecy
|
|
71
|
+
|
|
72
|
+
## Monitoring quick start
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
import linearjeph
|
|
76
|
+
linearjeph.init("lj_app_YOUR_KEY", release="1.4.2", environment="production")
|
|
77
|
+
linearjeph.capture_exception() # or let unhandled exceptions be captured automatically
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Get the key from Dashboard > Maintenance > your application. The endpoint defaults to `https://linearjeph.cc`.
|
|
81
|
+
|
|
82
|
+
- `capture_message("text")`, `heartbeat("nightly-job", "ok", interval_s=3600)`, `report_packages()`.
|
|
83
|
+
- Cron: `@linearjeph.monitor("nightly", interval_s=86400)` sends ok/error automatically.
|
|
84
|
+
- Performance: `with linearjeph.start_transaction("checkout"): ...`
|
|
85
|
+
- Django: add `"linearjeph.integrations.django.LinearjephMiddleware"` to `MIDDLEWARE`.
|
|
86
|
+
- FastAPI/Starlette: `app.add_middleware(linearjeph.integrations.asgi.LinearjephASGIMiddleware)`.
|
|
87
|
+
- Flask: `linearjeph.integrations.flask.init_app(app)`.
|
|
88
|
+
- Options: `sample_rate`, `traces_sample_rate`, `before_send`, `scrub_keys`, `max_queue`, `retries`, `debug`.
|
|
89
|
+
- Events are sent from a background thread (bounded queue, retry with backoff, 401 stops sending) and flushed at exit. The SDK never raises into your code.
|
|
90
|
+
|
|
91
|
+
Full documentation: `docs/sdk/python.md` in the repository, or https://linearjeph.cc/docs/sdk/python. Tests: `python -m pytest packages/sdk-python`. MIT licensed.
|
|
92
|
+
|
|
93
|
+
## Prophecy (growth intelligence, $500/month)
|
|
94
|
+
|
|
95
|
+
Tracks users, payments, feature use and API traffic, scans your codebase locally and returns a forecast report. Full guide: https://linearjeph.cc/docs/sdk/prophecy
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from linearjeph import prophecy
|
|
99
|
+
prophecy.init(ingest_key=os.environ["LINEARJEPH_INGEST_KEY"], secret_key=os.environ.get("LINEARJEPH_SECRET_KEY"))
|
|
100
|
+
prophecy.signup(user.id, plan="free", channel="ads")
|
|
101
|
+
prophecy.payment(user.id, 29, "USD", plan="pro")
|
|
102
|
+
prophecy.feature("export-csv", user.id)
|
|
103
|
+
prophecy.send_manifest(prophecy.scan("."))
|
|
104
|
+
report = prophecy.report() # secret key, server side only
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Requests: Django `MIDDLEWARE += ["linearjeph.prophecy.django.ProphecyMiddleware"]`, Flask `prophecy.middleware.init_flask(app)`, FastAPI/Starlette `app.add_middleware(prophecy.middleware.ASGIMiddleware)`, any WSGI app `prophecy.middleware.WSGIMiddleware(app)`. CLI: `python -m linearjeph.prophecy scan [dir] [--send]`, `python -m linearjeph.prophecy import events history.csv`. User ids are SHA-256 hashed by default.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
linearjeph/__init__.py,sha256=ik2butTnltyUlZCgnTTiHNX43cqfOLle5U0kwlejtIo,1154
|
|
2
|
+
linearjeph/client.py,sha256=IxztQXsa_H6uXPC7070VBcVct1pyWikdIi9bghT1uDY,24248
|
|
3
|
+
linearjeph/health.py,sha256=OIXDZPwk3mmUt32qTFpOgnSAMK2ggmP4goyPM9puHhE,5834
|
|
4
|
+
linearjeph/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
linearjeph/telemetry.py,sha256=xYyzRSGccJXrIQKbnsizQAPv7isNxGuqQrX0uNDfL78,17210
|
|
6
|
+
linearjeph/integrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
linearjeph/integrations/asgi.py,sha256=gwFHrjBjgBzhj6UVowPNl6yTPptB66bwUnC39HyWQoc,1896
|
|
8
|
+
linearjeph/integrations/django.py,sha256=QibzC-sBriZz-aud3yQm0k6rGY6Rzv91bhyme5wMPrQ,2213
|
|
9
|
+
linearjeph/integrations/flask.py,sha256=y5aThwvNYsE455BIRL-Tp-UIqN5nfV5d99584vdTcv0,1666
|
|
10
|
+
linearjeph/prophecy/__init__.py,sha256=zBcZMNKkeU9xieOtrwp7I9KL7HSCwmZVR7A75PjsDMc,20047
|
|
11
|
+
linearjeph/prophecy/__main__.py,sha256=oxHZQ54WNWewfyIwqdnyS0OLFrUS9f16HfXL-gPbp6I,2885
|
|
12
|
+
linearjeph/prophecy/django.py,sha256=tkUA63Qys_XD7s7QTJn4TpNHD42I4U3kPyC8x-3rUCk,2378
|
|
13
|
+
linearjeph/prophecy/middleware.py,sha256=rwID32ZeAPjf1E1HbRdSW_TXeCy73xse2Oi0X_nps78,5782
|
|
14
|
+
linearjeph/prophecy/scanner.py,sha256=nHevN3Y6kF6Jx5V6LG3KTWYNsNOyFY3SOxMqdh-CnJI,27539
|
|
15
|
+
linearjeph-1.2.0.dist-info/licenses/LICENSE,sha256=hmU0HWcnFhtXze4x3bbJddcDancLOreSBELH45qDGj8,1067
|
|
16
|
+
linearjeph-1.2.0.dist-info/METADATA,sha256=fkpq6I0gTsYSCQGKNudWO5tx2dkslESIAloC0ywPo_4,5889
|
|
17
|
+
linearjeph-1.2.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
18
|
+
linearjeph-1.2.0.dist-info/top_level.txt,sha256=FQazFO9xuPCTJ0F8IXxEzEWOUkg0xDLZIfsPoEjcc9Q,11
|
|
19
|
+
linearjeph-1.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LINEARJEPH
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
linearjeph
|