RFC: persist audit results to a warehouse table

Ouverte
#6,295 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 4 jours

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
32/100
Type d'issue
Fonctionnalité
Clarté
Clairement spécifiée
Activité
Active
Stack technique
python, sql

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.
Langage dominant
Python
Étoiles
3.3k
Forks
471
Merge moyen
6 j 7 h
PR mergées (30 j)
35

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de SQLMesh/sqlmesh

Toutes les issues de SQLMesh/sqlmesh

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.