Skip to content

Trace database statements and count the connection pool - #272

Open
qin-guan wants to merge 16 commits into
mainfrom
claude/intelligent-volta-g4bc0k-db-tracing
Open

qin-guan wants to merge 16 commits into
mainfrom
claude/intelligent-volta-g4bc0k-db-tracing

Conversation

@qin-guan

@qin-guan qin-guan commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

What

Follow-up you asked for: "add otel monitoring to the database operations as well". Stacked on #270, which has the metrics for statements (fbs.db.*) but no traces and nothing about the connection pool.

MySqlConnector already makes a span of each statement, in the trace of the request that ran it, and counts its connection pool. They only reach OpenTelemetry for those that ask for them by name, and the API didn't. So this is two names in Program.cs: AddSource("MySqlConnector") and AddMeter("MySqlConnector"), and a processor that keeps the SQL out of the spans (below).

What you get
Traces In each request's trace, an Open span for getting a connection and an Execute span for each statement, with db.operation (select, insert, …), db.name, db.user and the server's address
Pool db.client.connections.usage (state idle or used), .max, .idle.max, .idle.min, .pending_requests, .timeouts, .wait_time, .use_time, .create_time

So a slow request can be told from a slow database, and a busy pool from an idle one. The README has a Traces section, the pool metrics in its table, and what to alert on: pending_requests above 0 for long, timeouts going up, wait_time over a few milliseconds.

Worth a look

  • The SQL is not in the spans. MySqlConnector puts it in db.statement, and a first version of this PR exported it. A review pointed out that a batch of inserts has values in the statement and not in parameters. I checked, with a batch of two bookings: the ids and times are in the SQL (VALUES(N'862f42f6-…', …, '2026-10-04 01:00:00.000', …)), while the text people typed (PocName, PocPhone, Description) is in parameters in the version of SqlSugar there is. Nothing says it always will be, so DatabaseSpans (a span processor) takes the statement out of every MySqlConnector span and leaves db.operation in its place. To see which statement was slow, TiDB has it in its slow query log and information_schema.statements_summary.
  • It runs before the exporter: UseOtlpExporter puts the exporter last whatever order things are added in (I looked at the processors of the running API), and a test goes all the way to a collector.
  • The password isn't in a span. db.connection_string is in them, and I checked, with a password in the connection string, that MySqlConnector leaves it out.
  • Nothing is sent anywhere until OTEL_EXPORTER_OTLP_ENDPOINT is set, as before.

Tests

4 new (DatabaseTelemetryTests), using OpenTelemetry.Exporter.InMemory to see what an exporter would, and a small HTTP collector of its own to see what is sent:

  • a GET /Me has a select and an insert in its trace, with db.operation;
  • a booking of two slots has no db.statement, and not the ids of the bookings it made, in any span;
  • the same, in the bytes an OTLP endpoint is sent, which have the driver's spans and db.operation in them;
  • the pool metrics are exported.

