Skip to content

Commit 00c52fa

Browse files
lohanidamodarclaude
andcommitted
fix: CI failures - PHPStan types, index lengths, test assertions for country/userAgent
- Remove stale 'type' key from addBatch() @param array shape in Usage.php, Adapter.php, Database.php - Fix mixed-to-string cast in ClickHouse.php event column extraction with type-safe checks - Reduce path size from 1024 to 255 and userAgent size from 512 to 255 in Metric::getEventSchema() to stay within MySQL 768-byte index limit - Update MetricTest assertions: 11 attributes, 9 indexes, 7 EVENT_COLUMNS - Update ClickHouseTest: userAgent/country are now event columns, not tags Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1cb3178 commit 00c52fa

7 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/Usage/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract public function setup(): void;
2828
* For events, path/method/status/resource/resourceId are extracted from tags
2929
* into dedicated columns; remaining tags stay in the tags JSON.
3030
*
31-
* @param array<array{metric: string, value: int, type: string, tags?: array<string,mixed>}> $metrics
31+
* @param array<array{metric: string, value: int, tags?: array<string,mixed>}> $metrics
3232
* @param string $type Metric type: 'event' or 'gauge' — determines which table to write to
3333
* @param int $batchSize Maximum number of metrics per INSERT statement
3434
*/

