Skip to content

Add ObservableGauge metric type#6

Merged
loks0n merged 3 commits intomainfrom
feat-observable-gauge
Apr 1, 2026
Merged

Add ObservableGauge metric type#6
loks0n merged 3 commits intomainfrom
feat-observable-gauge

Conversation

@loks0n
Copy link
Copy Markdown
Contributor

@loks0n loks0n commented Apr 1, 2026

Summary

  • Adds ObservableGauge abstract base class — an asynchronous gauge whose value is collected via a callback at export time
  • Implements createObservableGauge() across all three adapters (None, Test, OpenTelemetry)
  • Adds tests to OpenTelemetryTestCase covering creation, observe callback, caching, and null parameters
  • Expands README with usage examples for all metric types including ObservableGauge

Usage

$gauge = $telemetry->createObservableGauge('process.cpu.usage', '%', 'CPU usage');

$gauge->observe(function (callable $observer): void {
    $observer(sys_getloadavg()[0] * 100);
    $observer(72.4, ['core' => '0']);
});

Test plan

  • composer lint (Pint PSR-12) passes
  • composer check (PHPStan level max) passes
  • All new and existing tests pass

🤖 Generated with Claude Code

Implements an asynchronous gauge whose value is collected via a callback
at export time, following the OpenTelemetry observable instrument pattern.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 1, 2026

Greptile Summary

This PR adds ObservableGauge — an asynchronous gauge metric type whose observation callback is invoked at collection time — across the full adapter stack (None, Test, OpenTelemetry), along with tests and README docs.

The implementation is clean and well-structured:

  • The previous concern about OTel-level callback accumulation is correctly resolved: the anonymous class in OpenTelemetry::createObservableGauge() registers the OTel observe() callback exactly once in its constructor and delegates to a single, replaceable $this->callback closure. Subsequent calls to observe() simply swap the closure rather than registering a new OTel-level callback.
  • Caching via createMeter() ensures the same ObservableGauge instance (and thus the same single OTel registration) is returned for repeated calls with the same name.
  • The Test adapter exposes the stored \\Closure as a public field on the anonymous class, consistent with how other metric types in that adapter work.
  • README corrections (OTLP endpoint path /v1/metrics, expanded metric-type examples) are accurate.

Confidence Score: 5/5

Safe to merge — no P0/P1 issues found; the prior callback-accumulation concern is properly addressed.

All changed files reviewed; the critical prior concern about OTel callback accumulation is correctly resolved by registering the OTel-level callback once in the constructor. No new bugs or regressions identified. Tests cover creation, callback invocation, caching, and null parameters.

No files require special attention.

Important Files Changed

Filename Overview
src/Telemetry/Adapter/OpenTelemetry.php Adds createObservableGauge() that registers the OTel-level callback exactly once in the anonymous class constructor and exposes a replaceable user-supplied closure — correctly addressing the prior review concern about callback accumulation.
src/Telemetry/ObservableGauge.php New abstract base class for the asynchronous gauge type with a well-typed observe() contract. Clean, minimal design.
src/Telemetry/Adapter/Test.php Adds createObservableGauge() that stores the callback as a public Closure field on the anonymous class, consistent with existing Test adapter patterns. No caching, also consistent with other metric types in this adapter.
tests/Telemetry/Adapter/OpenTelemetryTestCase.php Adds four test cases covering creation, observe callback invocation, caching (same instance returned), and null parameters. All tests align with the implemented behaviour.
src/Telemetry/Adapter/None.php Adds a correct no-op implementation of createObservableGauge().
src/Telemetry/Adapter.php Interface updated with createObservableGauge() including @param advisory docblock, matching the existing method signatures.
README.md Expands docs with a Metric Types section covering all five metric types and corrects the OTLP endpoint path (/v1/metrics). Accurate and clear.

Reviews (3): Last reviewed commit: "Align Test adapter observe() to replace ..." | Re-trigger Greptile

loks0n and others added 2 commits April 1, 2026 14:24
… calls

Register the OTel-level callback once in the constructor and delegate to a
single replaceable closure, so calling observe() again replaces rather than
adds another callback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…try adapter

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@loks0n loks0n merged commit 62bbada into main Apr 1, 2026
6 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