Skip to content

fix(telemetry): binary search truncation drops - #995

Merged
ParidelPooya merged 6 commits into
mainfrom
fix/truncation-bounded-meter
Nov 26, 2026
Merged

ParidelPooya merged 6 commits into
mainfrom
fix/truncation-bounded-meter

Conversation

@ParidelPooya

@ParidelPooya ParidelPooya commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Issue #, if available

Fixes #908

This fix grew out of aws/aws-durable-execution-sdk-java#881 by @pcbeingused333 and aws/aws-durable-execution-sdk-js#1175 by @jay23606. Their analysis and tests showed where the cost comes from and what a correct result looks like. Thank you both.

Guarantee

The output never goes over the limit, as long as dropping everything droppable makes it fit. If the record is still over the limit after every droppable part is dropped, the output is over the limit, the same as main.

Truncation can drop more results and operations than main. That is accepted for simpler and faster code. Example: one old result of 2,000 bytes, then 1,999 results of true, array format, limit = full size - 1 byte. Main clears the old result only. This branch clears every result and drops 41 operations.

Description of changes

truncate_record dropped one result or one operation at a time. After each drop it rendered and measured the whole record. A record with n operations needed up to 2n renders, and each render costs time proportional to n. So truncation time grew with the square of the operation count.

This change replaces the one-at-a-time check with a plain binary search over the drop count. It changes only truncation.py. The export path in _export_scheduler.py calls the public truncate_record. So every exporter gets the change, built-in or custom.

  1. If the record fits, it is returned unchanged, as before.
  2. Results step: a binary search finds the smallest j in 0..n such that the record fits when the results of the j oldest operations are cleared. If no j fits, every result is cleared.
  3. Operations step: if no j fit, a binary search finds the smallest k in 1..n such that the record fits when the k oldest operations are removed. If no k fits, every operation is removed.
  4. Input, then output, are dropped as before.

The phase order, the oldest-first order (operations without a startTime last) and the truncation markers do not change. The chosen count always comes from a real measurement. So a record takes about 2 * log2(n) renders.

Why it can drop more

The binary search assumes that each drop makes the rendered record smaller. Three cases break this assumption:

  1. Clearing a result replaces it with "truncated":true. In the array format this marker is longer than a short result such as 1, true or "ok". So clearing a short result makes the record bigger. The example above is this case.
  2. In the operationsByName map, the result and error of a repeated name appear when the other operation with that name is dropped. Example (test_by_name_truncation_can_drop_more_than_the_fewest_but_fits): main drops 1 operation, this branch drops 6.
  3. A render that escapes the record, such as the string body of OTelExporter, changes size by a different amount for each drop.

In these cases the search can return a larger count than the smallest one that fits. The returned count was measured. So the output still never goes over the limit.

Timing

Measured with Python 3.12.16 on a Linux dev host. Each row truncates one record of simple successful named steps through truncate_record. "Renders" counts the calls of render. In every row, main and this branch drop the same number of results and operations and produce a record of the same size.

Shape Limit Ops main renders main time branch renders branch time
LambdaLogExporter (by-name) 256,000 2,000 2,680 19.7 s 25 0.16 s
LambdaLogExporter (by-name) 256,000 4,000 6,680 94.1 s 27 0.28 s
LambdaLogExporter (by-name) 256,000 8,000 14,680 391.4 s 29 0.65 s
repeated name, near limit full size - 100 2,000 3,004 14.1 s 25 0.11 s
repeated name, near limit full size - 100 4,000 6,004 56.6 s 27 0.25 s
repeated name, near limit full size - 100 8,000 12,004 227.8 s 29 0.53 s
custom render 256,000 2,000 2,558 6.7 s 25 0.07 s
custom render 256,000 4,000 6,558 33.1 s 27 0.12 s
custom render 256,000 8,000 14,558 137.4 s 29 0.23 s
array, short results 256,000 2,000 2,594 6.6 s 25 0.06 s
array, short results 256,000 4,000 6,594 29.3 s 27 0.10 s
array, short results 256,000 8,000 14,594 128.2 s 29 0.20 s

"Repeated name, near limit" uses LambdaLogExporter. The older half of the operations share one name. "Custom render" wraps each operation with an extra field and uses the default measure. "Array, short results" uses the identity render. Every tenth operation has a 200-character result, and the others have true, 1, "ok" or null. Both main and this branch clear 1,057 results there and drop 590, 2,590 and 6,590 operations.

