Skip to content

transmitter

transmitter

Explicit submission of inspected envelopes; failures retain local data.

Classes

TelemetryTransmitter

TelemetryTransmitter(config, consent=None, *, transport=None)

Lazy, bounded HTTP client. No receiver or background sending is configured.

A successful HTTP response acknowledges a whole batch. Retrying after a lost response may deliver duplicates; the receiver must handle that possibility.

Source code in src/marianne/telemetry/transmitter.py
def __init__(
    self, config: TelemetryConfig, consent: ConsentRecord | None = None, *,
    transport: httpx.AsyncBaseTransport | None = None,
) -> None:
    self.config = config
    self.consent = consent if consent is not None else ConsentRecord()
    self._transport = transport
Methods:
submit async
submit(envelopes)

Check fresh consent at the network boundary; never raise into a run.

Source code in src/marianne/telemetry/transmitter.py
async def submit(self, envelopes: list[dict[str, Any]]) -> bool:
    """Check fresh consent at the network boundary; never raise into a run."""
    try:
        if not envelopes or not self._ready():
            return False
        identity = self.consent.consent_id
        if identity is None or not self._endpoint_allowed() or any(
            record.get("consent_id") != identity for record in envelopes
        ):
            _logger.warning("telemetry.submission_refused")
            return False
        if await self._post(envelopes):
            return True
        _logger.warning("telemetry.submission_failed")
    except Exception:  # noqa: BLE001 — best-effort telemetry; never log payload or URL
        _logger.warning("telemetry.submission_failed")
    return False
flush async
flush(*, dry_run=False)

Return submitted count, zero for a no-op, or -1 on a retained failure.

Source code in src/marianne/telemetry/transmitter.py
async def flush(self, *, dry_run: bool = False) -> int:
    """Return submitted count, zero for a no-op, or -1 on a retained failure."""
    try:
        if dry_run or not self._ready():
            return 0
        spool = TelemetrySpool(self.config.spool_path, consent_path=self.consent.path)
        with spool.submission() as acquired:
            if not acquired:
                _logger.warning("telemetry.submission_busy")
                return -1
            snapshot = spool.read()
            if not snapshot:
                return 0
            if not await self.submit(snapshot):
                return -1
            if not spool.acknowledge(snapshot):
                _logger.warning("telemetry.spool_changed_during_submission")
                return -1
            return len(snapshot)
    except Exception:  # noqa: BLE001 — leave failed storage/transport visible and retryable
        _logger.warning("telemetry.flush_failed")
        return -1

MockTelemetryTransmitter

MockTelemetryTransmitter(config, consent=None, *, succeed=True)

Bases: TelemetryTransmitter

Network-free test double exercising the real consent and prune paths.

Source code in src/marianne/telemetry/transmitter.py
def __init__(
    self, config: TelemetryConfig, consent: ConsentRecord | None = None, *,
    succeed: bool = True,
) -> None:
    super().__init__(config, consent)
    self.succeed = succeed
    self.submissions: list[list[dict[str, Any]]] = []

Functions: