RFC: persist audit results to a warehouse table

Offen
#6,295 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 4 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
32/100
Issue-Typ
Feature
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python, sql

Rechercherichtung

Start with SnapshotEvaluator.audit, Scheduler._audit_snapshot, run_merged_intervals, create_state_table, and the audit-results configuration and janitor entry points. The RFC proposes separate work for the completion callback, recorder buffering and flushing, wiring into plan, run, and sqlmesh audit, retention, documentation, and engine-adapter integration tests. A newcomer should not begin implementation until the open questions about strict versus warn-only failures, table naming and location, error recording, and schema reuse are resolved.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

RFC: persist audit results to a warehouse table

This revisits #3064. It was closed because Tobiko Cloud covered it. Now that Cloud is discontinued, I'd like to contribute an open-source version. Below are a proposed design and PR breakdown. Please push back on anything before I start.

Goal

Opt in to one summary row per audit execution, written to a table in the warehouse. Teams can then build audit history and data-quality dashboards with plain SQL. Today the only option is a custom GenericNotificationTarget. It sees failures only, and it gets no interval, environment or run context.

Out of scope: storing failing rows, as dbt's store_failures does. Only the summary is stored: status, failing-row count, and the rendered query.

Config
audit_results:
  enabled: true                  # default false
  gateway: warehouse             # default: the selected gateway
  table: sqlmesh.audit_results   # default
  include_query: true            # store the rendered audit SQL
  retention: 4 weeks             # optional; enforced by the janitor
Table

There is one row per audit execution, for plans, runs and sqlmesh audit, in both prod and dev. Timestamps are epoch-ms BIGINT, as in the state tables. There is no primary key.

Column(s) Contents
id, recorded_ts, started_ts, duration_ms Row id and timing
job_type (plan / run / audit), job_id Which job ran the audit. job_id is the plan id, or the run id from the existing CorrelationId.
environment, gateway, is_dev Where it ran
node_name, node_type (model / standalone_audit), snapshot_identifier, snapshot_version What was audited. snapshot_identifier joins to _snapshots.
audit_name, audit_args (JSON), blocking Which audit. blocking is the resolved value, including force_non_blocking.
status (passed / failed / skipped / error), failed_row_count, error_message The outcome. error means the audit query itself raised.
query, query_dialect The rendered SQL, in the dialect of the gateway it ran on (truncated)
interval_start_ts, interval_end_ts, execution_ts Which data was audited
sqlmesh_version

Schema evolution: columns are additive only. On first use SQLMesh creates the table, or adds any missing columns. It uses create_state_table, which gives Athena an Iceberg table and drops the primary key on BigQuery.

Design
  • Capture:
    • A keyword-only on_audit_complete callback on SnapshotEvaluator.audit receives an outcome for every audit, including audits whose query raised, along with its duration.
    • Scheduler._audit_snapshot binds the context: job, environment and interval.
    • Rows are buffered before NodeAuditsErrors is raised.
    • The execution observer from #6051 is unchanged.
  • Write:
    • Rows are buffered thread-safely.
    • The buffer is flushed with one insert_append in the outer finally of run_merged_intervals, plus an early flush every ~1,000 rows. That gives one load per run rather than one per model, which matters for BigQuery's load-job quotas.
    • The write runs outside any model, state or WAP transaction.
  • Failure semantics (the main open question):
    • Persisting is strict: if it's enabled and the write fails, the job fails.
    • A pre-flight check creates or verifies the table before any model work, so a missing permission fails fast instead of after a long backfill.
    • A write failure never masks an error that is already being raised; it is attached as a note instead.
    • I'm happy to switch to warn-only, or to add fail_on_error (default true), if you'd prefer that observability never fails a run.
  • Retention: when retention is set, the janitor deletes old rows.
  • Known gaps (to be documented):
    • A hard kill loses the buffer.
    • Audits skipped because an upstream node failed are not recorded.
PR plan

Each PR is small and lands independently on main. Everything is inert until PR 5 wires it in, behind enabled: false.

  1. fix(audit): render audit errors in the executing gateway's dialect. This is an independent existing bug: errors currently use the default gateway's dialect.
  2. feat(audit): contract types and table schema.
  3. feat(config): the audit_results config section.
  4. feat(evaluator): per-audit completion hook. No behaviour change.
  5. feat(audit): AuditResultsRecorder (buffer, ensure table, flush).
  6. feat: wire it into plan, run and sqlmesh audit, plus docs.
  7. feat(janitor): retention.
  8. test: engine-adapter integration coverage.

Points I'd especially like your view on:

  • strict failure versus warn-only;
  • the table name and default location;
  • recording error status;
  • reusing create_state_table for a table that isn't state;
  • whether you'd rather fold PRs 2–5 into fewer PRs.
Vorherrschende Sprache
Python
Sterne
3.3k
Forks
471
Ø Merge
6 T. 7 Std.
Gemergte PRs (30 T.)
35

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus SQLMesh/sqlmesh

Alle Issues in SQLMesh/sqlmesh

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.