Tests

  1. Commit 1 adds test_truncation_renders_a_logarithmic_number_of_times: 2,000 named operations, LambdaLogExporter, limit 256,000. It asserts fewer than 40 renders. It fails on main with 2,680 renders.
  2. The later commits add:
    1. test_truncation_renders_a_logarithmic_number_of_times_for_a_custom_render: the same bound for a custom render.
    2. test_truncation_never_exceeds_the_limit_and_drops_oldest_first: 300 random records in the array, by-name and both formats. The records hold quotes, backslashes, non-ASCII text, emoji, U+2028, repeated, empty and missing names, results, errors, input and output. Start times are random, so the operations are out of order. The output is over the limit only when nothing is left to drop. The kept operations are the newest ones by start time, and results are cleared oldest first.
    3. test_truncation_matches_one_at_a_time_drops_for_the_array_format: every result in these 300 random records is longer than 20 bytes. So each drop makes the record smaller, and the output equals the one-at-a-time result.
    4. test_by_name_truncation_can_drop_more_than_the_fewest_but_fits: case 2 above.
    5. test_truncation_clears_every_result_before_it_drops_an_operation: only clearing every result fits. No operation is dropped.
    6. test_truncation_drops_every_operation_before_it_drops_the_input: only dropping every operation fits. The input is kept.

Design

See Rules for every change.

  • Design note (rule 3): not needed. The public API does not change.
  • Simpler options considered (rule 2):
    1. One-at-a-time exact check (main). It finds the fewest drops, but it is O(n^2).
    2. Bounded size check, as in fix: bound telemetry truncation for built-in exporters aws-durable-execution-sdk-java#881. It finds the fewest drops. It still measures after almost every drop for repeated names near the limit and for a custom exporter. So those cases stay O(n^2). It also needs a list of built-in exporters and a shrink bound per drop.
    3. Incremental size tracker, as in fix(telemetry): bound truncateRecord renders to log(n) per drop phase aws-durable-execution-sdk-js#1175. It finds the fewest drops and is fast. It adds about 580 lines.
    4. Plain binary search, chosen. It takes about 2 * log2(n) renders with any exporter. In some records it drops more results or operations than the fewest. See "Why it can drop more".
  • Root cause (rule 5): the size check rendered the whole candidate record after every single drop. The fix finds the drop count of each step with a binary search in truncate_record.
  • Net change in source lines and in public API members: production code truncation.py +53 / -25 (14 of the added lines are comments or blank lines), README +21. Public API members: no change.

Assumptions

  • Each exporter's render and measure are deterministic and have no side effects. Evidence: the code of each built-in exporter. The LambdaLogExporter envelope timestamp changes between renders, but it has a fixed width.
  • The timing on Lambda is unverified. The table above was measured on a dev host.

Decisions not covered by the issue or the design note

  • The operations step runs only when no results count fits, as before.
  • The README states the guarantee and the extra drops next to its existing truncation paragraph, in "Error names and causes".

Checklist

  • Every new branch has a test that fails without it (rule 4)
  • For a bug fix: the first commit adds a test that fails on the current code (rule 5)
  • Every edited test expectation is listed in the description, with the reason (rule 5). No existing test expectation changed.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ParidelPooya
ParidelPooya force-pushed the fix/truncation-bounded-meter branch from 5d3c940 to 386b860 Compare November 25, 2026 20:49
ParidelPooya and others added 2 commits November 25, 2026 21:03
Truncation renders and measures the whole record before every drop.
A by-name record with 2,000 operations over a 256,000-byte limit
takes 2,680 renders on main. This test asserts fewer than 40 renders.
So it fails until the drop count is found by a binary search.
Truncation dropped one result or operation at a time. After each drop
it rendered and measured the whole record. So a record with n
operations took up to about 2n renders of size n, which is O(n^2).

Each phase now finds its drop count with a binary search. So a record
takes about 2 * log2(n) renders, with any render function. The search
assumes that each drop makes the record smaller. The by-name map can
break this. Then truncation can drop more old operations than the
fewest that fit. The chosen count is always measured, so the output
never goes over the limit.

Fixes #908

Co-authored-by: pcbeingused333 <[email protected]>
Co-authored-by: jay23606 <[email protected]>
@ParidelPooya
ParidelPooya force-pushed the fix/truncation-bounded-meter branch from 386b860 to 413ad01 Compare November 25, 2026 21:35
@ParidelPooya ParidelPooya changed the title fix(telemetry): skip truncation size checks that cannot succeed fix(telemetry): binary search truncation drops Nov 25, 2026
Clearing a result replaces "result":<json> with "truncated":true. When
the result JSON is 7 bytes or shorter, the record grows. So the size
after clearing j results was not monotonic, and the binary search could
clear every result and drop operations that the one-at-a-time check
kept.

