Skip to content

Run the usage collector daily and show its results in Grafana - #1878

Merged
rdimitrov merged 5 commits into
mainfrom
rdimitrov-usage-collector-rollout
Nov 20, 2026
Merged

rdimitrov merged 5 commits into
mainfrom
rdimitrov-usage-collector-rollout

Conversation

@rdimitrov

Copy link
Copy Markdown
Member

Runs the usage collector (#1877) once a day in the hosted clusters and shows what it records in Grafana, on top of the dashboards (#1875) and alert rules (#1876) that are now provisioned from this repository. Everything is created by Pulumi behind one setting, mcp-registry:usageCollector.

This pull request sets it to "true" in staging, so merging deploys all of it to staging. Production has no setting and is not changed; deploy/Pulumi.gcpProd.yaml is untouched.

The setting

Value Effect
not set Nothing is created and nothing changes. Production today.
"true" Everything in the next section.
"false" Retired: the workload is removed and Grafana deletes the data source, dashboards and alert rules, which it would otherwise keep. The stored history and its backups stay.

It is only accepted on the hosted stacks with the standalone Gateway, which produces the request counters the collector scales by.

What staging gets on merge

  • Daily run: CronJob registry-usage-collector in monitoring, 01:15 UTC. A run collects every missing day of the last 13, so a missed run heals itself.
  • History: database registry_usage on the existing grafana-pg cluster, kept indefinitely.
  • Three confined database roles, as CNPG DatabaseRole objects with generated passwords. Neither CNPG Cluster object is modified.
    • registry_usage_collector owns the usage database.
    • registry_usage_grafana can only read what the dashboards query.
    • registry_usage_reader reads the registry database through pg_read_all_data and can write nothing. Today that database holds servers and schema_migrations.
  • Google access: roles/logging.viewer and a key for the existing registry-usage-reader service account. The account itself is not managed.
  • Grafana: a read-only PostgreSQL data source and the "Registry usage" and "Registry publishing" dashboards. Grafana restarts once to read them.
  • Two alert rules (a missed day; the log-based estimate drifting more than 10% from the gateway counters), only where mcp-registry:alertDiscordWebhook is set too. Staging does not have it, so staging gets no rules yet.
  • Backup: see below.
  • Image: ghcr.io/modelcontextprotocol/registry-usage-collector, built in deploy-staging.yml in a job of its own, which the deploy does not wait for, and in release.yml.

A run cannot hold up a deployment or the registry. Pulumi waits for none of the jobs, the volume or the database objects. The pod has small requests, hard limits and a priority below every other workload. It reads the registry database over one connection in short read-only transactions.

deploy/README.md has a new "Usage collector" section covering all of this.

Backup of the history, and its restore

grafana-pg is a single instance, so CronJob registry-usage-history-backup archives the usage database every day at 03:30 UTC, the same way the registry's backup works: pg_dump, read back and checksummed, then Restic with 28 daily snapshots.

  • It goes to a bucket of its own, mcp-registry-<environment>-usage-backups. The collector's service account is granted that bucket and no other, so it cannot reach the registry's backups.
  • The archive is made by registry_usage_collector in read-only transactions. No credential of Grafana's own database is used.
  • Retiring the collector keeps the bucket and its snapshots, and Pulumi never deletes the bucket.
  • The Restic password is a fixed, documented string, not a secret: a generated one would be lost with the Pulumi state that held it. The job creates its repository if there is none. A failed backup is not alerted on.
  • Restore: fetch the archive with Restic, then pg_restore --clean into registry_usage as its owner. It works on an empty database and on one the collector has written to since. The collector then collects whatever of the last 13 days is missing. The commands are in the README under "Restoring the history".

Steps by hand

  1. After the first staging build, make the GHCR package registry-usage-collector public. A new package is private and the clusters pull without credentials. Until then the daily job cannot start; nothing else is affected.
  2. After the deploy, start the first run instead of waiting for 01:15 UTC: kubectl -n monitoring create job --from=cronjob/registry-usage-collector usage-first-run. It takes about an hour.
  3. Optional: set mcp-registry:usageCollectorOrganizationAliases as an encrypted secret.

Where the staging deploy itself could fail

A failed Pulumi run in staging blocks later staging deploys, so these are the points I could not rule out locally:

  • Grafana must become ready. Pulumi waits for the chart's Deployment. The data source and dashboard provider files load on Grafana 12.4.14 locally, but with SQLite as Grafana's own database; staging's uses PostgreSQL.
  • The random provider plugin. Pulumi fetched it on demand locally with 3.272. CI uses 3.188.0, which obtains the gcp and kubernetes plugins the same way.
  • Applying a saved plan with values unknown at preview (three passwords and the key). The staging restore's key-to-Secret flow has the same shape and works.
  • The new bucket. Its name is free today, and the deploy account holds roles/storage.admin.

Already checked against staging: a server-side dry run of the three DatabaseRole objects and the Database was accepted, with nothing persisted.

Not waited for, so they cannot fail the deploy: the two CronJobs, the cache volume and CNPG's reconciliation. If the organization does not let Actions create a new package, the image job fails and the deploy still runs.

How it was checked

  • go -C deploy test ./..., with new tests for all three states of the setting, credential handling, role confinement and the backup; the deploy script tests; the collector's tests.
  • On a local PostgreSQL 17, with the roles shaped as DatabaseRole creates them, the collector's code as merged in Add usage collector for the hosted registry #1877 created its schema, read a registry database through the read-only role and stored 13 days from its synthetic fixtures. 35 checks confirm what each role can and cannot do.
  • Grafana 12.4.14 was started with the files this code generates. All 52 panel queries run as the read-only role for day, week and month; both rules evaluate correctly for fresh, stale, drifted, empty and missing-table data; "false" removes the data source, both dashboards and both rules and nothing else.
  • The backup's two scripts ran against that database. A snapshot restored into an empty database was identical to the original, and the dashboards and the collector worked on it.
  • The generated objects pass strict schema validation against both clusters' published schemas.
  • The collector image builds with ko for amd64 and arm64.

What only staging can show

  • How the CNPG operator reconciles the DatabaseRole and Database objects, including a password change and retirement.
  • Key creation, the IAM grant, the new bucket and its grants with the real deploy account.
  • The collector in the cluster: image pull, egress to Google and the two public reference sites, the cache volume, memory on real data.
  • Restic against Google Cloud Storage with the service account key, and pg_dump from the job's pod.
  • Whether CNPG recreates the database and roles after grafana-pg is rebuilt, which the restore relies on.
  • The real one-hour and two-hour alert timings.

rdimitrov and others added 4 commits November 20, 2026 22:26
One setting, mcp-registry:usageCollector, puts a daily CronJob in the
monitoring namespace, a database of its own on grafana-pg with confined
roles, a read-only Grafana data source, the Registry usage and Registry
publishing dashboards and, where alerting is configured, two alert rules.
It is on in staging. Production is unchanged until it is set there.

"false" retires it: the workload goes and Grafana deletes the data
source, the dashboards and the rules, which it would otherwise keep. The
stored history stays.

The collector gets an image of its own, built by the workflows that
build the registry's.

Co-authored-by: Copilot App <[email protected]>
The dashboard is provisioned in staging as well as production. The
README now also says what the registry read role can create, as checked
on both live databases.

Co-authored-by: Copilot App <[email protected]>
grafana-pg is a single instance, so its disk was the only copy of the
history. A daily CronJob now archives the registry_usage database and
stores it with Restic, as the registry's backup does, in a bucket of its
own. The archive is made by the role that owns that database, which
cannot read Grafana's own; the bucket is reached with the collector's
existing Google key, which is granted that bucket and no other.

The bucket and its snapshots are kept when the collector is retired, and
are never deleted by Pulumi. The README has the restore procedure.

Co-authored-by: Copilot App <[email protected]>
The collector now names the client that asked for a server most only
when every day in the range agrees on it, and leaves the name and its
share empty otherwise. The panel's description said nothing about the
blank cells that result.

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Dashboard queries overcount publications and daily servers, webhook-gated provisioning can leave stale failing alerts, and the restore instructions allow active collectors to keep writing during restoration.

4 open issues
What changed in this PR

Integrates the existing hosted usage collector with Pulumi and Grafana, enabling daily collection in staging without changing production configuration.

Changes:

  • Provisions the collector, database roles, credentials, cache storage, and retained history backups.
  • Adds usage and publishing dashboards, conditional alerts, and retirement cleanup.
  • Publishes collector images and adds deployment tests and operator documentation.
File Description
deploy/​README.md Documents setup, operation, backups, restore, and retirement.
deploy/​Pulumi.gcpStaging.yaml Enables collection in staging.
deploy/​pkg/​usagecollector/​usagecollector.go Defines shared settings, validation, and resource identifiers.
deploy/​pkg/​usagecollector/​usagecollector_test.go Tests settings and encrypted alias validation.
deploy/​pkg/​providers/​types.go Carries the collector’s Google credential.
deploy/​pkg/​providers/​gcp/​usage_collector.go Provisions Google access and retained backup storage.
deploy/​pkg/​providers/​gcp/​usage_collector_test.go Tests access scope and backup retention.
deploy/​pkg/​providers/​gcp/​provider.go Connects collector access to cluster provisioning.
deploy/​pkg/​k8s/​usage/​dashboards/​registry-usage.json Adds usage, client, and lookup visualizations.
deploy/​pkg/​k8s/​usage/​dashboards/​registry-publishing.json Adds publishing and catalogue visualizations.
deploy/​pkg/​k8s/​usage/​alerting/​rules.yaml Defines missed-day and estimate-drift alerts.
deploy/​pkg/​k8s/​usage/​alerting/​retired.yaml Defines explicit deletion of usage alerts.
deploy/​pkg/​k8s/​usage_grafana.go Provisions the data source, dashboards, and restart triggers.
deploy/​pkg/​k8s/​usage_collector.go Creates database resources and the daily collector workload.
deploy/​pkg/​k8s/​usage_collector_test.go Tests workloads, credentials, Grafana integration, and retirement.
deploy/​pkg/​k8s/​usage_backup.go Schedules database archives and Restic backups.
deploy/​pkg/​k8s/​monitoring.go Integrates collection into the monitoring deployment.
deploy/​pkg/​k8s/​grafana_alerting.go Accepts additional alert files and preserves annotations.
deploy/​pkg/​k8s/​grafana_alerting_test.go Updates the Grafana deployment test call.
deploy/​pkg/​k8s/​deploy.go Passes the registry database to monitoring.
deploy/​main.go Validates collector configuration before provisioning.
deploy/​go.sum Records checksums for the random provider dependency.
deploy/​go.mod Adds the random provider for generated passwords.
.ko.yaml Configures the collector image build.
.github/​workflows/​release.yml Publishes release-tagged collector images.
.github/​workflows/​deploy-staging.yml Builds staging collector images independently of deployment.

Review effort: Balanced

Comment thread deploy/pkg/k8s/grafana_alerting.go Resolved
Comment thread deploy/README.md Resolved
Retiring the collector now deletes its two alert rules even where the
webhook has been removed first or at the same time. Rules and the
contact point still need the webhook; a file that only deletes does not.

"Servers ever published" no longer counts first versions published
after the end of the selected range in its last week or month.

"Servers looked up per day" and the network count in "Names seen per
day" now average over every stored day, counting a day without rows as
zero and leaving out a day stored without the catalogue or the
network-owner table.

The restore steps now have the operator stop or wait for collector runs
that are already going, which suspending the CronJob does not do. Runs
carry a label so that they can be found.

Co-authored-by: Copilot App <[email protected]>
@rdimitrov
rdimitrov merged commit 7f3398f into main Nov 20, 2026
16 checks passed
@rdimitrov
rdimitrov deleted the rdimitrov-usage-collector-rollout branch November 20, 2026 20:55
rdimitrov added a commit that referenced this pull request Nov 20, 2026
…ls in Secret data (#1880)

The staging deploy that followed #1878 failed while applying its saved
plan (`apply-failed-plan-constraint,resource-secret`), and that blocks
every later staging deploy. This fixes it.

## Cause

`deploy/scripts/deploy_stack.py` runs `pulumi preview --save-plan` and
then `pulumi up --plan`. The Kubernetes provider moves a Secret's
`stringData` into `data` once it knows the values. Four of the usage
collector's Secrets took values that are random passwords, unknown at
preview:

- the three `registry-usage-*-password` Secrets (role name and generated
password)
- `registry-usage-collector` (the two database addresses, which contain
those passwords)

For those, the plan recorded `stringData` and the apply presented `data`
instead, so Pulumi rejected the resource:

```
kubernetes:core/v1:Secret::registry-usage-collector-password violates plan:
properties changed: +-stringData[...], =+data[...]
```

The repository's older Secrets that use `stringData` take values known
at preview (configuration), so they are not affected.
`registry-usage-collector-gcp` already used `data`, which is why it was
created.

## Fix

Those four Secrets now supply base64 values in `data` directly, which
has the same shape at preview and at apply. Values remain Pulumi
secrets; nothing moves into chart values, ConfigMaps or logs. A test
asserts that no usage Secret uses `stringData`.

## How it was checked

Nothing was run against the staging or production stack. Everything
below used Pulumi CLI 3.188.0 (the version CI pins), a local file
backend and the Kubernetes provider rendering manifests to a directory.

1. **Small programs with the Secret shapes.** `stringData` with a value
unknown at preview fails in every variant tried: mixed with a known
value, alone, the whole map as one output, wrapped as a secret, with and
without the optional aliases. `data` passes in each. This rules out the
CLI version, the random provider and the mixed map as causes.
2. **The real program** (`k8s.DeployMonitoringStack` with staging's
configuration, and a stand-in for the Google key):

| Step | Program | Result |
| --- | --- | --- |
| 1 | main before the fix, flag unset | applied |
| 2 | main before the fix, flag `"true"` | preview lists 18 creates and
2 updates; apply fails on `registry-usage-collector-password` with the
message above, leaving a partial state like staging's |
| 3 | this branch, flag `"true"`, on that partial state | preview lists
10 creates and 1 update (the Grafana Deployment, in place); apply
succeeds |
| 4 | this branch again | no changes |

No resource was replaced or deleted in step 3, and the stand-in key kept
its id and creation time. The rendered Secrets hold the role names,
32-character passwords and the two addresses under `data`; Grafana reads
its password through `secretKeyRef`.

`go -C deploy test ./...` and the script tests pass.

## What the next staging deploy does

It creates what the failed apply did not reach: the four Secrets, the
three `DatabaseRole` objects, the `Database`, and both CronJobs, and it
updates Grafana. Resources that already exist (bucket, service account
key, IAM grant, `registry-usage-collector-gcp`, PVC, PriorityClass, the
two dashboard ConfigMaps) have unchanged inputs and are left as they
are. The service account key is not recreated.

The local reproduction stops at the first failing resource, as staging
did, so this deploy is the first time the CNPG objects, the CronJobs and
the Grafana update are applied to a real cluster. A server-side dry run
of the four CNPG objects was accepted in staging earlier.

## Not changed

`deploy/pkg/providers/gcp/provider.go` has an older Secret
(`AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` from an HMAC key) with the
same pattern. It exists in both stacks and deploys today; it would hit
this error only if its key were created or replaced in a planned
deployment. Left for a separate change.

The manual steps after a successful deploy are unchanged from #1878:
make the `registry-usage-collector` image package public, then start the
first run by hand.

Co-authored-by: Copilot App <[email protected]>
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.

2 participants