Skip to content

feat(queue): record consumed message size and log large messages - #14480

Merged
ChiragAgg5k merged 2 commits into
mainfrom
feat/queue-job-size-telemetry
Oct 14, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
feat/queue-job-size-telemetry

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

On 2026-10-13 a single ~138MB v1-audits job OOMKilled every worker that reserved it for 14h, and nothing pointed at it: worker memory looked flat between scrapes and no line was logged. This adds the missing signal at the consumer, where the raw bytes are already in hand.

Change Where
Message::getSize() / setSize(); brokers stamp strlen($raw) when they decode Message.php, Broker/Redis.php, Broker/Nats.php
messaging.process.message.size histogram (bytes, 1KB–256MB buckets), next to the existing wait and process histograms Server.php
error_log for any message over 1MB, written before decode so it survives an OOM during decode brokers via Message::warnIfLarge()

No re-encode on publish or consume, no limit, nothing rejected. Every consumer built on this Server gets it, cloud included.

Lean payloads themselves: #14477.

Test Plan

  • composer lint, phpstan pass.
  • Queue package tests pass: ServerTelemetry, ServerJobs, ProcessPhases, Prefetch, RedisQueueSize, RedisReconnectCallback, Codec, Maintenance, NatsBrokerConfig, Locking. ConsumerResilienceTest segfaults on the host Swoole build on base too; CI runs it in docker.
  • After deploy: histogram_quantile(0.99, sum by (le, service_name) (rate(messaging_process_message_size_bytes_bucket[5m]))) per worker, and Loki |= "Large queue message".

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

Related: #14478

A 138MB audits job OOMKilled every worker that reserved it for 14h with nothing pointing at it. Brokers now stamp the encoded size on each Message (no re-encode), the Server records it as messaging.process.message.size, and a message over 1MB is logged before decode so the line survives an OOM during decode.
@luxor-code-review

luxor-code-review Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Size histogram recorded without queue attribution — Server.php:368
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 warnIfLarge logs every large delivery, redeliveries too (Message.php:145) — Message::warnIfLarge calls error_log for every message larger than LARGE_SIZE (packages/queue/src/Message.php:145), and both brokers invoke it per message inside their receive loops (packages/queue/src/Broker/Redis.php:155, packages/queue/src/Broker/Nats.php:817).

This PR instruments queue message sizes: brokers now measure each incoming payload and stamp it on the message, the server records the sizes into an OpenTelemetry histogram, and oversized messages trigger a log warning.

Files Change
packages/queue/src/Message.php Adds a LARGE_SIZE constant, a nullable size field with getter/setter, and a static warnIfLarge() helper.
packages/queue/src/Broker/Nats.php Measures payload size on the JetStream pull path and sets it on each message.
packages/queue/src/Broker/Redis.php Measures payload size on the claim path and sets it on each message.
packages/queue/src/Server.php Wires a messageSize histogram instrument in setTelemetry() and records sizes for messages that report one.

Reviewed commit: d03df43

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@luxor-code-review luxor-code-review 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.

Adds message-size telemetry (a nullable size on the queue Message, a warnIfLarge helper, and an OTel size histogram in the queue server) across the NATS and Redis brokers.

Key findings

  • 🟡 Size histogram recorded without queue attribution — Server.php:368

Comment on lines +368 to +371
$size = $message->getSize();
if ($size !== null) {
$this->messageSize->record($size);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 bug · medium

Size histogram recorded without queue attribution

messageSize is recorded with no attributes at packages/queue/src/Server.php:370, so all queues on a process collapse into one aggregate size series. The sibling depth gauges in the same setTelemetry() path attach messaging.destination.name and messaging.destination.namespace (packages/queue/src/Server.php:290-293), making the histogram inconsistent with them and unusable for the per-queue capacity analysis its name implies. Impact: operators cannot tell which queue produces large messages.

📋 Prompt for AI Agents

In packages/queue/src/Server.php around lines 368-371, change $this->messageSize->record($size) to pass the attributes ['messaging.destination.name' => $message->getQueue(), 'messaging.destination.namespace' => $this->adapter->namespace] — the same attribute set the depth gauges use in createDepthGauge (~line 290) — so the size histogram can be sliced per queue like the other messaging instruments.

@ghost

ghost commented Oct 14, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The Redis and NATS consumers measure encoded message bytes, attach the size to each decoded message, and log messages larger than 1 MiB before decoding. The queue server records message sizes in a histogram alongside its existing processing telemetry.

Latest changes: The Redis claim path now measures only string reservations and reuses that size for logging and message metadata.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
packages/queue/src/Broker/Nats.php Logs oversized raw deliveries before decoding and attaches their byte size to decoded messages.
packages/queue/src/Broker/Redis.php Measures string reservations, logs oversized messages, and attaches their byte size to decoded messages.
packages/queue/src/Message.php Adds optional message-size accessors and a shared large-message warning helper.
packages/queue/src/Server.php Records consumed message sizes in a histogram alongside existing processing telemetry.

Reviewed the commits since d03df43 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost 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.

🔵 Tier A · Looks good to merge. Summary

Comment thread packages/queue/src/Server.php Resolved
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → feat/queue-job-size-telemetry (after).

Metric Before After Change
🚀 Requests/sec 200.69 178.74 🔴 -10.9%
⏱️ Latency P50 86.35 ms 97.67 ms 🔴 +13.1%
⏱️ Latency P95 200.05 ms 224.83 ms 🔴 +12.4%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 97.67 224.83 11,343 178.74 +24.77
Account 193.32 347.66 597 9.97 +41.03
TablesDB 93.85 169.99 6,169 99.6 +19.15
Storage 91.87 185.14 2,985 50.01 +11.75
Functions 140.76 266.54 1,592 27.37 +20.78

Top API waits (after)

API request Max wait (ms)
account.name.update 589.96
functions.variables.create 483.26
tablesdb.rows.decrement 467.11
account.prefs.update 424.67
tablesdb.rows.create 395.58

Script replies are mixed; the package's max-level phpstan rejects strlen on them.

@ghost ghost 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.

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k
ChiragAgg5k merged commit abbb102 into main Oct 14, 2026
65 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the feat/queue-job-size-telemetry branch October 14, 2026 13:03
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