Repository navigation
Conversation
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.
… 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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.kqlafter the usage script. The materialized viewGatewayCoveragecounts the heartbeats per 5 minutes, cluster and Pod, so a Pod has one in each interval. It also counts all lines of thegatewaycontainer 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 ofGatewayDailyUsageV2and a hot cache of 31 days. The functionGatewayCoverageGaps(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: theGatewaytable 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 ofGatewayDailyUsageV2stay, 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), andtest_cluster_deploy.py(40, with Bicep 0.47.16) in a Linux container: OK.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), andbicep buildofinfra/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:GatewayCoveragecounted 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.GatewayRequests()returned it.firstis 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.collection not observedfor 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 returnedrequest lines past retentionandcollection not observed.request lines removed, andcollection not observedfor the intervals before the view.Not run:
make docs-check(Hugo) and the image scans of CI.Compatibility and operational impact
GatewayRequests()and the usage views read only request lines.make deploydoes not run the ADX scripts.Checklist