You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Introduced increment and set methods for metrics, replacing log and logCounter.
- Added in-memory buffers for increment and counter metrics with automatic flushing.
- Implemented collect and collectSet methods for deferred metric accumulation.
- Enhanced flush logic to handle both increment and set metrics, clearing buffers post-flush.
- Added configuration options for flush thresholds and intervals.
- Updated tests to reflect changes in metric logging and buffer management.
Values **replace** the existing value when the same metric/period/time bucket already exists. Use for periodic recounts or resource gauges (e.g., current storage size, active user count).
135
+
136
+
```php
137
+
// Single metric, auto fan-out to all periods (1h, 1d, inf)
Log multiple metrics in batch for better performance.
150
+
For high-throughput scenarios (e.g., inside a request loop or worker), use `collect()` / `collectSet()` to accumulate metrics in memory and `flush()` to write them in batch.
123
151
124
152
```php
125
-
$metrics = [
126
-
[
127
-
'metric' => 'requests',
128
-
'value' => 100,
129
-
'period' => '1h',
130
-
'tags' => ['region' => 'us-east'],
131
-
],
132
-
[
133
-
'metric' => 'bandwidth',
134
-
'value' => 50000,
135
-
'period' => '1h',
136
-
'tags' => ['region' => 'us-east'],
137
-
],
138
-
];
139
-
140
-
$usage->logBatch($metrics);
153
+
// Accumulate increment metrics (values are summed in-memory)
154
+
$usage->collect('requests', 1);
155
+
$usage->collect('requests', 1);
156
+
$usage->collect('bandwidth', 5000);
157
+
158
+
// Accumulate set metrics (last-write-wins in-memory)
159
+
$usage->collectSet('storage.size', 1048576);
160
+
161
+
// Check if flush is recommended (threshold or interval reached)
162
+
if ($usage->shouldFlush()) {
163
+
$usage->flush();
164
+
}
165
+
166
+
// Or flush explicitly
167
+
$usage->flush();
141
168
```
142
169
143
-
**Get Usage By Period**
170
+
### Flush Configuration
144
171
145
-
Fetch all usage metrics by period.
172
+
```php
173
+
// Flush when 5000 collect() calls have been made (default: 10,000)
174
+
$usage->setFlushThreshold(5000);
175
+
176
+
// Flush when 10 seconds have elapsed since last flush (default: 20)
177
+
$usage->setFlushInterval(10);
178
+
```
179
+
180
+
## Querying Metrics
181
+
182
+
**Get Usage By Period**
146
183
147
184
```php
148
185
$metrics = $usage->getByPeriod('requests', '1h');
149
-
// Returns an array of all usage metrics for specific period
0 commit comments