Skip to content

fix(infra): inject GCP_PROJECT_ID/GCP_REGION into ingestion Job (D113) - #85

Merged
GMartin-Data merged 2 commits into
mainfrom
fix/dbt-invocation-env-vars
May 25, 2026
Merged

GMartin-Data merged 2 commits into
mainfrom
fix/dbt-invocation-env-vars

Conversation

@GMartin-Data

Copy link
Copy Markdown
Owner

Contexte

Premier run Scheduler observé en conditions réelles (Phase 4 du chantier inter-bloc) le lundi 2026-05-25 04:00 UTC. L'ingestion s'est exécutée avec succès (exit 0, 8 sources OK), mais le Cloud Run Job datatalent-dbt n'a pas été déclenché. Les marts sont restés stale (marts.fct_offres.last_modified_time figé au 2026-05-23 10:03, jour de la validation manuelle de PR #83).

Diagnostic en 4 steps (executions list / image deployed / logs structlog / BigQuery freshness) — voir section "Incident notable" ci-dessous.

Cause racine identifiée : _invoke_dbt() (introduit en D108, PR #81) lit GCP_PROJECT_ID et GCP_REGION depuis os.environ pour construire le nom complet de la ressource Cloud Run cible. Le module Terraform cloud_run/ n'injecte ces variables dans aucun container. La garde environnementale a court-circuité l'invocation avec un log dbt_invocation_skipped_missing_env en niveau warning, donc invisible côté monitoring Niveau 1.

D113 ajoutée à notes-projet.md pour acter le choix d'implémentation et le pattern doctrinal (sixième occurrence du pattern "découverte à l'apply").

Décisions techniques

Choix Voie retenue Voies écartées
Où injecter les env vars A — var.static_env_vars (map) côté module Terraform, instanciée au root pour datatalent-ingestion uniquement B (en dur dans module — couple à 2 vars spécifiques) ; C (auto-détection metadata server — couple le code applicatif au runtime GCP) ; D (sys.exit(1) — durcissement de visibilité, pas fix de la cause)
Symétrie de design Nouveau bloc dynamic "env" parallèle au secret_env_vars existant Bloc statique inline (perte de réutilisabilité)
Périmètre du fix Module + instanciation ingestion uniquement Pas de modification cloud_run_dbt (map vide = no-op naturel via for_each)
Garde Python _invoke_dbt() Inchangée (warning silencieux préservé) Promotion warning → error reportée en TODO-8 / D114 future (orthogonal au fix)

Changements

  • infra/modules/cloud_run/variables.tf — ajout variable "static_env_vars" (map(string), default {})
  • infra/modules/cloud_run/main.tf — ajout bloc dynamic "env" itérant sur static_env_vars ; suppression du bloc env statique commenté ll. 26-30 (TODO-3 du rétro-todo absorbé : le pattern est désormais utilisé en vrai, plus besoin de la version documentaire)
  • infra/main.tf — argument static_env_vars = { GCP_PROJECT_ID, GCP_REGION } ajouté à l'instanciation module.cloud_run (ingestion). Aucune modification de module.cloud_run_dbt.

Incident notable durant l'observation Phase 4

Le run du 2026-05-25 04:00 UTC a produit l'asymétrie suivante en BigQuery :

raw.adzuna                  last_modified 2026-05-25 04:05:14   ← frais
raw.bmo                     last_modified 2026-05-25 04:05:48   ← frais
raw.france_travail          last_modified 2026-05-25 04:02:33   ← frais
raw.geo_communes            last_modified 2026-05-25 04:05:57   ← frais
raw.urssaf_*                last_modified 2026-05-25 04:05:*    ← frais
marts.fct_offres            last_modified 2026-05-23 10:03:33   ← stale (J-2)

Diagnostic des logs structlog de l'exécution datatalent-ingestion-hj7gg :

2026-05-25T04:05:59.070144Z [warning] event=dbt_invocation_skipped_missing_env
  extras={'gcp_project_id_set': False, 'gcp_region_set': False, 'module': '__main__'}

main.py a fait son travail (la garde a tracé pourquoi elle ne déclenchait pas dbt), mais le mode warning + exit code 0 du Job ingestion (toutes sources OK) → aucune alerte D105 déclenchée. Mode de défaillance silencieux exactement contraire à D72, deuxième prolongation après D111.

Validation

terraform plan post-edit

Plan: 0 to add, 1 to change, 0 to destroy.

# module.cloud_run.google_cloud_run_v2_job.this will be updated in-place
  ~ containers {
      + env { name = "GCP_PROJECT_ID"; value = "datatalent-glaq-2" }
      + env { name = "GCP_REGION"; value = "europe-west1" }
    }

Aucun diff sur module.cloud_run_dbt (validation expérimentale du choix Voie A : map vide = no-op).

terraform apply

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

Run end-to-end post-apply

Invocation manuelle gcloud run jobs execute datatalent-ingestion --wait :

Execution [datatalent-ingestion-t5zl4] has successfully completed.

Logs structlog de l'exécution t5zl4 :

2026-05-25T08:57:04.109669Z [info] event=dbt_invoked
  job=projects/datatalent-glaq-2/locations/europe-west1/jobs/datatalent-dbt
  operation=projects/datatalent-glaq-2/locations/europe-west1/operations/a7a31eca-0d9d-44a9-a441-e8bf5f01840f

(comparer avec le run cassé du matin qui émettait dbt_invocation_skipped_missing_env avec gcp_project_id_set: False / gcp_region_set: False)

Exécution dbt déclenchée :

EXECUTION             COMPLETE  CREATED                  RUN BY
datatalent-dbt-5tm7t  1 / 1     2026-05-25 08:57:04 UTC  sa-ingestion@datatalent-glaq-2.iam.gserviceaccount.com

sa-ingestion comme RUN BY confirme l'invocation programmatique depuis main.py (vs invocations manuelles du 2026-05-23 par gregory.martin.data).

BigQuery marts.fct_offres rafraîchi :

table_id     row_count  creation_time        last_modified_time
fct_offres   11490      2026-05-25 08:58:48  2026-05-25 08:58:48

(était figé à 2026-05-23 10:03:33 / 10269 rows avant le fix — soit J-2 et +1221 rows accumulés depuis)

terraform plan post-apply

No changes. Your infrastructure matches the configuration.

Règle D31 respectée — zéro diff post-apply.

Décisions liées

  • D113 (cette PR) — env vars statiques GCP_PROJECT_ID / GCP_REGION côté Terraform
  • D108 — origine de la dépendance env var (orchestration main.py → dbt)
  • D72 — mode de défaillance silencieux refusé, prolongé pour la 2e fois après D111
  • D70 — asymétrie révélée : les vars existaient déjà en CI (vars.GCP_PROJECT_ID / vars.GCP_REGION) mais pas dans le runtime prod du container
  • D111 — précédent immédiat (pattern jumeau : défaut implicite côté image, ici côté config)
  • D34 — modules à la demande (variable ajoutée parce qu'un besoin concret émerge)

Refs : T9.17 (origine de _invoke_dbt()), TODO-3 absorbé (rétro-todo), TODO-8 créé (durcissement garde, Bloc 2).

Add a static_env_vars variable (map(string), default {}) and a parallel
dynamic env block in the cloud_run module, symmetric with the existing
secret_env_vars pattern. Modules consumers can now inject non-sensitive
runtime configuration that container code reads via os.environ, while
sensitive values keep going through secret_env_vars.

Remove the commented-out documentary env block (lines 26-30) now that
the real pattern exists in the module.

No behavioral change in production: instances that do not pass
static_env_vars get an empty map, which expands to a no-op for_each.
…D113)

ingestion/main.py invokes the dbt Cloud Run Job at end of pipeline
(D72, D108). _invoke_dbt() reads GCP_PROJECT_ID and GCP_REGION from
os.environ to build the fully-qualified target resource name
(projects/.../locations/.../jobs/datatalent-dbt), which is not
auto-detectable from ADC.

These two variables were missing from the container env, causing the
environmental guard of _invoke_dbt() to skip the invocation silently
with a warning-level log. Observed on the first real Scheduler run
(2026-05-25 04:00 UTC): ingestion SUCCEEDED, dbt never triggered,
marts stale (raw.* fresh / marts.fct_offres last_modified J-2).

End-to-end validation post-apply: manual gcloud run jobs execute on
datatalent-ingestion → event=dbt_invoked → datatalent-dbt-5tm7t
SUCCEEDED → marts.fct_offres recreated at 08:58:48 UTC (11490 rows).

Refs: D113, T9.17
@GMartin-Data
GMartin-Data merged commit 2997c49 into main May 25, 2026
6 checks passed
@GMartin-Data
GMartin-Data deleted the fix/dbt-invocation-env-vars branch May 25, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant