Find a lost TRN - Analytics
Find a lost TRN sends data to BigQuery for the data team in two ways:
- Database replication. Airbyte copies the database tables into BigQuery. Airbyte runs outside the app; this repository decides which columns it copies and which BigQuery hides.
- Request events. The dfe-analytics gem records every web request as an event.
The gem can also send an event for every database change. We switch that off, because Airbyte replaces it.
This page explains how both work, which data leaves the service, what to do when the schema changes, and how to switch an environment on or off.
How request events work
- After every controller action,
ApplicationController#trigger_request_eventbuilds an event. It holds the method, path, query string, referer, user agent, response status, an anonymised IP, the session ID, and the staff user's ID while staff are signed in. -
lib/dfe/analytics/filtered_request_event.rbpasses the query string, and the referring page's query string, through Rails'filter_parameters, so the event filters the same parameters the logs do. The gem's own event class doesn't filter, which is why we override it. Filtering the referer matters because Devise's password reset and invitation links carry live tokens. The event leaves out any referer it can't parse. - The app queues the event on the worker's
analyticsSidekiq queue, and the worker sends it to theeventstable in thefaltrn_events_<environment>dataset.
The worker authenticates to Google with Azure workload identity federation, so no keys are stored. Terraform creates the dataset and table, gives the worker that identity and sets GOOGLE_CLOUD_CREDENTIALS (terraform/aks/dfe_analytics.tf).
Events only go out while the send_analytics_events feature flag is active. It's inactive by default in every environment, and you can switch it in the support interface under /support/features.
Only activate the flag in an environment that has BigQuery credentials. Without them, every request queues a job that fails and retries.
Which data leaves the service
| File | What it controls |
|---|---|
config/analytics.yml |
The columns Airbyte copies. Anything not listed stays in the service. |
config/analytics_hidden_pii.yml |
Copied columns that get the hidden policy tag, so only people approved for PII can read them. |
config/analytics_blocklist.yml |
Columns deliberately not copied. |
The gem builds Airbyte's list of streams from analytics.yml, and applies the hidden policy tag to the columns in analytics_hidden_pii.yml after each sync.
The PII review in #298 decided which columns BigQuery hides. The review didn't cover these columns, so they stay in the blocklist until the data team decides on them:
- the raw DQT response
- rejected form values
- the TRN a user typed in
- staff sign-in IPs and unconfirmed emails
We never copy credentials (password hashes and tokens), console audit text, or the columns TrnRequest encrypts, which would only reach BigQuery as ciphertext.
When a migration adds a column
The app refuses to boot if a model attribute is in neither analytics.yml nor the blocklist. So for each new column, decide whether the data team needs it:
-
To copy it, add it to
config/analytics.yml. If it holds personal data, also add it toconfig/analytics_hidden_pii.yml. -
To keep it out, regenerate the blocklist:
SUPPRESS_DFE_ANALYTICS_INIT=1 bin/rails dfe:analytics:regenerate_blocklist bin/lint
The check covers model attributes only. It skips tables without a model, and it logs instead of raising when the database is unreachable or has pending migrations. CI boots against a migrated database, so it catches a column you forgot.
A spec also fails if a hidden column isn't in analytics.yml. Without that check, the column would never reach BigQuery, so it would never be hidden either.
How database replication works
- Postgres runs with logical replication on (
pg_airbyte_enabled), so it keeps every change in a replication slot,airbyte_slot, until Airbyte reads it. - Each namespace on the cluster has one Airbyte instance, which the infrastructure team runs. FaLTRN has a workspace in it, and terraform creates a source (our database), a destination (BigQuery) and a connection between them (
terraform/aks/airbyte.tf). - Every 15 minutes, the connection copies new changes into the
faltrn_airbyte_<environment>dataset. It appends rather than merges, so each table in BigQuery holds every version of each row.
On every deploy, terraform runs a Kubernetes job called airbyte-stream-update. The job runs rake dfe:analytics:airbyte_deploy_tasks, which queues a job on the worker. That job waits for migrations, refreshes the Airbyte connection so it picks up schema changes, runs a sync, and tags the hidden columns.
The worker tags the columns with the request events credentials. So terraform refuses airbyte_enabled without enable_dfe_analytics_federated_auth, and hands the app's own service account (app-wif-faltrn-<environment>) to the Airbyte module, which makes it an owner of its dataset.
Switching analytics on for an environment
Each environment opts in separately, in two deploys. Airbyte has to wait for the first, for two reasons. Its module looks up the app's service account, which the first deploy creates. And its setup job creates the replication slot as soon as it runs: if Postgres hasn't yet restarted with logical replication, psql carries on past the error and exits 0, so terraform records the job as done and never reruns it.
Every deploy workflow authenticates terraform to Google with this repository's workload identity provider, which a project owner sets up once with the dfe_analytics module's authorise_workflow.sh. Running terraform locally, for any environment, needs Google Cloud access and gcloud auth application-default login first. The terraform holds the teaching-qualifications project's policy tag taxonomy, its hidden tag (one tag covers both the events table and the Airbyte tables) and its KMS key in terraform/aks/dfe_analytics.tf, since they're the same for every environment.
First, request events and logical replication:
-
In
terraform/aks/workspace_variables/<environment>.tfvars.json, setenable_dfe_analytics_federated_authandpg_airbyte_enabledtotrue.Turning on
pg_airbyte_enabledrestarts the database server several times. Deploy in a window agreed with the infrastructure team. -
Merge and deploy.
-
Activate
send_analytics_eventsin the environment's support interface.
Then Airbyte, once the infrastructure team has created a FaLTRN workspace in the namespace's Airbyte instance, added AIRBYTE-CLIENT-ID, AIRBYTE-CLIENT-SECRET and AIRBYTE-WORKSPACE-ID to the environment's infra Key Vault, and set up an alert on the database's storage. Nothing caps the WAL a replication slot holds on Azure Postgres, so a stalled Airbyte fills the disk:
- In the same file, set
airbyte_enabledtotrueandairbyte_connection_statusto"active". If the module's resource names are too long for the environment name, setairbyte_environmentto a shorter one, such as"preprod". - Merge and deploy.
- Check that the first sync shows in the Airbyte UI at
https://airbyte-<namespace>.<cluster domain>, and that the worker logs showAirbyteDeployJoband the policy tag job finishing without errors. Theairbyte-stream-updateKubernetes job only queues that work, so its success proves nothing about the sync or the tags. - Ask the data team to confirm that the data has arrived and the hidden columns are tagged.
Switching analytics off
To stop request events, deactivate send_analytics_events. This takes effect immediately, with no deploy.
While nothing reads airbyte_slot, Postgres keeps every change for it, and the database server's storage fills up. Setting airbyte_connection_status to "inactive" stops reading, so only use it for a short pause, and make sure storage alerts are in place.
To remove replication, in this order:
-
Set
airbyte_connection_statusto"inactive"and deploy. -
Drop the slot and publication from a console on the database:
ALTER ROLE CURRENT_USER WITH REPLICATION; SELECT pg_drop_replication_slot('airbyte_slot'); ALTER ROLE CURRENT_USER WITH NOREPLICATION; DROP PUBLICATION airbyte_publication; DROP OWNED BY airbyte_replication; DROP ROLE airbyte_replication; DROP TABLE airbyte_heartbeat;The database user only has the replication attribute while it needs it, which is how the Airbyte module creates the slot too. Dropping the
airbyte_replicationrole matters: it can read every table. -
While
airbyte_enabledis stilltrue, ask the infrastructure team to runterraform destroy -target=module.airbytefor the environment. Once the flag isfalse, the Airbyte provider has no credentials, so terraform can't delete the source, destination and connection it still holds in state. This destroys the Airbyte dataset in BigQuery, including its data. Check with the data team first. -
Set
airbyte_enabledtofalseand deploy. -
Set
pg_airbyte_enabledtofalseand deploy, which restarts the database server. Postgres won't start with logical replication off while a logical slot still exists, which is why the slot goes first.
Setting enable_dfe_analytics_federated_auth to false fails while the events table exists, because the table has deletion protection. Once the data team agrees, ask the infrastructure team to delete the table, then deploy.
Running locally
Analytics is off locally, because the flag is inactive. To send events from your machine:
- Get a BigQuery key, following the gem's BigQuery setup guide.
- Set
BIGQUERY_API_JSON_KEY,BIGQUERY_PROJECT_ID,BIGQUERY_DATASETandBIGQUERY_TABLE_NAMEin.env.development.local. - Activate
send_analytics_eventsin your local support interface. - Run Sidekiq, since the worker sends the events.