Skip to content

Commit c2ff9fc

Browse files
loks0nclaude
andcommitted
fix(usage): accept tenant/metric id "0" in collect()
empty() treats the string "0" as empty, so a tenant or metric literally named "0" was wrongly rejected. Compare against '' instead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c0cf170 commit c2ff9fc

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/Usage/Accumulator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ public function __construct(Usage $usage)
5353
*/
5454
public function collect(string $tenant, string $metric, int $value, string $type, array $tags = []): self
5555
{
56-
if (empty($tenant)) {
56+
// Compare against '' rather than empty(): the string "0" is a valid
57+
// tenant/metric id but empty("0") is true in PHP.
58+
if ($tenant === '') {
5759
throw new \InvalidArgumentException('Tenant cannot be empty');
5860
}
59-
if (empty($metric)) {
61+
if ($metric === '') {
6062
throw new \InvalidArgumentException('Metric name cannot be empty');
6163
}
6264
if ($value < 0) {

tests/Usage/AccumulatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ public function testEmptyTenantThrows(): void
236236
$this->accumulator->collect('', 'requests', 10, Usage::TYPE_EVENT);
237237
}
238238

239+
public function testTenantZeroIsAccepted(): void
240+
{
241+
// "0" is a valid tenant id even though empty("0") is true in PHP
242+
$this->accumulator->collect('0', 'requests', 10, Usage::TYPE_EVENT);
243+
244+
$this->assertEquals(1, $this->accumulator->count());
245+
246+
$this->assertTrue($this->accumulator->flush());
247+
$this->assertEquals('0', $this->adapter->batches[0]['metrics'][0]['tenant']);
248+
}
249+
239250
public function testEmptyMetricNameThrows(): void
240251
{
241252
$this->expectException(\InvalidArgumentException::class);

0 commit comments

Comments
 (0)