Skip to content

feat(mqtt): broker telemetry and per-project usage - #14043

Merged
ArnabChatterjee20k merged 10 commits into
mainfrom
telemetry-mqtt
Oct 1, 2026
Merged

ArnabChatterjee20k merged 10 commits into
mainfrom
telemetry-mqtt

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

What

Adds observability to the MQTT push broker in two layers.

Telemetry (OpenTelemetry, in the broker)

New instruments on the Mqtt adapter, hooked in the Handler/adapter:

  • mqtt.connections.active (UpDownCounter) — +1 on an accepted CONNECT, -1 on disconnect
  • mqtt.connect.result — accepted / not_authorized / rate_limited
  • mqtt.subscribe.outcome — granted / denied
  • mqtt.delivery.latency — publish→deliver seconds, carried as publishedAt through the pub/sub envelope (clamped for clock skew)
  • mqtt.replay.backlog — pending messages replayed on reconnect
  • mqtt.ledger.duration — the push-ledger write in the adapter

Low-cardinality attributes only (qos, result) — no projectId label (cardinality).

Per-project usage (billing/dashboards, via queueForUsage)

Per-project projectId belongs in the usage pipeline, not metric labels. The broker (single worker) accumulates per-project connections and deliveries in memory and flushes them to the stats-usage queue every 10s:

  • METRIC_MQTT_CONNECTIONS, METRIC_MQTT_MESSAGES_DELIVERED (cumulative, summed by StatsUsage)
  • a publisherForUsage resource in the broker (mirrors realtime)

Notes

  • Mirrors the realtime server's usage-flush pattern (publisherForUsage from _APP_CONNECTIONS_QUEUE_STATS_USAGE, periodic flush).
  • Lint (Pint), PHPStan level 4, and syntax all clean locally; broker e2e runs in CI.

… outcomes

New instruments on the Mqtt adapter and their hooks in the Handler/adapter:
- mqtt.connections.active (UpDownCounter): +1 on an accepted CONNECT, -1 on disconnect
- mqtt.connect.result (accepted/not_authorized/rate_limited) and mqtt.subscribe.outcome (granted/denied)
- mqtt.delivery.latency: publish-to-deliver seconds, carried as publishedAt through the pub/sub envelope
- mqtt.replay.backlog: pending messages replayed on reconnect
- mqtt.ledger.duration: the push-ledger write in the adapter
Accumulate per-project connections and deliveries in the broker worker and
flush them to the stats-usage queue every 10s, so push-broker activity shows
up in project usage. Adds METRIC_MQTT_CONNECTIONS and METRIC_MQTT_MESSAGES_DELIVERED
(cumulative, summed by StatsUsage), a publisherForUsage resource in the broker,
and recordConnection/recordDeliveries/flushUsage on the Mqtt adapter.

@greptile-apps greptile-apps Bot left a comment

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.

Your trial has ended. Reactivate Greptile to resume code reviews.

@ghost

ghost commented Oct 1, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The incremental changes are consistent with their callers and tests, with no concrete defects found.

Adds MQTT delivery, replay, connection-refusal, and ledger telemetry, along with per-project connection and delivery usage accumulated in the broker and queued periodically. Publication timestamps travel through pub/sub for delivery-latency measurement, and unit and end-to-end tests cover usage accounting. The broker now flushes usage every 60 seconds and records failed push attempts in a dedicated counter.

Latest changes: The newest commits extend the usage flush interval to 60 seconds, adjust the end-to-end polling window accordingly, and add an MQTT failed-message counter for push errors.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 7
File Change
app/init/constants.php Defines MQTT connection and delivered-message usage metric keys.
app/mqtt.php Registers the usage publisher, flushes project usage every 60 seconds, and forwards publication timestamps.
src/Appwrite/Messaging/Adapter/Mqtt.php Adds telemetry instruments, including failed messages, and per-project usage accumulation and draining.
src/Appwrite/Messaging/Adapter/Push/Appwrite.php Carries publication timestamps, measures ledger writes, and counts failed push attempts.
src/Appwrite/Mqtt/Handler.php Records connection refusals, delivery latency, replay backlog, and live and replay usage.
tests/e2e/Services/Mqtt/MqttServerTest.php Tests usage in an isolated project with polling adjusted for the 60-second flush interval.
tests/unit/Appwrite/Messaging/Adapter/MqttTest.php Tests usage isolation, accumulation, draining, and invalid inputs.

Reviewed the commits since 2dfa74d · 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 B · 2 blocking findings to address. Summary

Comment thread src/Appwrite/Mqtt/Handler.php Resolved
Comment thread src/Appwrite/Mqtt/Handler.php Resolved
Comment thread src/Appwrite/Messaging/Adapter/Mqtt.php Resolved
…liveries

The broker Server already owns mqtt.connections.active, mqtt.connections.opened
and mqtt.subscriptions via setTelemetry, so the Handler's connections.active,
connect.result and subscribe.outcome double-counted (and the disconnect decrement
could go negative, since identity is set before the abuse check). Remove them and
leave that accounting to the broker. Replay re-deliveries now count toward both
the delivered counter and per-project usage, which previously missed them.

@greptile-apps greptile-apps Bot left a comment

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.

Your trial has ended. Reactivate Greptile to resume code reviews.

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → telemetry-mqtt (after).

Metric Before After Change
🚀 Requests/sec 171.19 176.9 ⚪ +3.3%
⏱️ Latency P50 97.88 ms 97.81 ms ⚪ -0.1%
⏱️ Latency P95 245.89 ms 232.11 ms 🟢 -5.6%
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.81 232.11 11,286 176.9 -13.77
Account 187.35 357.06 594 9.92 -120.92
TablesDB 94.53 185.25 6,138 98.77 -4.46
Storage 88.67 191.92 2,970 49.41 -20.63
Functions 139.27 281.28 1,584 27.02 -25.62

Top API waits (after)

API request Max wait (ms)
account.name.update 592.26
account.prefs.update 480.54
functions.variables.update 461.03
tablesdb.rows.increment 440.35
functions.create 425.83

@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 src/Appwrite/Messaging/Adapter/Mqtt.php Resolved
The broker counts accepted/rejected CONNECTs but not why a refusal happened.
Re-add a refusal-reason counter (mqtt.connect.refused, reason=not_authorized
or rate_limited) emitted only on the refuse paths, so the auth-failure vs
rate-limit distinction survives without duplicating the broker's totals.
e2e: a user-targeted push to a live subscriber must surface as non-empty
mqtt.messages.delivered and mqtt.connections usage series for the project,
covering the broker-side flush. Skips where the stack has no usage pipeline.

@greptile-apps greptile-apps Bot left a comment

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.

Your trial has ended. Reactivate Greptile to resume code reviews.

@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

The usage assertion ran against the shared test project, which earlier tests
already fill with deliveries, so it passed even if live accounting were removed.
Use getProject(fresh: true): a project with no prior MQTT traffic, so a non-zero
mqtt.messages.delivered/mqtt.connections series can only come from this test.

@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

A 10s flush cycles the queue/DB work six times more often than needed for
usage aggregation. Widen it to 60s and loosen the e2e usage wait accordingly.
Add mqtt.messages.failed, incremented when a per-target publish/persist throws
in the Appwrite push adapter, so failed sends are observable alongside
published/delivered/dropped.
@ArnabChatterjee20k
ArnabChatterjee20k merged commit efd2707 into main Oct 1, 2026
49 checks passed
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.

1 participant