Without the processor, three of them fail; with the processor but keeping the statement, the two about what is sent fail. The whole suite passes (553: the 549 of #270, and these).

🤖 Generated with Claude Code

https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T06:26:56.573410Z 197b24f PR opened
🔒 Security Review ✅ Completed 2026-10-02T06:31:21.820425Z 197b24f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 197b24f86b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Fbs.WebApi/Program.cs Resolved
@qin-guan
qin-guan force-pushed the claude/intelligent-volta-g4bc0k-metrics branch from 63fc9c2 to d0dd800 Compare October 2, 2026 06:52
@qin-guan
qin-guan force-pushed the claude/intelligent-volta-g4bc0k-db-tracing branch from 197b24f to 304faad Compare October 2, 2026 06:52
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Deploying 3sib-fbs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3a96efd
Status: ✅  Deploy successful!
Preview URL: https://30919794.3sib-fbs.pages.dev
Branch Preview URL: https://claude-intelligent-volta-g4b-logl.3sib-fbs.pages.dev

View logs

@qin-guan
qin-guan force-pushed the claude/intelligent-volta-g4bc0k-metrics branch from d0dd800 to dc2d0d6 Compare October 3, 2026 09:25
@qin-guan
qin-guan force-pushed the claude/intelligent-volta-g4bc0k-db-tracing branch from 1746f63 to 15051f2 Compare October 3, 2026 09:25
claude added 16 commits October 4, 2026 08:20
An organisation can have 50 units, 100 facilities and 500 people (those
waiting to be let in and those added by phone number count, those removed
don't), and can make 1000 bookings in 24 hours (cancelled ones count, and
each slot of a batch is one). The limits are Limits:MaxUnits, MaxFacilities,
MaxMembers and MaxBookingsPerDay. Going over is a 403 whose error code says
which: unit-limit, facility-limit, member-limit or booking-limit.

They are checked before anything is made: joining with a link is refused
before a use of the link is taken, so a full organisation doesn't use its
links up; letting somebody who was removed back in is one more person; and
a batch that would go over is refused whole. Each is looked at just before
it is made, so a number made at the same moment can go a little over, which
is enough for what they are for: stopping one organisation filling what they
all share.

Tested with an organisation of 2 units, 2 facilities, 4 people and 3
bookings a day, including that another organisation doesn't count against
it, and with each rule mutated to see it fail: the two off by one, removed
people counted, the batch counted as one, the window of 24 hours, cancelled
bookings not counting, and the check left out of letting back in and of
joining.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
Anybody can make an organisation, so whoever runs the system needs a way to
stop one being used. The migrator, which is in the image, has list-tenants
(slug, name, status, how many people, how many bookings in the last day),
suspend and unsuspend, each by slug. They exit with 1 if there is no such
organisation, and doing either twice changes nothing.

What suspending does was already there and is now tested as a whole: nobody
in it can use it, its admins included (403, unavailable), nobody can look
at or join with its links (404, as if they were never there), the others
aren't affected, people can still sign in and are still told they are in
it, and nothing is deleted, so unsuspending puts it back as it was.

The service, TenantSuspensions, says what each did apart from the exit code,
which is tested, and each rule of the list (removed people don't count, only
the last day's bookings do) and of the switch was mutated to see it fail.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
An organisation's admins can see what has been done to it: GET
/t/{slug}/Audit, the latest first, paged with limit (50, at most 200) and
before. This first part writes down the changes to settings, units,
facilities and invite links, and an organisation being suspended, or made
available again, by whoever runs the system. What was done to people follows.

An entry has no name of a person in it. Who did it and who it was done to
are kept as who they are in the organisation and are turned into names when
it is read, so that when somebody's account is erased they are a former
member here too, and what they did stays. Nothing is written for a change
that was refused, or that changed nothing (the same settings, a rename to
the same name, a link stopped twice). It is written in the transaction of
the change where there is one.

AuditEntry is a new table, with an index on the organisation and the time, so
the migrator's apply makes it.

Tested for what is written and by whom, the latest first, that nothing is
written when nothing changed or the change was refused, that only admins can
read it and only their own organisation's, paging, that a page is never more
than 200, suspending, and erasure. Each of those was mutated to see it fail.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
The rest of what is written down: an organisation being made (by its founder),
somebody being added by phone number, joining with a link, taking over their
place from before accounts, and an admin letting somebody in, turning them
away, removing them, letting them back in, making them an admin or a member,
or changing their name, phone number, unit or whose bookings they are told
about.

What was done to somebody is worked out in words with no name in them
(MemberAudit): Let a person in. Made them an admin. Changed their unit. When
more than one thing was done in one change it is one entry, whose action is
the most important of them (in or out, then role, then details), and saving
somebody as they were writes nothing. The names are worked out when it is
read, as they are then, so somebody renamed is shown by their new name.

Written in the transaction of what it is about, as in the last part.

Tested for each kind and what each says, who did it and to whom, that a
change of nothing and one that was refused write nothing, and each write was
mutated to see it fail, as was each rule of what a change is called.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
/t/:slug/admin/audit, with "History" in the sidebar for admins: what has been
done to the organization, the latest first, with an icon for what each is
about, who did it and to whom (not said twice when they did it to themselves,
and "whoever runs the system" when it was nobody in it), and when, in the
organization's time zone. A page is 50, and Show older asks for the next from
the time of the last seen. Pages are kept by where they start, so reading the
first again for what is new does not put those after the older ones.

The API client is regenerated for GET /t/{slug}/Audit, and nothing else in it
changed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
The API now limits what an organization can have and make in a day (units,
facilities, people, bookings). The pages that add those say so, in the place
each is about:

- joining with a link to an organization that has as many people as it can
  says it is full and to ask an admin, and leaves the link there to use again;
- the form for a facility shows what is not about a field, such as there being
  as many facilities as there can be, in the form, which stays for what was
  typed (it was shown by no field, so by nothing);
- the box for a unit, and the confirmation of a booking, already said it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
POST /Tenants/{slug}/Deletion, with the address of the organisation typed to
say that they mean it, makes it pending deletion: nobody in it can use it (403,
pending-deletion), its invite links look as if they were never there, and
nothing is deleted. GET /Me says, in tenantStatus and deleteAfter, that it is to
be deleted and when, which is how the people in it find out, and how its
admins find the way to take it back: DELETE /Tenants/{slug}/Deletion, by any
admin, until it has been deleted for good, and it is as it was. Both are
written in its history.

It is asked for and taken back from under /Tenants, not /t/{slug}, since it is
not available at /t/{slug} while it is pending. Whether the person is an admin
of it is decided in the same locked read as the state of it, and somebody who
isn't, or is waiting, or was removed, is told there is no such organisation.

Limits:DeletionGraceDays (30) is how long from asking to when it can be
deleted for good, which the next part does. An organisation that is
suspended, or is to be deleted already, can't be asked to be, and one to be
deleted can't be suspended.

Tenant gets DeleteAfter, and TenantStatus PendingDeletion, so the migrator's
apply adds a column and nothing else. Ten rules were mutated to see them fail:
each that says who can, in which state, the confirmation, the time, the code,
what /Me says, the guard on suspending, and the history.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
…good

An organisation that an admin asked to be deleted, and whose time to change
their mind has passed, is deleted for good by purge-tenants, from the
migrator, on a schedule (see docs/runbooks/offboarding.md): it and everything
that belongs to it, by its ID, in a transaction for each organisation.
Accounts stay, and the chats people connected, as they are theirs.

Each is locked and looked at again in the lock, so one that is restored at the
moment it is due is left alone. --dry-run says what would go, --tenant is for
one, and --early with it deletes one that an admin has asked to be deleted
before the time is up, for somebody who has to be erased sooner: it is never
for one that has not been asked for. It exits with 1 when the one named can't
be, and the address is free to be used again once it has gone.

What belongs to an organisation is listed (TenantPurges.TenantOwned), and a
test says that every entity with a TenantId is in it, so that one added later
and left out fails a test rather than being kept for good.

Tested with an organisation with one of everything, that nothing of anybody
else's, and nothing not due, or not asked for, or restored, is touched, a
dry run, --early, and the address being used again. Ten rules were mutated to
see them fail: a kind not deleted, the deletion not limited to the
organisation, not due, not asked for, early ignored, the organisation
itself kept, a dry run that deletes, a kind left off the list, the due list,
and the commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
…tion

GET /Me/Export gives anybody signed in what is kept about them, as a file:
their account, the Telegram chat they connected, every organisation they are
or were or asked to be in with the name, phone number and unit they have
there, and every booking they made, cancelled ones and the point of contact
written on each included. What they changed or cancelled of somebody else's
booking is only said to have been, and when, as what it was for is not
theirs, and nothing about anybody else is in it.

GET /t/{slug}/Export gives the admins of an organisation everything of it:
settings, units, facilities, everybody in it (removed ones and phone numbers
included), every booking, the invite links that were made (never the links: a
hash of each is all that is kept), and its history. Taking a copy of
everybody's phone numbers is written in its history.

Both are limited to 5 in 10 minutes for each person (the export policy), as
they take some reading, and neither changes anything.

Tested for what is in each and, as much, what is not (another person's
bookings and name, another organisation's, the links), authorisation, the
history, and the limit, and each rule was mutated to see it fail.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
What is left of the old version after both cutovers is code nobody uses and a
way back nobody should need. The runbook says when removing it is safe (the
gates: four weeks, everybody has claimed their place, nothing calls the old
API, a backup and an archive of the Sheets and Calendar), the order it is done
in as small changes (the web app's pages, the old endpoints and sign-in, Google
as the store, the claim flow, the schema), and what can be undone after each,
which is less each time, with the one step after which it can't be by
configuration named.

The deletion itself is left undone on purpose, and is still the unticked item
under Phase 1 of the ADR, which now points at the runbook. The ADR has a
Phase 3 section with what was done and where.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
…f data

Settings has, lower down, a copy of the organization's data to download as a
file, and deleting it: by typing its address, which is what the button waits for,
and which the API checks again. Once it is asked for, the organization is
to be deleted and can't be used, and where somebody is told that, an admin is
offered to restore it: from the list of organizations, which says it is to be
deleted and when, and from the address of it, which used to say only that it
wasn't there. Others are told that any admin can. A paused organization is said
to be paused in the list.

The account page has a download of everything that is kept about the person.
Both downloads say when they have been asked for too often.

Found and fixed on the way: the page for an organization that can't be used
asked for the organization again each time it was shown, and it is only shown
because it can't be had, so it asked for ever. It takes the address from the
route, and a check says it is not asked for over and over.

The API client is regenerated for what the last parts added, and nothing else in it
changed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
…alendar sync

Suspending an organisation stopped people using it, but what was already
queued for it still went out, and its calendar was still kept in step. Now the
outbox does not take messages of an organisation that isn't active, which is
one that is suspended or is to be deleted:

- what puts something in step, the calendar, waits, untried (so waiting
  doesn't use up its attempts), and goes when the organisation is active
  again, so it catches up;
- what is only worth doing at the time, telling people on Telegram, is
  skipped: marked Skipped (a new status, kept and deleted like done ones), and
  not sent later, as it is about what happened then;
- a handler chooses which by WhenTenantInactive, which is to wait by default.

The notifier also looks at the status when it gets a message, for one the
dispatcher took a moment before the organisation was suspended, and the calendar
planner, which the daily check and connecting a calendar go through, adds nothing
for one that can't be used.

The claim is one statement, so other organisations are not slowed by it, and it
looks at the organisation of each message, not at whether any is suspended.
Tested for both, for each of suspended and to be deleted, and each rule was
mutated to see it fail.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
Adds the meter Fbs.WebApi, exported wherever OTEL_EXPORTER_OTLP_ENDPOINT points:

- Sending: outbox messages by type and outcome (done, retry, dead, skipped), how
  long they took, what is waiting, how late the oldest is, and what was given up
  on; Telegram messages by result (sent, blocked, failed) and how long Telegram
  took; calendars Google refused.
- The database: statement duration and errors by kind, and transactions run
  again, or given up on, by why the database turned them away.
- Use: bookings made, changed, cancelled and refused for a clash; quota
  refusals by limit; organisations, accounts, invites, claims and Telegram links.
- The door: refused session tokens by reason, Clerk webhooks by type and result,
  and requests refused for being too many, by limit.
- Gauges for organisations, people and calendars by status, and messages held
  for a suspended organisation, read from the database every 30 seconds.

None is by organisation, and every tag is one of ours. The README says what each
is and which are worth alerting on.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
MySqlConnector makes a span of each statement, in the trace of the request that ran it, and
counts its connection pool, but only for those that ask for it by name. Name it, so a slow
request can be told from a slow database, and a busy pool from an idle one.

The password is not in a span (checked with one in the connection string).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
A batch of inserts has some of its values, the ids and times of what it inserts, in the
statement itself and not in parameters, and the SQL is not for wherever traces are sent. A
processor, DatabaseSpans, takes the statement out of each span of the driver before the
exporter has it, and leaves db.operation (select, insert, ...) in its place.

Tested to the collector: what an HTTP OTLP endpoint is sent has the driver's spans and
db.operation in it, and no statement, nor the ids of the bookings a batch made.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013KHuma5DMACL6bXRcEP3T8
@qin-guan
qin-guan force-pushed the claude/intelligent-volta-g4bc0k-metrics branch from dc2d0d6 to f7fcc34 Compare October 4, 2026 08:28
@qin-guan
qin-guan force-pushed the claude/intelligent-volta-g4bc0k-db-tracing branch from 15051f2 to 3a96efd Compare October 4, 2026 08:28
@qin-guan
qin-guan force-pushed the claude/intelligent-volta-g4bc0k-metrics branch from f7fcc34 to e245741 Compare October 5, 2026 08:25
Base automatically changed from claude/intelligent-volta-g4bc0k-metrics to main October 5, 2026 08:27

This branch has not been deployed

No deployments
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