Only results longer than 7 bytes are now cleared. Shorter results stay
in the record, unmarked. The record after dropping causes is measured
before the results search, so a record that fits then keeps every
result. The array format now drops the fewest. Extra drops can happen
only in the by-name format.
The by-name map writes no truncated marker. So in by-name, clearing a
result of 7 bytes or less still makes the record smaller. Keeping such
results made by-name truncation drop operations that the base kept.

Before the results search, one render clears every small result. If it
is smaller than the record after dropping causes, small results stay
candidates, as in the base. Otherwise only longer results are cleared.
The extra render runs only when small results exist.
The binary search can drop more results or operations than the
one-at-a-time check. This happens with escaped string bodies (OTel)
and with repeated names in the by-name map. The output never goes over
the limit while dropping everything droppable makes it fit.
Remove the small-result probe and the separate check before the
results search. Every operation with a result is a candidate again,
oldest first, as in the first binary-search commit.

The output never goes over the limit, as long as dropping everything
droppable makes it fit. Truncation can drop more results and
operations than the one-at-a-time code. That trade-off is accepted
for simpler code.

Tests: remove the small-result and cause-only tests. Keep the
one-at-a-time comparison only for results longer than 20 bytes, where
each drop makes the record smaller. Keep the upper-bound tests.
@ParidelPooya
ParidelPooya marked this pull request as ready for review November 26, 2026 03:36
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 26, 2026 03:36 — with GitHub Actions Active
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 26, 2026 03:37 — with GitHub Actions Active
@github-actions

Copy link
Copy Markdown
Contributor

Codex AI review

One approach finding at packages/aws-durable-execution-sdk-python-telemetry-exporter/src/aws_durable_execution_sdk_python_telemetry_exporter/truncation.py:131: the reduced telemetry retention requires design and compatibility review. Tests were not executed under the read-only review constraints.

Reviewed commit 202615cd14c4a1115c2e2b50ff1e6a4b2810a3a9. Workflow run

@ParidelPooya
ParidelPooya merged commit 54806d0 into main Nov 26, 2026
44 checks passed
@ParidelPooya
ParidelPooya deleted the fix/truncation-bounded-meter branch November 26, 2026 19:01
ops[idx] = trimmed
any_result = True
count = len(ops)
found = first_fit(0, count, lambda j: fits(j, 0))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should-fix: Python searches over every operation, but JS #1293 searches only over the operations that have a result. So the two can drop different things.

Example: 5 operations, oldest first. Only the first 2 have a result: B (a big one) and C (true).

Python tries Results cleared Fits?
1 B yes
2 B and C no. Clearing C makes the record 3 bytes bigger.
3 to 5 B and C no. There is nothing left to clear.
  • JS only tries 0 to 2. It tries 1 first, so it clears B and stops.
  • Python tries 0 to 5. It tries 2, then 4, then 5. None fit, so it drops whole operations.

With no results at all, Python also renders about 11 times where JS renders once.

Could Python search only over the operations that have a result?

🤖 AI-assisted comment

Comment on lines +131 to +148
def first_fit(low: int, high: int, fits_at: Callable[[int], bool]) -> int | None:
# Binary search for the smallest count in ``low..high`` that fits. The
# search assumes that a larger count never makes the record larger.
# Some records break this assumption, for example a by-name map with
# repeated names, a short result replaced by the truncated marker, or
# an escaped string body. Then the search can return a larger count
# than the smallest one that fits. So truncation can drop more results
# or operations than the one-at-a-time check. The returned count was
# always measured. So the output never goes over the limit, as long as
# dropping everything droppable makes it fit.
found = None
while low <= high:
middle = (low + high) // 2
if fits_at(middle):
found, high = middle, middle - 1
else:
low = middle + 1
return found

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: this loop could use the standard library. bisect.bisect_left takes a key= function since Python 3.10, and this package needs 3.11 or newer:

import bisect

def first_fit(low: int, high: int, fits_at: Callable[[int], bool]) -> int | None:
    counts = range(low, high + 1)
    i = bisect.bisect_left(counts, True, key=fits_at)
    return counts[i] if i < len(counts) else None

range is lazy, so this probes the same number of counts as the loop, and the count it returns was always measured to fit. One thing to know: bisect_left uses the upper midpoint, like Go's sort.Search, Java and Rust, while this loop and JS use the lower one. For low..high = 0..3 it probes 2 first, not 1. So this fits if the SDKs settle on the upper midpoint as the shared rule.

🤖 AI-assisted comment

This branch was successfully deployed

1 active deployment
ai-pr-review-runtime — 202615cd Deployed Nov 26, 2026 by ParidelPooya via ai-pr-review / Codex review / Generate Codex review #3465
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.

[Bug]: telemetry truncation time grows with the square of the operation count

3 participants