viktorbarzin-telemetry 0.1.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.
@@ -0,0 +1,58 @@
1
+ """Client library for the viktorbarzin.me telemetry standard.
2
+
3
+ Product events go to the Event pipeline through Segment's SDK, errors to
4
+ GlitchTip through Sentry's, and logs to stdout as OpenTelemetry-shaped JSON.
5
+ """
6
+
7
+ from ._asgi import EnduserMiddleware
8
+ from ._config import (
9
+ ENVIRONMENT_VARIABLE,
10
+ HOST_VARIABLE,
11
+ PRODUCTION,
12
+ Config,
13
+ public_host,
14
+ resolve_environment,
15
+ resolve_host,
16
+ )
17
+ from ._context import LogContext, current_context, log_context
18
+ from ._events import EventClient, Telemetry, configure, flush, identify, track
19
+ from ._logging import JsonFormatter, setup_logging
20
+ from ._scrub import Scrubber, register_secret
21
+ from ._sentry import init_sentry
22
+ from ._validation import (
23
+ MAX_PROPERTIES_LENGTH,
24
+ JsonValue,
25
+ Properties,
26
+ event_name_problem,
27
+ properties_problem,
28
+ )
29
+
30
+ __all__ = [
31
+ "ENVIRONMENT_VARIABLE",
32
+ "HOST_VARIABLE",
33
+ "MAX_PROPERTIES_LENGTH",
34
+ "PRODUCTION",
35
+ "Config",
36
+ "EnduserMiddleware",
37
+ "EventClient",
38
+ "JsonFormatter",
39
+ "JsonValue",
40
+ "LogContext",
41
+ "Properties",
42
+ "Scrubber",
43
+ "Telemetry",
44
+ "configure",
45
+ "current_context",
46
+ "event_name_problem",
47
+ "flush",
48
+ "identify",
49
+ "init_sentry",
50
+ "log_context",
51
+ "properties_problem",
52
+ "public_host",
53
+ "register_secret",
54
+ "resolve_environment",
55
+ "resolve_host",
56
+ "setup_logging",
57
+ "track",
58
+ ]
@@ -0,0 +1,39 @@
1
+ """ASGI middleware that puts the signed-in Person on the request's log context."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import contextlib
6
+ from collections.abc import Awaitable, Callable, MutableMapping
7
+
8
+ from ._context import log_context
9
+
10
+ Scope = MutableMapping[str, object]
11
+ Message = MutableMapping[str, object]
12
+ Receive = Callable[[], Awaitable[Message]]
13
+ Send = Callable[[Message], Awaitable[None]]
14
+
15
+
16
+ class EnduserMiddleware:
17
+ """Sets ``enduser.id`` for each HTTP or WebSocket request.
18
+
19
+ ``resolve_user`` gets the ASGI scope and returns the Person's Authentik
20
+ ``sub`` (or None), directly or as an awaitable. With Starlette or FastAPI:
21
+ ``app.add_middleware(EnduserMiddleware, resolve_user=...)``.
22
+ """
23
+
24
+ App = Callable[[Scope, Receive, Send], Awaitable[None]]
25
+ Resolver = Callable[[Scope], "str | Awaitable[str | None] | None"]
26
+
27
+ def __init__(self, app: App, resolve_user: Resolver) -> None:
28
+ self.app = app
29
+ self.resolve_user = resolve_user
30
+
31
+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
32
+ if scope.get("type") not in ("http", "websocket"):
33
+ await self.app(scope, receive, send)
34
+ return
35
+ result = self.resolve_user(scope)
36
+ user = result if result is None or isinstance(result, str) else await result
37
+ block = log_context(enduser_id=user) if user else contextlib.nullcontext()
38
+ with block:
39
+ await self.app(scope, receive, send)
@@ -0,0 +1,48 @@
1
+ from __future__ import annotations
2
+
3
+ import dataclasses
4
+ import os
5
+
6
+ PRODUCTION = "production"
7
+ ENVIRONMENT_VARIABLE = "TELEMETRY_ENVIRONMENT"
8
+ HOST_VARIABLE = "TELEMETRY_HOST"
9
+
10
+
11
+ def resolve_environment(explicit: str | None = None) -> str:
12
+ """The explicit value, else ``$TELEMETRY_ENVIRONMENT``, else ``dev``."""
13
+ if explicit is not None:
14
+ return explicit
15
+ return os.environ.get(ENVIRONMENT_VARIABLE) or "dev"
16
+
17
+
18
+ def resolve_host(explicit: str | None = None) -> str | None:
19
+ """The explicit Event pipeline URL, else ``$TELEMETRY_HOST``, else None."""
20
+ if explicit:
21
+ return explicit
22
+ return os.environ.get(HOST_VARIABLE) or None
23
+
24
+
25
+ def public_host(app_host: str) -> str:
26
+ """The Event pipeline on an app's public host, for code outside the cluster."""
27
+ return f"https://{app_host}/_t/e"
28
+
29
+
30
+ @dataclasses.dataclass(frozen=True)
31
+ class Config:
32
+ """One app's telemetry settings.
33
+
34
+ ``host`` is the Event pipeline's base URL; the Segment client appends
35
+ ``/v1/batch``. Production needs one. Inside our cluster it is the
36
+ pipeline's Service; elsewhere it is ``public_host("<app host>")``.
37
+ """
38
+
39
+ app: str
40
+ write_key: str
41
+ release: str
42
+ environment: str
43
+ host: str | None = None
44
+ dsn: str | None = None
45
+
46
+ @property
47
+ def production(self) -> bool:
48
+ return self.environment == PRODUCTION
@@ -0,0 +1,63 @@
1
+ """Per-request context: who the Person is and which trace the work belongs to.
2
+
3
+ Kept in a ContextVar, so each asyncio task and thread sees its own value and
4
+ the value unwinds when the ``log_context`` block ends.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import contextlib
10
+ import contextvars
11
+ import dataclasses
12
+ from collections.abc import Iterator, Mapping
13
+ from types import MappingProxyType
14
+
15
+
16
+ @dataclasses.dataclass(frozen=True)
17
+ class LogContext:
18
+ enduser_id: str | None = None
19
+ anonymous_id: str | None = None
20
+ trace_id: str | None = None
21
+ span_id: str | None = None
22
+ attributes: Mapping[str, object] = dataclasses.field(
23
+ default_factory=lambda: MappingProxyType({})
24
+ )
25
+
26
+
27
+ _EMPTY = LogContext()
28
+ _current: contextvars.ContextVar[LogContext] = contextvars.ContextVar(
29
+ "viktorbarzin_telemetry_context", default=_EMPTY
30
+ )
31
+
32
+
33
+ def current_context() -> LogContext:
34
+ return _current.get()
35
+
36
+
37
+ @contextlib.contextmanager
38
+ def log_context(
39
+ *,
40
+ enduser_id: str | None = None,
41
+ anonymous_id: str | None = None,
42
+ trace_id: str | None = None,
43
+ span_id: str | None = None,
44
+ **attributes: object,
45
+ ) -> Iterator[LogContext]:
46
+ """Add fields to every log line, and ids to every event, inside the block.
47
+
48
+ Values left as None keep whatever an enclosing block set. Keyword
49
+ ``attributes`` merge over the enclosing ones.
50
+ """
51
+ outer = _current.get()
52
+ merged = LogContext(
53
+ enduser_id=enduser_id if enduser_id is not None else outer.enduser_id,
54
+ anonymous_id=anonymous_id if anonymous_id is not None else outer.anonymous_id,
55
+ trace_id=trace_id if trace_id is not None else outer.trace_id,
56
+ span_id=span_id if span_id is not None else outer.span_id,
57
+ attributes=MappingProxyType({**outer.attributes, **attributes}),
58
+ )
59
+ token = _current.set(merged)
60
+ try:
61
+ yield merged
62
+ finally:
63
+ _current.reset(token)
@@ -0,0 +1,188 @@
1
+ """Product events through Segment's Python SDK, pointed at the Event pipeline."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ import uuid
7
+ from typing import Protocol
8
+
9
+ from segment.analytics.client import Client
10
+
11
+ from ._config import HOST_VARIABLE, Config, resolve_environment, resolve_host
12
+ from ._context import current_context
13
+ from ._scrub import register_secret
14
+ from ._sentry import init_sentry
15
+ from ._validation import JsonValue, Properties, event_name_problem, properties_problem
16
+
17
+ _log = logging.getLogger("viktorbarzin_telemetry")
18
+
19
+
20
+ class EventClient(Protocol):
21
+ """The part of ``segment.analytics.Client`` this package calls."""
22
+
23
+ def track(
24
+ self,
25
+ *,
26
+ user_id: str | None,
27
+ event: str,
28
+ properties: dict[str, JsonValue],
29
+ context: dict[str, JsonValue],
30
+ anonymous_id: str,
31
+ ) -> object: ...
32
+
33
+ def identify(
34
+ self,
35
+ *,
36
+ user_id: str,
37
+ traits: dict[str, JsonValue],
38
+ context: dict[str, JsonValue],
39
+ anonymous_id: str,
40
+ ) -> object: ...
41
+
42
+ def flush(self) -> None: ...
43
+
44
+
45
+ def _on_error(err: Exception, batch: list[dict[str, object]]) -> None:
46
+ _log.warning("sending %d events failed: %s", len(batch), err)
47
+
48
+
49
+ def segment_client(config: Config) -> Client:
50
+ if not config.host:
51
+ raise ValueError(
52
+ f"production telemetry needs the Event pipeline URL: pass host or set ${HOST_VARIABLE}"
53
+ )
54
+ return Client(config.write_key, host=config.host, on_error=_on_error)
55
+
56
+
57
+ class Telemetry:
58
+ """One app's event sender. Outside production it validates and sends nothing."""
59
+
60
+ def __init__(self, config: Config, client: EventClient | None = None) -> None:
61
+ self.config = config
62
+ self._client: EventClient | None = None
63
+ if config.production:
64
+ self._client = client if client is not None else segment_client(config)
65
+ self._context: dict[str, JsonValue] = {
66
+ "app": {"name": config.app, "version": config.release}
67
+ }
68
+
69
+ @property
70
+ def enabled(self) -> bool:
71
+ """True when events leave the process."""
72
+ return self._client is not None
73
+
74
+ @property
75
+ def segment_client(self) -> Client | None:
76
+ return self._client if isinstance(self._client, Client) else None
77
+
78
+ def _ids(self, user_id: str | None, anonymous_id: str | None) -> tuple[str | None, str]:
79
+ ctx = current_context()
80
+ user = user_id if user_id is not None else ctx.enduser_id
81
+ anon = anonymous_id or ctx.anonymous_id or str(uuid.uuid4())
82
+ return user, anon
83
+
84
+ def track(
85
+ self,
86
+ event: str,
87
+ properties: Properties | None = None,
88
+ *,
89
+ user_id: str | None = None,
90
+ anonymous_id: str | None = None,
91
+ ) -> bool:
92
+ """Queue a Product event. False when it was invalid and dropped."""
93
+ props = dict(properties or {})
94
+ problem = event_name_problem(event) or properties_problem(props)
95
+ if problem is not None:
96
+ _log.warning("event dropped: %s", problem, extra={"event_name": event})
97
+ return False
98
+ if self._client is not None:
99
+ user, anon = self._ids(user_id, anonymous_id)
100
+ self._client.track(
101
+ user_id=user,
102
+ event=event,
103
+ properties=props,
104
+ context=dict(self._context),
105
+ anonymous_id=anon,
106
+ )
107
+ return True
108
+
109
+ def identify(
110
+ self,
111
+ user_id: str,
112
+ traits: Properties | None = None,
113
+ *,
114
+ anonymous_id: str | None = None,
115
+ ) -> bool:
116
+ """Tie ``anonymous_id`` to the Person ``user_id`` (their Authentik ``sub``)."""
117
+ values = dict(traits or {})
118
+ problem = "identify without a user id" if not user_id else properties_problem(values)
119
+ if problem is not None:
120
+ _log.warning("identify dropped: %s", problem)
121
+ return False
122
+ if self._client is not None:
123
+ _, anon = self._ids(user_id, anonymous_id)
124
+ self._client.identify(
125
+ user_id=user_id, traits=values, context=dict(self._context), anonymous_id=anon
126
+ )
127
+ return True
128
+
129
+ def flush(self) -> None:
130
+ """Block until queued events are sent."""
131
+ if self._client is not None:
132
+ self._client.flush()
133
+
134
+
135
+ _default = Telemetry(Config(app="unconfigured", write_key="", release="0", environment="unset"))
136
+
137
+
138
+ def configure(
139
+ app: str,
140
+ write_key: str,
141
+ release: str,
142
+ environment: str | None = None,
143
+ dsn: str | None = None,
144
+ host: str | None = None,
145
+ *,
146
+ client: EventClient | None = None,
147
+ ) -> Telemetry:
148
+ """Set up events (and Sentry when ``dsn`` is given) for this process.
149
+
150
+ Sending happens only when the environment, explicit or from
151
+ ``$TELEMETRY_ENVIRONMENT``, is ``production``. ``host`` is the Event
152
+ pipeline's base URL, explicit or from ``$TELEMETRY_HOST``; production
153
+ without one raises ValueError.
154
+ """
155
+ global _default
156
+ config = Config(
157
+ app=app,
158
+ write_key=write_key,
159
+ release=release,
160
+ environment=resolve_environment(environment),
161
+ host=resolve_host(host),
162
+ dsn=dsn,
163
+ )
164
+ register_secret(write_key)
165
+ _default = Telemetry(config, client=client)
166
+ if dsn and config.production:
167
+ init_sentry(dsn, release=release, environment=config.environment)
168
+ return _default
169
+
170
+
171
+ def track(
172
+ event: str,
173
+ properties: Properties | None = None,
174
+ *,
175
+ user_id: str | None = None,
176
+ anonymous_id: str | None = None,
177
+ ) -> bool:
178
+ return _default.track(event, properties, user_id=user_id, anonymous_id=anonymous_id)
179
+
180
+
181
+ def identify(
182
+ user_id: str, traits: Properties | None = None, *, anonymous_id: str | None = None
183
+ ) -> bool:
184
+ return _default.identify(user_id, traits, anonymous_id=anonymous_id)
185
+
186
+
187
+ def flush() -> None:
188
+ _default.flush()
@@ -0,0 +1,201 @@
1
+ """JSON log lines in the OpenTelemetry log data model field names."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import datetime as _dt
6
+ import functools
7
+ import json
8
+ import logging
9
+ import re
10
+ import sys
11
+ from collections.abc import Callable, Iterable
12
+ from typing import TextIO
13
+
14
+ from ._config import resolve_environment
15
+ from ._context import current_context
16
+ from ._scrub import Scrubber
17
+
18
+ # LogRecord's own attributes, never echoed as extra fields.
19
+ _RECORD_KEYS = frozenset(vars(logging.LogRecord("", 0, "", 0, "", (), None))) | {
20
+ "message",
21
+ "asctime",
22
+ "color_message",
23
+ }
24
+
25
+ _STANDARD_KEYS = frozenset(
26
+ {
27
+ "timestamp",
28
+ "severity_text",
29
+ "severity_number",
30
+ "body",
31
+ "logger.name",
32
+ "service.name",
33
+ "service.version",
34
+ "deployment.environment.name",
35
+ "trace_id",
36
+ "span_id",
37
+ "enduser.id",
38
+ "exception.type",
39
+ "exception.message",
40
+ "exception.stacktrace",
41
+ "code.stacktrace",
42
+ }
43
+ )
44
+
45
+ _CAMEL_WORD = re.compile(r"(.)([A-Z][a-z]+)")
46
+ _CAMEL_TAIL = re.compile(r"([a-z0-9])([A-Z])")
47
+ _NOT_SNAKE = re.compile(r"[^a-z0-9]+")
48
+
49
+
50
+ def severity(levelno: int) -> tuple[str, int]:
51
+ """OpenTelemetry severity text and number for a ``logging`` level."""
52
+ if levelno < logging.INFO:
53
+ return "DEBUG", 5
54
+ if levelno < logging.WARNING:
55
+ return "INFO", 9
56
+ if levelno < logging.ERROR:
57
+ return "WARN", 13
58
+ if levelno < logging.CRITICAL:
59
+ return "ERROR", 17
60
+ return "FATAL", 21
61
+
62
+
63
+ def format_timestamp(created: float) -> str:
64
+ """RFC 3339 in UTC with a ``Z``, to the microsecond ``logging`` records."""
65
+ moment = _dt.datetime.fromtimestamp(created, tz=_dt.timezone.utc)
66
+ return moment.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
67
+
68
+
69
+ def snake_case_key(key: str) -> str:
70
+ """``channelId`` to ``channel_id``. Dots stay, for OTel names like ``http.request.method``."""
71
+ parts: list[str] = []
72
+ for segment in key.split("."):
73
+ words = _CAMEL_TAIL.sub(r"\1_\2", _CAMEL_WORD.sub(r"\1_\2", segment)).lower()
74
+ cleaned = _NOT_SNAKE.sub("_", words).strip("_")
75
+ if cleaned:
76
+ parts.append(cleaned)
77
+ return ".".join(parts) or "attribute"
78
+
79
+
80
+ SpanIds = Callable[[], "tuple[str, str] | None"]
81
+
82
+
83
+ @functools.cache
84
+ def _otel_span_ids() -> SpanIds | None:
85
+ """A reader for the active OTel span's ids, or None without opentelemetry-api."""
86
+ try:
87
+ from opentelemetry import trace
88
+ except ImportError:
89
+ return None
90
+
91
+ def read() -> tuple[str, str] | None:
92
+ ctx = trace.get_current_span().get_span_context()
93
+ if not ctx.is_valid:
94
+ return None
95
+ return format(ctx.trace_id, "032x"), format(ctx.span_id, "016x")
96
+
97
+ return read
98
+
99
+
100
+ def _exception_type(exc_type: type[BaseException]) -> str:
101
+ if exc_type.__module__ == "builtins":
102
+ return exc_type.__qualname__
103
+ return f"{exc_type.__module__}.{exc_type.__qualname__}"
104
+
105
+
106
+ class JsonFormatter(logging.Formatter):
107
+ """One JSON object per record, scrubbed of secrets."""
108
+
109
+ def __init__(
110
+ self,
111
+ *,
112
+ service_name: str,
113
+ service_version: str,
114
+ environment: str | None = None,
115
+ secrets: Iterable[str] = (),
116
+ ) -> None:
117
+ super().__init__()
118
+ self._service = {
119
+ "service.name": service_name,
120
+ "service.version": service_version,
121
+ "deployment.environment.name": resolve_environment(environment),
122
+ }
123
+ self._scrubber = Scrubber(secrets)
124
+
125
+ def format(self, record: logging.LogRecord) -> str:
126
+ text, number = severity(record.levelno)
127
+ payload: dict[str, object] = {
128
+ "timestamp": format_timestamp(record.created),
129
+ "severity_text": text,
130
+ "severity_number": number,
131
+ "body": record.getMessage(),
132
+ "logger.name": record.name,
133
+ **self._service,
134
+ }
135
+ ctx = current_context()
136
+ reader = _otel_span_ids()
137
+ span = reader() if reader is not None else None
138
+ trace_id, span_id = span if span is not None else (ctx.trace_id, ctx.span_id)
139
+ if trace_id:
140
+ payload["trace_id"] = trace_id
141
+ if span_id:
142
+ payload["span_id"] = span_id
143
+ if ctx.enduser_id:
144
+ payload["enduser.id"] = ctx.enduser_id
145
+
146
+ attributes: dict[str, object] = {}
147
+ for key, value in ctx.attributes.items():
148
+ attributes[snake_case_key(key)] = value
149
+ for key, value in vars(record).items():
150
+ if key not in _RECORD_KEYS and not key.startswith("_"):
151
+ attributes[snake_case_key(key)] = value
152
+ for key, value in attributes.items():
153
+ if key not in _STANDARD_KEYS:
154
+ payload[key] = value
155
+
156
+ if record.exc_info and record.exc_info[0] is not None:
157
+ exc_type, exc, _ = record.exc_info
158
+ payload["exception.type"] = _exception_type(exc_type)
159
+ payload["exception.message"] = str(exc)
160
+ payload["exception.stacktrace"] = self.formatException(record.exc_info)
161
+ if record.stack_info:
162
+ payload["code.stacktrace"] = record.stack_info
163
+ return self._scrubber.scrub(_dumps(payload))
164
+
165
+
166
+ def _dumps(payload: dict[str, object]) -> str:
167
+ try:
168
+ return json.dumps(payload, ensure_ascii=False, default=str)
169
+ except ValueError:
170
+ # A circular structure: keep the line, as text.
171
+ flat = {
172
+ k: v if isinstance(v, (str, int, float, bool)) else str(v) for k, v in payload.items()
173
+ }
174
+ return json.dumps(flat, ensure_ascii=False)
175
+
176
+
177
+ def setup_logging(
178
+ *,
179
+ service_name: str,
180
+ service_version: str,
181
+ environment: str | None = None,
182
+ level: int | str = logging.INFO,
183
+ stream: TextIO | None = None,
184
+ secrets: Iterable[str] = (),
185
+ ) -> logging.Handler:
186
+ """Make the root logger write JSON lines to stdout (or ``stream``), replacing its handlers."""
187
+ handler = logging.StreamHandler(stream if stream is not None else sys.stdout)
188
+ handler.setFormatter(
189
+ JsonFormatter(
190
+ service_name=service_name,
191
+ service_version=service_version,
192
+ environment=environment,
193
+ secrets=secrets,
194
+ )
195
+ )
196
+ root = logging.getLogger()
197
+ for existing in list(root.handlers):
198
+ root.removeHandler(existing)
199
+ root.addHandler(handler)
200
+ root.setLevel(level)
201
+ return handler
@@ -0,0 +1,54 @@
1
+ """Redact secrets from a rendered log line before it leaves the process.
2
+
3
+ The same approach as tripit's ``observability.Scrubber``: known secret values,
4
+ plus credential-shaped patterns that should never reach a shared log store.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import re
10
+ from collections.abc import Iterable
11
+
12
+ REDACTED = "[REDACTED]"
13
+
14
+ # Values shorter than this are not masked: hiding short config such as "dev"
15
+ # would remove ordinary content from the logs.
16
+ MIN_SECRET_LENGTH = 8
17
+
18
+ _BEARER_RE = re.compile(r"(?i)\bbearer\s+[A-Za-z0-9._~+/=\-]+")
19
+ _BASIC_RE = re.compile(r"(?i)\bbasic\s+[A-Za-z0-9+/]{8,}={0,2}")
20
+ _JWT_RE = re.compile(r"\beyJ[A-Za-z0-9_\-]{5,}\.[A-Za-z0-9_\-]{5,}\.[A-Za-z0-9_\-]{2,}")
21
+ _PEM_RE = re.compile(r"-----BEGIN[^-]+-----.*?-----END[^-]+-----", re.DOTALL)
22
+
23
+
24
+ _registered: tuple[str, ...] = ()
25
+
26
+
27
+ def _longest_first(values: Iterable[str]) -> tuple[str, ...]:
28
+ # Longest first, so a secret that contains another is masked whole.
29
+ return tuple(sorted({v for v in values if len(v) >= MIN_SECRET_LENGTH}, key=len, reverse=True))
30
+
31
+
32
+ def register_secret(value: str) -> None:
33
+ """Mask ``value`` in every Scrubber from now on. ``configure`` registers the write key."""
34
+ global _registered
35
+ _registered = _longest_first((*_registered, value))
36
+
37
+
38
+ class Scrubber:
39
+ def __init__(self, secret_values: Iterable[str] = ()) -> None:
40
+ self._own = _longest_first(secret_values)
41
+ self._seen: tuple[str, ...] | None = None
42
+ self._secrets: tuple[str, ...] = self._own
43
+
44
+ def scrub(self, text: str) -> str:
45
+ if self._seen is not _registered:
46
+ self._seen = _registered
47
+ self._secrets = _longest_first((*self._own, *_registered))
48
+ for secret in self._secrets:
49
+ if secret in text:
50
+ text = text.replace(secret, REDACTED)
51
+ text = _BEARER_RE.sub(f"Bearer {REDACTED}", text)
52
+ text = _BASIC_RE.sub(f"Basic {REDACTED}", text)
53
+ text = _JWT_RE.sub(REDACTED, text)
54
+ return _PEM_RE.sub(REDACTED, text)
@@ -0,0 +1,22 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+
5
+ _log = logging.getLogger("viktorbarzin_telemetry")
6
+
7
+
8
+ def init_sentry(dsn: str, *, release: str, environment: str) -> bool:
9
+ """Start the Sentry SDK against GlitchTip. False when sentry-sdk is not installed.
10
+
11
+ The DSN has the form ``https://<key>@<app host>/_t/x/<project id>``.
12
+ """
13
+ try:
14
+ import sentry_sdk
15
+ except ImportError:
16
+ _log.warning(
17
+ "a Sentry DSN is set but sentry-sdk is not installed; "
18
+ "install viktorbarzin-telemetry[sentry]"
19
+ )
20
+ return False
21
+ sentry_sdk.init(dsn=dsn, release=release, environment=environment)
22
+ return True
@@ -0,0 +1,49 @@
1
+ """The Product event rules the Event pipeline also enforces.
2
+
3
+ The pipeline rejects an event that breaks them, so the library checks first,
4
+ drops the event and logs why, instead of sending something that will be thrown
5
+ away.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import re
12
+ from collections.abc import Mapping, Sequence
13
+ from typing import TypeAlias
14
+
15
+ JsonValue: TypeAlias = (
16
+ "str | int | float | bool | Sequence[JsonValue] | Mapping[str, JsonValue] | None"
17
+ )
18
+ Properties: TypeAlias = Mapping[str, JsonValue]
19
+
20
+ EVENT_NAME_PATTERN = re.compile(r"[a-z][a-z0-9_]*\.[a-z][a-z0-9_]*")
21
+ PROPERTY_KEY_PATTERN = re.compile(r"[a-z][a-z0-9_]*")
22
+ MAX_PROPERTIES_LENGTH = 2048
23
+
24
+
25
+ def event_name_problem(name: str) -> str | None:
26
+ """Why ``name`` is not a valid ``noun.past_verb`` event name, or None."""
27
+ if EVENT_NAME_PATTERN.fullmatch(name):
28
+ return None
29
+ return f"event name {name!r} is not noun.past_verb"
30
+
31
+
32
+ def properties_length(properties: Properties) -> int:
33
+ """UTF-8 bytes of the compact JSON encoding, which bounds the pipeline's count."""
34
+ encoded = json.dumps(properties, separators=(",", ":"), ensure_ascii=False, allow_nan=False)
35
+ return len(encoded.encode("utf-8"))
36
+
37
+
38
+ def properties_problem(properties: Properties) -> str | None:
39
+ """Why ``properties`` would be rejected, or None when they are fine."""
40
+ for key in properties:
41
+ if not PROPERTY_KEY_PATTERN.fullmatch(key):
42
+ return f"property key {key!r} is not snake_case"
43
+ try:
44
+ length = properties_length(properties)
45
+ except (TypeError, ValueError) as err:
46
+ return f"properties are not JSON: {err}"
47
+ if length > MAX_PROPERTIES_LENGTH:
48
+ return f"properties are {length} bytes as JSON, over {MAX_PROPERTIES_LENGTH}"
49
+ return None
File without changes
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.5
2
+ Name: viktorbarzin-telemetry
3
+ Version: 0.1.0
4
+ Summary: Product events, errors and JSON logs to the viktorbarzin.me telemetry standard
5
+ Project-URL: Source, https://github.com/ViktorBarzin/telemetry
6
+ Author: Viktor Barzin
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Typing :: Typed
12
+ Requires-Python: >=3.10
13
+ Requires-Dist: segment-analytics-python<3,>=2.3
14
+ Provides-Extra: otel
15
+ Requires-Dist: opentelemetry-api>=1.20; extra == 'otel'
16
+ Provides-Extra: sentry
17
+ Requires-Dist: sentry-sdk<3,>=2.0; extra == 'sentry'
18
+ Description-Content-Type: text/markdown
19
+
20
+ # viktorbarzin-telemetry
21
+
22
+ The Python client for the viktorbarzin.me telemetry standard: Product events
23
+ through Segment's `segment-analytics-python` to our Event pipeline, errors
24
+ through `sentry-sdk` to GlitchTip, and logs as one OpenTelemetry-shaped JSON
25
+ object per line on stdout.
26
+
27
+ ```sh
28
+ pip install viktorbarzin-telemetry # events and logs
29
+ pip install "viktorbarzin-telemetry[sentry]" # plus Sentry
30
+ ```
31
+
32
+ ```python
33
+ import logging
34
+ import viktorbarzin_telemetry as telemetry
35
+
36
+ telemetry.setup_logging(service_name="tv", service_version="1.4.0")
37
+ telemetry.configure(
38
+ app="tv",
39
+ write_key=settings.segment_write_key,
40
+ release="1.4.0",
41
+ environment="production", # or set TELEMETRY_ENVIRONMENT=production
42
+ host=settings.event_pipeline_url, # or set TELEMETRY_HOST
43
+ dsn=settings.sentry_dsn, # https://<key>@tv.viktorbarzin.me/_t/x/<project id>
44
+ )
45
+
46
+ with telemetry.log_context(enduser_id=user.sub):
47
+ logging.getLogger(__name__).info("recording started", extra={"channel_id": 4})
48
+ telemetry.track("recording.started", {"channel_id": 4})
49
+
50
+ telemetry.flush() # before exit
51
+ ```
52
+
53
+ - `host` is the Event pipeline's base URL, from the argument or
54
+ `TELEMETRY_HOST`. Inside our cluster it is the event-pipeline Service's URL;
55
+ elsewhere use `telemetry.public_host("tv.viktorbarzin.me")`. Production
56
+ without a host raises `ValueError`.
57
+ - Nothing is sent unless the environment is `production`. Invalid events
58
+ are validated in every environment, dropped and logged as a warning.
59
+ - `track` takes `userId` from `log_context(enduser_id=...)` and `anonymousId`
60
+ from `log_context(anonymous_id=...)`, or a fresh random id.
61
+ - `EnduserMiddleware(app, resolve_user=...)` sets `enduser.id` per request in
62
+ any ASGI app. `resolve_user` gets the ASGI scope and returns the Person's
63
+ Authentik `sub` or None, directly or as an awaitable.
64
+ - `trace_id` and `span_id` come from the active OpenTelemetry span when
65
+ `opentelemetry-api` is installed, else from `log_context`.
66
+ - The JSON formatter masks the write key, values passed as `secrets=`, and
67
+ bearer tokens, Basic credentials, JWTs and PEM blocks.
68
+
69
+ The standard itself is in the repository README.
@@ -0,0 +1,14 @@
1
+ viktorbarzin_telemetry/__init__.py,sha256=a5HFlrVugjtuxJ9SMMW4EAANceQOe9X0kIFmMYNomyk,1378
2
+ viktorbarzin_telemetry/_asgi.py,sha256=jjmRpiN37a1axKXgPG49cEnKjZEcaGxHHDeQIl7Ld_0,1470
3
+ viktorbarzin_telemetry/_config.py,sha256=aSszoN_r8oMncpoIunLSRBvLHqvugxZ8g6-kmWvbVDo,1352
4
+ viktorbarzin_telemetry/_context.py,sha256=eOv2T2vkhLuFj-hAsHdrvd4mHzLokMSNuJjd_hxUf5k,1903
5
+ viktorbarzin_telemetry/_events.py,sha256=yqwWAbDuQu5TIzg-sTMDxM_MhDNAAWRpo4Nu5M8TKEA,5898
6
+ viktorbarzin_telemetry/_logging.py,sha256=BPAh4QbcJ5ZyO2S4aXBZ_aWgTCouflEjNbNsqLd-dbY,6400
7
+ viktorbarzin_telemetry/_scrub.py,sha256=nFSQbBOcpnYmCR6QQtUcrJxLtciX15gz9theAFYR-rs,2023
8
+ viktorbarzin_telemetry/_sentry.py,sha256=1_8Q_DuA0K5rMm_NU_D-QwGveUimsRZrSSrhlmAsLjg,664
9
+ viktorbarzin_telemetry/_validation.py,sha256=nZGavopfoQij2DRD246UNuYMIpejhhOvdPwtZ5CBeQI,1774
10
+ viktorbarzin_telemetry/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ viktorbarzin_telemetry-0.1.0.dist-info/METADATA,sha256=8Vosj-l8GFxiT8FN7Q_Myt1fkBnPSpp_crrcaVHAp5g,2848
12
+ viktorbarzin_telemetry-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
13
+ viktorbarzin_telemetry-0.1.0.dist-info/licenses/LICENSE,sha256=bh5tWZXvkWABk3qpM5JpCVN6xihEm7WaUCyKaZ_UwEo,1070
14
+ viktorbarzin_telemetry-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Viktor Barzin
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.