Skip to content

Commit 7cba673

Browse files
loks0nclaude
andcommitted
Fix ObservableGauge accumulating OTel callbacks on repeated observe() 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>
1 parent c6dfbc7 commit 7cba673

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/Telemetry/Adapter/OpenTelemetry.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,23 @@ public function createObservableGauge(string $name, ?string $unit = null, ?strin
236236
$otelGauge = $this->meter->createObservableGauge($name, $unit, $description, $advisory);
237237

238238
return new class ($otelGauge) extends ObservableGauge {
239+
private ?\Closure $callback = null;
240+
239241
public function __construct(private \OpenTelemetry\API\Metrics\ObservableGaugeInterface $gauge)
240242
{
243+
$this->gauge->observe(function (ObserverInterface $observer): void {
244+
if ($this->callback !== null) {
245+
($this->callback)(function (float|int $value, iterable $attributes = []) use ($observer): void {
246+
/** @var iterable<non-empty-string, array<mixed>|bool|float|int|string|null> $attributes */
247+
$observer->observe($value, $attributes);
248+
});
249+
}
250+
});
241251
}
242252

243253
public function observe(callable $callback): void
244254
{
245-
$this->gauge->observe(function (ObserverInterface $observer) use ($callback): void {
246-
$callback(function (float|int $value, iterable $attributes = []) use ($observer): void {
247-
$observer->observe($value, $attributes);
248-
});
249-
});
255+
$this->callback = \Closure::fromCallable($callback);
250256
}
251257
};
252258
});

0 commit comments

Comments
 (0)