Skip to content

Index

telemetry

Opt-in community telemetry: consent and privacy primitives.

Collection and submission are separate from user-global consent. No import in this package starts a network client or creates a consent record. See docs/telemetry.md for the data policy, inspection, revocation, and purge.

Classes

TelemetryCollector

TelemetryCollector(consent=None)

Capture sanitized score and live sheet outcomes for later inspection.

Callers own error isolation: a storage failure must never fail a job. Consent is user-global, independent of score-controlled telemetry settings.

Source code in src/marianne/telemetry/collector.py
def __init__(self, consent: ConsentRecord | None = None) -> None:
    self.consent = consent if consent is not None else ConsentRecord()
Methods:
collect
collect(score, status, sheets)

Stage one envelope only when enabled and the current grant is valid.

Source code in src/marianne/telemetry/collector.py
def collect(
    self, score: JobConfig, status: str, sheets: list[dict[str, Any]],
) -> bool:
    """Stage one envelope only when enabled and the current grant is valid."""
    if not score.telemetry.enabled:
        return False
    consent_id = self.consent.consent_id
    if consent_id is None:
        return False
    envelope = {
        "schema_version": 1,
        "collected_at": datetime.now(UTC).isoformat(),
        "consent_id": consent_id,
        "payload": {
            "score": sanitize_score(score),
            "job": {"status": status, "sheet_count": len(sheets)},
            "sheets": [sanitize_outcome(sheet) for sheet in sheets],
        },
    }
    spool = TelemetrySpool(score.telemetry.spool_path, consent_path=self.consent.path)
    with self.consent.collection_transaction():
        return spool.append(
            envelope, authorize=lambda: self.consent.consent_id == consent_id,
        )

TelemetryConfig

Bases: BaseModel

Default-off collection with local inspection before optional submission.

ConsentRecord

ConsentRecord(path=None)

Atomic, private consent record independent of any score's contents.

Do not cache this object’s state: long-running daemons must observe revocation. A caller may override the path for isolated tests, never via score YAML.

Source code in src/marianne/telemetry/consent.py
def __init__(self, path: Path | None = None) -> None:
    self.path = path if path is not None else Path.home() / ".marianne/telemetry-consent.json"
Attributes
state property
state

Return current on-disk consent, failing closed on malformed data.

consent_id property
consent_id

Opaque identifier for this grant; absent after revocation.

Methods:
grant
grant()

Persist explicit permission, retaining the ID of an existing grant.

Source code in src/marianne/telemetry/consent.py
def grant(self) -> None:
    """Persist explicit permission, retaining the ID of an existing grant."""
    with self.collection_transaction():
        self._write(ConsentState.GRANTED, self.consent_id or str(uuid4()))
collection_transaction
collection_transaction()

Serialize grant/revoke with authorized appends across every spool.

Always acquire this before a spool lock. Revocation then waits for any already-authorized append and no append can finish after revoke returns.

Source code in src/marianne/telemetry/consent.py
@contextmanager
def collection_transaction(self) -> Iterator[None]:
    """Serialize grant/revoke with authorized appends across every spool.

    Always acquire this before a spool lock. Revocation then waits for any
    already-authorized append and no append can finish after revoke returns.
    """
    self.path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
    lock_path = self.path.with_name(self.path.name + ".lock")
    fd = os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
    try:
        fcntl.flock(fd, fcntl.LOCK_EX)
        yield
    finally:
        os.close(fd)
revoke
revoke(*, spool_path=None, purge=False)

Stop collection first, then optionally remove the supplied local spool.

Source code in src/marianne/telemetry/consent.py
def revoke(self, *, spool_path: Path | None = None, purge: bool = False) -> None:
    """Stop collection first, then optionally remove the supplied local spool."""
    with self.collection_transaction():
        self._write(ConsentState.REVOKED, None)
        if purge and spool_path is not None:
            self.purge_spool(spool_path)
purge_spool staticmethod
purge_spool(path)

Remove staged envelopes; a missing spool is already purged.

Source code in src/marianne/telemetry/consent.py
@staticmethod
def purge_spool(path: Path) -> None:
    """Remove staged envelopes; a missing spool is already purged."""
    from marianne.telemetry.spool import TelemetrySpool

    TelemetrySpool(path).purge()

ConsentState

Bases: str, Enum

Missing or unreadable records never imply permission.

Functions:

sanitize_outcome

sanitize_outcome(outcome)

Copy outcome metrics and recursively redact nested diagnostic text.

Source code in src/marianne/telemetry/sanitizer.py
def sanitize_outcome(outcome: dict[str, Any]) -> dict[str, Any]:
    """Copy outcome metrics and recursively redact nested diagnostic text."""
    result: dict[str, Any] = _sanitize(outcome)
    return result

sanitize_score

sanitize_score(score)

Copy score data, removing private strings and telemetry transport settings.

Source code in src/marianne/telemetry/sanitizer.py
def sanitize_score(score: JobConfig | dict[str, Any]) -> dict[str, Any]:
    """Copy score data, removing private strings and telemetry transport settings."""
    raw = score if isinstance(score, dict) else score.model_dump(mode="json")
    payload = {key: value for key, value in raw.items() if key != "telemetry"}
    result: dict[str, Any] = _sanitize(payload)
    return result