Skip to content

feat(parser): count Flow and Process Builder database usage (#871) - #930

Merged
lcottercertinia merged 3 commits into
certinia:mainfrom
lukecotter:feat-flow-db-attribution
Aug 13, 2026
Merged

lcottercertinia merged 3 commits into
certinia:mainfrom
lukecotter:feat-flow-db-attribution

Conversation

@lukecotter

Copy link
Copy Markdown
Collaborator

Do not merge until we are satisfied it is fully correct. The trade-off below is real and needs a decision, not just a green CI run.

Closes #871.

The problem

Salesforce emits no SOQL_EXECUTE_BEGIN or DML_BEGIN for the database work a Flow or Process Builder element does itself. The only record is the count the element reports:

FLOW_BULK_ELEMENT_LIMIT_USAGE|1 DML statements, total 2 out of 150

Without this, that work is invisible in every table and column view.

What this does

The reported count is the rise in the transaction's governor counter across the element, so it also covers anything the element's subtree logged for itself — Apex it called, a trigger its DML fired, elements inside a subflow. Attributing the whole count would double-count that work.

So only the residual is attributed: the reported count less what the subtree already logged, floored at zero. Every line either becomes accurate or stays as it is today; none can report more than the log itself claims for the element.

applyFlowDbResiduals runs after aggregateTotals, because the residual needs the subtree totals to exist. Elements register in parser.flowDbElements at parse time, so an element the log truncated before its END line is still counted. Elements are visited innermost first, so a subflow does not re-count what its children reported.

Needs WORKFLOW at FINER or above — at FINE the log reports no flow usage at all.

The known limitation

The parser cannot tell a limit-consuming statement from a free one. Where a subtree statement consumed no limit — custom metadata SOQL, a managed-package describe — the residual subtracts it anyway and understates the element. That is the same direction as today's behaviour, never an overstatement.

Validation

Against a corpus of real logs, over the 16 flow-delta logs that carry a CUMULATIVE_LIMIT_USAGE block:

So the guarantee is per element, not per transaction: a transaction total can still pass a snapshot where free statements were logged or namespaces split. That gap is pre-existing and stays visible in the Database tab's found-vs-counted row.

Tests

apex-log-parser/__tests__/FlowDatabaseAttribution.test.ts covers bulk and non-bulk elements, delta-not-running-total, subtree residual, subtree covering the whole reported count reconciled against the snapshot, nested subflow, a truncated element, and FINE level fabricating nothing.

…#871)

A flow element's SOQL and DML never appear as statement lines, so the only
record is the count the element reports. Attribute that count to the element,
less whatever its own subtree already logged, so the totals stay at or below
what the log reports.

Runs after the tree is aggregated, because the residual needs the subtree
totals. Elements register at parse time, so an element the log truncated is
still counted.
COUNT_LABEL_RE requires a leading digit, so a head such as
"SOQL queries, total 1 out of 100" lost its limitUsage entirely. The
previous code stripped the count with a no-op replace and kept the
observation. A missing count now costs only the delta.
applyFlowDbResiduals summed every child delta. A repeated or cumulative
line over-attributed the element, and the zero floor does not catch that
direction. The reported rise in `used` is now the ceiling.
@lcottercertinia
lcottercertinia merged commit fb35a53 into certinia:main Aug 13, 2026
7 checks passed
lcottercertinia pushed a commit to apex-dev-tools/apex-log-parser that referenced this pull request Aug 14, 2026
Ports certinia/debug-log-analyzer#930 (merged upstream as `fb35a536`),
plus a cleanup pass.

## Why

Salesforce emits no `SOQL_EXECUTE_BEGIN` or `DML_BEGIN` for the database
work a Flow or
Process Builder element does itself. The only record is the leading
count on
`FLOW_BULK_ELEMENT_LIMIT_USAGE` / `FLOW_ELEMENT_LIMIT_USAGE`. Those
elements therefore
showed zero SOQL and DML.

## How

That count is the rise in the transaction's governor counter across the
element, so it also
covers any statement the element's subtree logged for itself. Only the
residual is
attributed - the reported rise minus the subtree total, floored at zero
and capped at the
rise in the running total. This can only make a line more accurate. It
can never report
more than the log claims.

The pass runs after `aggregateTotals`, over the elements in reverse
registration order, so
descendants settle before their ancestors and a nested subflow is not
counted twice. CPU
time and heap size are measures, not counters, so they are not
attributed.

`parseTotalLimit` also keeps a head with no leading count, at `delta:
0`. Before, such a
line lost its `limitUsage`.

Needs `WORKFLOW` at `FINER` or above. At `FINE` the parser fabricates
nothing.

## Notes

- Two commits: the port, then a refactor that maps metrics to counter
fields instead of a
  switch and walks each ancestor chain once.
- No changeset. `0.1.0` is not published yet, so `initial-release.md`
covers this.

## Test

`pnpm run ci` - 138 tests pass. 10 new cases in
`FlowDatabaseAttribution.test.ts` cover
bulk and non-bulk elements, delta versus running total, the subtree
residual, a full-subtree
cover, a repeated line, a nested subflow, truncation, and `FINE` level.
@lukecotter
lukecotter deleted the feat-flow-db-attribution branch August 28, 2026 09:39
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.

✨ feat: flow SOQL/SOSL/DML usage undercounted in Database tab / governor totals

2 participants