src/Usage/Adapter/ClickHouse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,8 @@ public function addBatch(array $metrics, string $type = Usage::TYPE_EVENT, int $
13351335
$eventColumns = [];
13361336
foreach (Metric::EVENT_COLUMNS as $col) {
13371337
if (isset($tags[$col])) {
1338-
$eventColumns[$col] = (string) $tags[$col];
1338+
$tagValue = $tags[$col];
1339+
$eventColumns[$col] = is_string($tagValue) ? $tagValue : (is_scalar($tagValue) ? (string) $tagValue : null);
13391340
unset($tags[$col]);
13401341
} else {
13411342
$eventColumns[$col] = null;

src/Usage/Adapter/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function getColumnDefinition(string $id, string $type = 'event'): stri
101101
* Database adapter uses a single collection for both types. The $type parameter
102102
* is stored as a field in each document for query-time differentiation.
103103
*
104-
* @param array<array{metric: string, value: int, type?: string, tags?: array<string,mixed>}> $metrics
104+
* @param array<array{metric: string, value: int, tags?: array<string,mixed>}> $metrics
105105
* @param string $type Metric type: 'event' or 'gauge'
106106
* @param int $batchSize
107107
* @return bool

src/Usage/Metric.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public static function getEventSchema(): array
430430
[
431431
'$id' => 'path',
432432
'type' => 'string',
433-
'size' => 1024,
433+
'size' => 255,
434434
'required' => false,
435435
'signed' => true,
436436
'array' => false,
@@ -484,7 +484,7 @@ public static function getEventSchema(): array
484484
[
485485
'$id' => 'userAgent',
486486
'type' => 'string',
487-
'size' => 512,
487+
'size' => 255,
488488
'required' => false,
489489
'signed' => true,
490490
'array' => false,

src/Usage/Usage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Usage
2626
* In-memory buffer for metrics.
2727
* Keyed by "{metric}:{type}" — events are summed, gauges use last-write-wins.
2828
*
29-
* @var array<string, array{metric: string, value: int, type: string, tags: array<string,mixed>}>
29+
* @var array<string, array{metric: string, value: int, type: string, tags: array<string, mixed>}>
3030
*/
3131
private array $buffer = [];
3232

@@ -84,7 +84,7 @@ public function setup(): void
8484
/**
8585
* Add metrics in batch (raw append).
8686
*
87-
* @param array<array{metric: string, value: int, type: string, tags?: array<string,mixed>}> $metrics
87+
* @param array<array{metric: string, value: int, tags?: array<string,mixed>}> $metrics
8888
* @param string $type Metric type: 'event' or 'gauge'
8989
* @param int $batchSize Maximum number of metrics per INSERT statement
9090
* @return bool

tests/Usage/Adapter/ClickHouseTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ public function testEventColumnsExtractedFromTags(): void
224224
'status' => '201',
225225
'resource' => 'bucket',
226226
'resourceId' => 'bucket123',
227-
'region' => 'us-east',
227+
'country' => 'US',
228228
'userAgent' => 'test-agent',
229+
'region' => 'us-east',
229230
],
230231
],
231232
];
@@ -245,16 +246,19 @@ public function testEventColumnsExtractedFromTags(): void
245246
$this->assertEquals('201', $metric->getStatus());
246247
$this->assertEquals('bucket', $metric->getResource());
247248
$this->assertEquals('bucket123', $metric->getResourceId());
249+
$this->assertEquals('US', $metric->getCountry());
250+
$this->assertEquals('test-agent', $metric->getUserAgent());
248251

249252
// Remaining tags should only contain non-event fields
250253
$tags = $metric->getTags();
251254
$this->assertEquals('us-east', $tags['region'] ?? null);
252-
$this->assertEquals('test-agent', $tags['userAgent'] ?? null);
253255
$this->assertArrayNotHasKey('path', $tags);
254256
$this->assertArrayNotHasKey('method', $tags);
255257
$this->assertArrayNotHasKey('status', $tags);
256258
$this->assertArrayNotHasKey('resource', $tags);
257259
$this->assertArrayNotHasKey('resourceId', $tags);
260+
$this->assertArrayNotHasKey('country', $tags);
261+
$this->assertArrayNotHasKey('userAgent', $tags);
258262
}
259263

260264
/**

tests/Usage/MetricTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testGetEventSchemaReturnsAttributeDefinitions(): void
1515
$schema = Metric::getEventSchema();
1616

1717
$this->assertIsArray($schema);
18-
$this->assertCount(9, $schema);
18+
$this->assertCount(11, $schema);
1919

2020
// Test metric attribute
2121
$metricAttr = $schema[0];
@@ -57,8 +57,18 @@ public function testGetEventSchemaReturnsAttributeDefinitions(): void
5757
$this->assertEquals('resourceId', $resourceIdAttr['$id']);
5858
$this->assertFalse($resourceIdAttr['required']);
5959

60+
// Test country attribute (optional)
61+
$countryAttr = $schema[8];
62+
$this->assertEquals('country', $countryAttr['$id']);
63+
$this->assertFalse($countryAttr['required']);
64+
65+
// Test userAgent attribute (optional)
66+
$userAgentAttr = $schema[9];
67+
$this->assertEquals('userAgent', $userAgentAttr['$id']);
68+
$this->assertFalse($userAgentAttr['required']);
69+
6070
// Test tags attribute (optional)
61-
$tagsAttr = $schema[8];
71+
$tagsAttr = $schema[10];
6272
$this->assertEquals('tags', $tagsAttr['$id']);
6373
$this->assertEquals('string', $tagsAttr['type']);
6474
$this->assertFalse($tagsAttr['required']);
@@ -98,7 +108,7 @@ public function testGetEventIndexesReturnsIndexDefinitions(): void
98108
$indexes = Metric::getEventIndexes();
99109

100110
$this->assertIsArray($indexes);
101-
$this->assertCount(7, $indexes);
111+
$this->assertCount(9, $indexes);
102112

103113
// Test metric index
104114
$metricIndex = $indexes[0];
@@ -117,6 +127,8 @@ public function testGetEventIndexesReturnsIndexDefinitions(): void
117127
$this->assertEquals('index-status', $indexes[4]['$id']);
118128
$this->assertEquals('index-resource', $indexes[5]['$id']);
119129
$this->assertEquals('index-resourceId', $indexes[6]['$id']);
130+
$this->assertEquals('index-country', $indexes[7]['$id']);
131+
$this->assertEquals('index-userAgent', $indexes[8]['$id']);
120132
}
121133

122134
/**
@@ -668,7 +680,7 @@ public function testToArrayReturnsArray(): void
668680
*/
669681
public function testEventColumnsConstant(): void
670682
{
671-
$expected = ['path', 'method', 'status', 'resource', 'resourceId'];
683+
$expected = ['path', 'method', 'status', 'resource', 'resourceId', 'country', 'userAgent'];
672684
$this->assertEquals($expected, Metric::EVENT_COLUMNS);
673685
}
674686
}

0 commit comments

Comments
 (0)