@@ -603,6 +603,9 @@ public function log(string $metric, int $value, string $period = Usage::PERIOD_1
603603 ];
604604 Metric::validate ($ data );
605605
606+ // Normalize tags for deterministic hashing
607+ ksort ($ tags );
608+
606609 // Period-aligned time so increments fall into the correct bucket
607610 $ now = new \DateTime ();
608611 $ time = $ period === Usage::PERIOD_INF
@@ -611,11 +614,8 @@ public function log(string $metric, int $value, string $period = Usage::PERIOD_1
611614 $ timestamp = $ this ->formatDateTime ($ time );
612615
613616 // Deterministic id so SummingMergeTree will aggregate increments for the same group
614- $ idComponents = [$ timestamp , $ period , $ metric ];
615- if ($ this ->sharedTables ) {
616- $ idComponents [] = (string )$ this ->tenant ;
617- }
618- $ id = md5 (implode ('_ ' , $ idComponents ));
617+ $ tenant = $ this ->sharedTables ? $ this ->tenant : null ;
618+ $ id = $ this ->buildDeterministicId ($ metric , $ period , $ timestamp , $ tenant );
619619
620620 // Build insert columns dynamically from attributes
621621 $ insertColumns = ['id ' ];
@@ -760,6 +760,10 @@ public function logBatch(array $metrics): bool
760760
761761 foreach ($ metrics as $ metricData ) {
762762 $ period = $ metricData ['period ' ] ?? Usage::PERIOD_1H ;
763+ $ metric = $ metricData ['metric ' ];
764+ $ value = $ metricData ['value ' ];
765+ $ tags = $ metricData ['tags ' ] ?? [];
766+ ksort ($ tags );
763767
764768 // Period-aligned time so increments fall into the correct bucket
765769 $ now = new \DateTime ();
@@ -768,14 +772,9 @@ public function logBatch(array $metrics): bool
768772 : $ now ->format (Usage::PERIODS [$ period ]);
769773 $ timestamp = $ this ->formatDateTime ($ time );
770774
771- $ idComponents = [$ timestamp , $ period , $ metric ];
772- if ($ this ->sharedTables ) {
773- $ idComponents [] = (string )$ this ->tenant ;
774- }
775- $ id = md5 (implode ('_ ' , $ idComponents ));
776-
777- $ metric = $ metricData ['metric ' ];
778- $ value = $ metricData ['value ' ];
775+ // Deterministic id for aggregation
776+ $ tenant = $ this ->sharedTables ? $ this ->tenant : null ;
777+ $ id = $ this ->buildDeterministicId ($ metric , $ period , $ timestamp , $ tenant );
779778
780779 $ valuePlaceholders = [];
781780
@@ -867,7 +866,6 @@ public function find(array $queries = []): array
867866 // Build LIMIT and OFFSET
868867 $ limitClause = isset ($ parsed ['limit ' ]) ? ' LIMIT {limit:UInt64} ' : '' ;
869868 $ offsetClause = isset ($ parsed ['offset ' ]) ? ' OFFSET {offset:UInt64} ' : '' ;
870-
871869 $ sql = "
872870 SELECT {$ selectColumns }
873871 FROM {$ escapedTable }{$ whereClause }{$ orderClause }{$ limitClause }{$ offsetClause }
0 commit comments