$counters */ public function __construct( private array $counters = [] ) { } public function get(string $name): Counter { return $this->counters[$name] ?? new Counter($name, 0); } public function increment(string $name, int $by = 1): self { $current = $this->get($name); $updated = $current->increment($by); return new self([ ...$this->counters, $name => $updated, ]); } public function reset(string $name): self { $current = $this->get($name); $updated = $current->reset(); return new self([ ...$this->counters, $name => $updated, ]); } public function resetAll(): self { return new self([]); } /** * Get all counters * @return array */ public function all(): array { return $this->counters; } /** * Convert to simple array for serialization * @return array */ public function toArray(): array { return array_map( fn (Counter $counter) => $counter->value, $this->counters ); } /** * Export counters as MetricsCollection for Prometheus compatibility */ public function toMetricsCollection(): MetricsCollection { $collection = new MetricsCollection(); foreach ($this->counters as $counter) { $collection->counter( name: "framework_{$counter->name}", value: (float) $counter->value, help: "Framework internal counter: {$counter->name}", labels: ['type' => 'internal'] ); } return $collection; } }