Skip to content

Fix database operation usage resource attribution - #13523

Merged
ChiragAgg5k merged 4 commits into
mainfrom
fix/database-operation-resource-id
Sep 8, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
fix/database-operation-resource-id

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Fix empty database-card operation statistics reported in the self-hosted 2.0.0 testing thread.

Database operation handlers set resourceType=database and the internal database sequence, but leave resourceId empty. Request shutdown fills that empty field with the project ID. Project totals remain correct, while queries filtered by the actual database ID return no operations.

  • Set the public database ID before addMetric() snapshots metadata for document/row CRUD, list, bulk operations, and increment/decrement.
  • Resolve the public ID for each database group when recording transaction commits, including commits spanning two databases.
  • Add real API regression coverage. No console or deployment configuration changes.

This fixes newly recorded events; it does not backfill historical usage.

Verification

Tested a disposable local self-hosted stack with PHP 8.5.10, PostgreSQL, Redis, ClickHouse, and the 2.0.0 console, running the changed source. Requests traversed the HTTP API → usage queue → stats worker → ClickHouse → usage API; no usage events were injected directly.

Check Result
New regression against unchanged handlers Both API variants failed: database-filtered reads were 0 instead of 3, after project-total assertions passed
Same regression with fix 2 tests, 222 assertions passed
tests/e2e/Services/Usage/UsageCustomServerTest.php 9 passed, 1 skipped; 299 assertions (stats-disabled-only test skipped because stats were enabled)
Pint and PHP syntax checks All 13 changed PHP files passed
git diff --check Passed

The regression covers legacy documents and TablesDB rows, empty/nonempty lists, CRUD/upsert, increment/decrement, bulk create/update/upsert/delete, and a transaction spanning two databases. It checks each database's exact reads/writes, unchanged project totals, and zero matches for project/unknown IDs.

Commands inside the local appwrite container:

test tests/e2e/Services/Usage/UsageCustomServerTest.php --filter=testDatabaseOperationResourceId
test tests/e2e/Services/Usage/UsageCustomServerTest.php

Local test topology used a dedicated stats-usage worker following the supported separate-worker pattern; the combined worker excluded that queue. This avoided local combined-worker backlog. ClickHouse settings were restored to defaults before the final before/after runs. Existing instances and repository deployment configuration were not changed. This is focused regression verification, not a full release/service-matrix run; separate DocumentsDB/MongoDB and VectorsDB adapters were not exercised. Independent source/test review found no issues.

Console screenshots

Actual browser captures from the local stack after generating different workloads through the API.

Database grid: both cards have their own nonzero usage

Usage Alpha: 230 reads / 20 writes. Usage Beta: 35 reads / 7 writes. Reads count returned rows, not just HTTP requests.

Database grid showing distinct nonzero reads and writes for Usage Alpha and Usage Beta

Usage → Databases: breakdown resolves the real database names

Project reads total 265, correctly split into 230 + 35 rather than attributed to the project ID.

Database read usage grouped under Usage Alpha and Usage Beta rather than the project ID

@greptile-apps

greptile-apps Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR corrects database-operation usage attribution by recording each database’s public resource ID alongside its internal sequence.

  • Updates document and row CRUD, list, bulk, increment, and decrement metric emitters.
  • Resolves the public database ID separately for each database represented in a committed transaction.
  • Adds end-to-end coverage for both Databases and TablesDB APIs, including cross-database transactions and project-total checks.

Confidence Score: 5/5

The PR appears safe to merge with no actionable new findings.

No blocking correctness, security, or repository-rule issues remain in the current changeset.

Important Files Changed

Filename Overview
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Resolves each transaction operation group’s public database ID before recording post-commit write usage.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php Adds the resolved public database ID to create-operation usage metadata.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Get.php Adds the resolved public database ID to single-record read usage metadata.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php Adds the resolved public database ID to list-operation read usage metadata.
tests/e2e/Services/Usage/UsageCustomServerTest.php Adds comprehensive attribution regression coverage across both database API families and multi-database transactions.

Reviews (3): Last reviewed commit: "Merge branch 'main' into fix/database-op..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/database-operation-resource-id (after).

Metric Before After Change
🚀 Requests/sec 235.24 236.67 ⚪ +0.6%
⏱️ Latency P50 74.06 ms 73.5 ms ⚪ -0.8%
⏱️ Latency P95 175.84 ms 174.41 ms ⚪ -0.8%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 73.5 174.41 14,820 236.67 -1.43
Account 143.43 258.33 780 13.07 -0.67
TablesDB 70.91 128.92 8,060 130.84 -0.58
Storage 68.95 148.11 3,900 65.32 -0.08
Functions 109.13 216.69 2,080 35.47 +2.24

Top API waits (after)

API request Max wait (ms)
storage.files.create 394.76
storage.buckets.create 389.52
account.prefs.update 386.98
account.name.update 381.22
storage.files.preview 370.13

@ChiragAgg5k
ChiragAgg5k merged commit 5070d71 into main Sep 8, 2026
46 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/database-operation-resource-id branch September 8, 2026 09:35
@ChiragAgg5k ChiragAgg5k mentioned this pull request Sep 10, 2026
2 of 4 tasks
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