Repository navigation
perf(telemetry, circuit-breaker): record less per call on the cache path - #280
Conversation
Profiling the cloud API showed the OpenTelemetry metrics SDK at 11% of busy CPU, with every cache load behind it emitting eight metric records: the circuit breaker wrote its state, failure and success gauges and an in-flight up/down counter on each call, on top of the call counter, and the SDK walked an exemplar reservoir for each of them. circuit-breaker: state, failures, successes and active calls are now observable gauges read when the adapter collects, not written on every call. A breaker that receives no calls still reports its state on every collection, which the per-call gauges did not. The per-call cost drops to one counter add; metric names and attributes are unchanged. telemetry: the meter provider declares no exemplars. Nothing that uses the adapter starts traces, so the SDK's default filter accepted nothing, but it still cost a reservoir offer on every record. Measured on a warm-cache Database::getDocument through Cache + CircuitBreaker + OpenTelemetry (5k iterations, PHP 8.5, telemetry overhead per cache load): 15.7 µs before, 8.0 µs after (together with the cache memo change this brings a cache load from 72.9 µs to 22.8 µs CPU). Refs appwrite-labs/cloud#5861. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Review of e199838:
Validation: circuit-breaker unit suite passed (34 tests, 106 assertions); non-Swoole telemetry tests passed (8 tests, 53 assertions), including the new sampled-span exemplar test. Used the existing PHP 8.5.10 dependency/toolchain installation with a temporary autoloader pointing at this PR's sources. The full telemetry suite exited 139 in transport tests; this matches the limitation described in the PR, but I did not independently rerun the full suite on main. No source changes made. |
…refreshed at collection Review of the observable gauges found two holes. The closures captured the breaker strongly and both telemetry adapters accumulate callbacks per instrument, so every discarded breaker stayed alive and kept reporting, and rebinding an adapter registered again without releasing the old one. And the callbacks read the local snapshot, so an idle breaker kept reporting a verdict another process had since changed. One observation per adapter and metric prefix now walks a WeakMap of the breakers bound to it: a discarded breaker drops out of collection and is not retained, a breaker bound twice reports once, and moving to another adapter removes it from the first. The state, failures and successes callbacks refresh the shared snapshot from the cache adapter before reporting, without applying the open timeout, so collecting telemetry never moves the circuit. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Both points addressed in 55837b1. Callback lifecycle. The breaker no longer registers closures over Shared state at collection. The state, failures and successes callbacks call Side note from writing the test: Rector's unused-variable rule dropped
|
Why
Profiling the cloud API (appwrite-labs/cloud#5861) showed the OpenTelemetry metrics SDK at 11% of busy CPU (
MetricAggregator::record,AttributesBuilder,FilteredReservoir::offer). Every cache load behind it emitted eight metric records: the circuit breaker wrote its state, failure and success gauges and an in-flight up/down counter on each call, on top of the call counter, the cache wrote its duration histogram and hit/miss counter, and the SDK walked an exemplar reservoir for each of them. A single record costs about 1.3 µs on PHP 8.5.What
circuit-breaker:
breaker.state,breaker.failures,breaker.successesandbreaker.active_callsare now observable gauges read when the adapter collects, not written on every call. The per-call cost drops to one counter add (breaker.calls). Metric names and attributes are unchanged, so the Circuit Breaker dashboard keeps working. Two behaviour changes worth knowing:breaker.active_callswas an up/down counter summed from ±1 writes and is now a gauge sampled at collection. Same name, same meaning, but a collection between increment and decrement is what it reports. It is not used on any dashboard.testActiveCallTelemetryUsesPostUpdateStatebecomestestStateObservedDuringACallIsThePostUpdateState: the state observed while the half-open probe runs is the post-update one, with the probe in flight. A new test checks that gauges are observable without any call and that a call in flight is visible while it runs. The package's PHPStan ignore for theUtopia\Telemetry\Adapter\Testdouble's runtime properties is extended toObservableGauge::$callbacks, the same shape it already ignores for$values.telemetry: the meter provider declares
NoneExemplarFilter. Nothing that uses this adapter starts traces, so the SDK's defaultWithSampledTraceExemplarFilteraccepted nothing, but each record still walked the reservoir to find that out. The new test records under a sampled span and asserts the export carries no exemplars; it fails onmain.Measured
Warm-cache
Database::getDocumentthroughCache+CircuitBreaker+OpenTelemetrywith a discarding transport, PHP 8.5, 5k iterations, CPU µs per operation. Telemetry overhead is the OpenTelemetry-on minus telemetry-off cost of oneCache::load:Cache::loadCache::load(OpenTelemetry on, with #279)Database::getCollection(OpenTelemetry on, with #279)record()The exemplar change alone is small per record; the record count is what moves the number. Pairs with #279 (cache decoded-envelope memo) for the second and third rows.
Checks
bin/monorepo check telemetryandbin/monorepo check circuit-breakerpass.bin/monorepo test circuit-breakerpasses.bin/monorepo test telemetryexits with code 139 in the Swoole transport tests on this host (macOS, PHP 8.5.10) on unchangedmainas well; the whole telemetry suite passes when run with the rootvendor/bin/phpunitagainst the package'svendor/autoload.php(24 tests), with and without this change.🤖 Generated with Claude Code