Skip to content

feat(observability): record when the AI Gateway request log reached ADX - #689

Open
Zhupku wants to merge 5 commits into
mainfrom
feat/gateway-coverage-heartbeat
Open

Zhupku wants to merge 5 commits into
mainfrom
feat/gateway-coverage-heartbeat

Conversation

@Zhupku

@Zhupku Zhupku commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The usage report reads the AI Gateway's usage from Azure Data Explorer. A time without request lines can be a time without calls, or a time whose lines did not reach ADX, for example while the collection stopped. If the report cannot tell them apart, it shows zero usage for an outage. This pull request records which times the request log covered, apart from the collection health checks:

  • AI Gateway. With the request log, each Pod writes a heartbeat line every 5 minutes, on a fixed schedule, from a daemon thread. It writes also while no call comes:

    {"log_type":"cetirun.ai_gateway.heartbeat","schema_version":1,"time":"2026-11-26T00:05:00.000Z","interval_seconds":300}

    The request lines and the heartbeats share a lock, and each line goes out in one write. The entrypoint starts the heartbeat last, right before the proxy, so that its first line does not fall among the lines that the entrypoint prints.

  • ADX. The module runs the new infra/cluster/modules/adx/cetirun-gateway-coverage.kql after the usage script. The materialized view GatewayCoverage counts the heartbeats per 5 minutes, cluster and Pod, so a Pod has one in each interval. It also counts all lines of the gateway container as they arrive. It has no backfill (for the reason of Count Daily AI Gateway Usage by Agent UID, User, and Copilot Route #668), the retention of GatewayDailyUsageV2 and a hot cache of 31 days. The function GatewayCoverageGaps(cluster, fromTime, toTime) lists the 5-minute intervals of a period whose request lines can be missing, with the reason:

    • collection not observed: no Pod has heartbeats in the interval and in the intervals before and after it. The first interval after a start or an outage, the last before one, and the current interval are such gaps.
    • missed heartbeat: a Pod that ran in the interval lacks its heartbeat in it or next to it, also while another Pod's heartbeats arrive. A Pod runs from its first heartbeat to its last in the whole view, so an outage of days shows in a short query too. A Pod needs none before its first interval or after its last.
    • request lines removed: the Gateway table holds fewer lines of the interval than the view counted, because the retention of the logs database or a deletion removed some. The function compares the counts over whole intervals and does not read the retention setting, so a retention that was raised later does not take the reason away. GatewayRequests() can lack calls of the interval. The daily totals of GatewayDailyUsageV2 stay, and for them only the other two reasons count.

    In an interval without a reason, a usage of zero means that no calls were recorded and no heartbeat gap was detected.

  • Docs. Observability (signals, query functions, an example, and the scripts for databases prepared by hand), infrastructure, PRIVACY.md, and the AI Gateway chart README.

The docs name the limits: the heartbeats are samples, so lines lost between two heartbeats that arrived, or written by a Pod after its last heartbeat, leave no gap (CollectionHealth() reports the lines that the collector dropped); and times before the image that writes heartbeats, and before the view, have no coverage.

Related issue

Part of the usage report proposal (phase 2), whose review asked to tell an idle Agent from a collection outage and to show zero only for covered periods. There is no issue.

Validation

Offline:

  • scripts/tests/test_ai_gateway_request_log.py (32 tests, 6 new), test_ai_gateway_entrypoint.py (17), and test_cluster_deploy.py (40, with Bicep 0.47.16) in a Linux container: OK.
  • Mutations: 22 of 22 fail a test. They cover one write per line, the first beat at start, a failed write, the daemon thread, the interval field, the heartbeat period, the start before the proxy, the interval size of the view and of the function, the log type, the rules of the two collection reasons, the Pod spans from the whole view, the Pods without heartbeats, the line counts and their comparison over whole intervals, the wording of a zero, and the module wiring.
  • The repository tests check the KQL as text. The body of GatewayCoverageGaps() also ran on a Kusto cluster, read only, with synthetic heartbeats in place of the view, with synthetic rows in place of the table too, in seventeen cases: steady Pods, one lost heartbeat of one Pod with one and with two Pods, the first heartbeats of a new image, an outage of both Pods, a rollout, the end of the heartbeats, no heartbeats, a Pod without heartbeats for two days, queried for one hour in the gap, after it, and before it, and a Pod of an older image that writes lines without heartbeats. Five cases remove lines from the table: two of three request lines of an interval, every line of the hour (a retention that was shorter and then raised), a heartbeat line, a line that arrived before the view, and a period that ends inside an interval. Each gave the expected intervals and reasons. Four mutations of the function body each fail one of these cases.
  • scripts/ci/validate.py python-check, license headers, site/tests (157 tests), source-level site checks (0 content errors, no new findings in the changed lines), and bicep build of infra/cluster/main.bicep: OK.

Live, on a test cluster with CetiRun v0.2.2, its ADX database, and an AI Gateway image of this branch (v0.2.2 has the same AI Gateway sources as the base of this branch), set with kubectl set image:

  • The image build ran the build checks of the Dockerfile. The rollout replaced both AI Gateway Pods in about 2.5 minutes, and no Agent Pod restarted.
  • Each Pod wrote its first heartbeat at start, after the lines of the entrypoint and before LiteLLM's startup lines, then one every 300 seconds exactly (01:45:43.374Z, then 01:50:43.374Z).
  • In ADX, the heartbeat lines had the cluster and the Pod. GatewayCoverage counted one heartbeat of each Pod in each 10-minute interval. GatewayCoverageGaps("<cluster>", ago(1h), now()) listed the intervals before the new image, and the current interval until its heartbeats arrived.
  • A call through the AI Gateway wrote its request line next to the heartbeats, and GatewayRequests() returned it.
  • The first deployment of the ADX module refused the function, because first is a KQL keyword: e59edf1 renames it. The second deployment succeeded in 36 seconds and created the view, its policies (400 days, hot cache 31 days) and the function.
  • After the review changes, the 10-minute view was dropped, and the module created the 5-minute view and the new function. The view counted one heartbeat of each Pod in each interval. For the last hour, the function returned collection not observed for the intervals before the view, for its first interval, and for the last two intervals, whose next heartbeats had not arrived; it returned no gap for the interval between them. For an interval 100 days ago, that version returned request lines past retention and collection not observed.
  • After the third review change, the view was dropped again, and the module created it with the line counts. For the interval in which the view started, the table held more lines than the view had counted, the lines from before the view; for the next interval, both had 22 lines. The function returned no request lines removed, and collection not observed for the intervals before the view.

Not run: make docs-check (Hugo) and the image scans of CI.

Compatibility and operational impact

  • The AI Gateway writes one more kind of line to stdout: one line of about 120 bytes every 5 minutes per Pod. GatewayRequests() and the usage views read only request lines.
  • Existing databases get the view and the function at the next deployment of the cluster template, or when an administrator runs the script. make deploy does not run the ADX scripts.
  • No chart, operator or Portal change.

Checklist

  • The change is focused and preserves unrelated work.
  • Tests cover the changed behavior, or I explained why no new test is necessary.
  • Documentation and examples match any public behavior changes.
  • Generated artifacts were updated from source, or none are affected.
  • Dependency changes include lockfiles, source attribution, and license information, or none are affected.
  • I examined the diff and attachments for secrets, private endpoints or identifiers, customer data, agent conversations, and internal information.
  • I followed the applicable CLA instructions, or asked a maintainer about an unclear check.
  • I identified checks that must have credentials or a live environment and did not run them without authorization.

A time without request lines can be a time without calls, or a time whose
lines did not reach the logs database. The usage report must show zero only
for the first.

- With the request log, each AI Gateway Pod writes a heartbeat line every
  five minutes on a fixed schedule, from a daemon thread, also while no call
  comes: {"log_type": "cetirun.ai_gateway.heartbeat", "schema_version": 1,
  "time": ..., "interval_seconds": 300}. The request lines and the
  heartbeats share a lock, and each line goes out in one write. The
  entrypoint starts the heartbeat last, right before the proxy.
- The ADX module runs the new cetirun-gateway-coverage.kql after the usage
  script: the materialized view GatewayCoverage counts the heartbeats per
  10 minutes, cluster and Pod, without backfill and with the retention of
  GatewayDailyUsageV2; GatewayCoverageGaps(cluster, fromTime, toTime) lists
  the 10-minute intervals without a heartbeat.
- Docs: observability (signals, query functions, the scripts for databases
  prepared by hand), infrastructure, PRIVACY.md, and the AI Gateway chart
  README.
…thout a reserved word

The module deployment on a test cluster refused the function: first is a KQL keyword (SYN0002). The function also orders its intervals by time.
@Zhupku Zhupku self-assigned this Nov 27, 2026
@Zhupku
Zhupku marked this pull request as ready for review November 27, 2026 01:54
… lines past retention

Review of #689: the heartbeats are samples, and the request lines expire
before the coverage view.

- GatewayCoverage counts the heartbeats per 5 minutes, the period of a Pod's
  heartbeat, so a Pod has one in each interval.
- GatewayCoverageGaps() returns the reason of each gap. An interval is
  observed only when a Pod's heartbeats surround it; a Pod that ran in an
  interval and lacks its heartbeat in it or next to it marks it too, also
  while another Pod's heartbeats arrive. The first interval after a start,
  the last before a stop, and the current one are therefore gaps.
- Intervals older than the retention of the logs database are gaps of
  GatewayRequests(), whose lines can be gone: the module passes that
  retention to the script. The daily views keep their totals; for them only
  the other two reasons count.
- Docs: the reasons, the limits of the sampling, and the placeholders for
  databases prepared by hand.
Review of #689: with the heartbeats of a day around the period, a Pod without heartbeats for longer left no gap in a short period in between, and a wider query gave another answer. Each Pod now runs from its first heartbeat to its last in the whole view; only the heartbeats in and next to the period are joined.
(missed | extend Reason = "missed heartbeat"),
// GatewayRequests() reads the Gateway table, which keeps a line for the
// retention of the logs database from its ingestion.
(intervals | where Interval < now() - __LOGS_RETENTION_DAYS__d | extend Reason = "request lines past retention")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Increasing raw retention moves this cutoff backwards, but it does not restore deleted request rows. For example, after changing from 90 to 180 days, a 120-day-old interval with retained heartbeats can lose its warning and look like zero usage. Keep already-expired intervals marked unavailable when retention increases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed in 39c24bc, without the retention setting. GatewayCoverage now also counts all lines of the gateway container as they arrive (Lines), and GatewayCoverageGaps() compares that count with the lines that the Gateway table still holds in each whole interval of the period. An interval with fewer lines left gets the reason request lines removed, whatever the retention was or is now, so a raised retention does not take it away. A deletion also shows. The module no longer passes __LOGS_RETENTION_DAYS__, and the comparison reads typed columns only, without the request bodies.

Run like the other cases, with synthetic rows in place of the view and of the table:

  • Two of three request lines of an interval removed: that interval.
  • Every line of the hour removed while the view keeps their counts, as after a shorter retention that was raised later: all 12 intervals.
  • A removed heartbeat line: that interval.
  • A line that arrived before the view, and a period that ends inside an interval (the table is counted over whole intervals): no gap.

The earlier 12 cases give the same results, and a Pod of an older image that writes lines without heartbeats does not count as a Pod that ran. Four mutations of the function body each fail one of these cases. On the test cluster, the view was created again with the line counts: for an interval after its creation, the view and the table both had 22 lines, and the function returned no request lines removed.

| `missed heartbeat` | A Pod that ran in the interval lacks its heartbeat in it, in the interval before it, or in the interval after it. A Pod runs from its first heartbeat to its last in the whole view, also through days without its heartbeats. The collection can have lost lines of that Pod. A Pod needs no heartbeat before its first interval or after its last. |
| `request lines past retention` | The interval is older than the retention of the logs database, which counts from the ingestion of a line. `GatewayRequests()` can lack its lines. |

In an interval without a reason, a usage of zero means that no call came. For

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The sampling caveat below says a request line can be lost between two delivered heartbeats. In that case the query returns no gap even though a call occurred. Please say “no calls were recorded and no heartbeat gap was detected” here and in the function description.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done in 39c24bc. The docs now say "In an interval without a reason, a usage of zero means that no calls were recorded and no heartbeat gap was detected", and the description of GatewayCoverageGaps() uses the same words.

…onger holds, from counts

Review of #689: the retention reason compared the interval with the current retention, so a higher retention took the warning off intervals whose request lines were already deleted. GatewayCoverage now also counts all lines of the gateway container as they arrive, and GatewayCoverageGaps() compares that count with the lines that the Gateway table still holds over whole intervals: an interval with fewer lines left is 'request lines removed', whatever the retention was or is, and after a deletion too. The function no longer depends on the retention setting, so the module passes none. The docs and the function description say that a zero means that no calls were recorded and no heartbeat gap was detected.

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.

4 participants