null]; public function __construct(PrometheusRegistryAccessor $prometheusRegistryAccessor) { $this->registry = $prometheusRegistryAccessor->getRegistry(); } public function addLabel(string $label, $optionalValue = true): self { $this->labels[$label] = $optionalValue; return $this; } /** * @internal */ public function getLabels(): array { return $this->labels; } public function setCounter( string $namespace, string $name, string $description = '', float $value = 1, array $labels = [], ): PrometheusWrapper { if (isset($this->countersData[$name])) { $this->countersData[$name]['increments'][] = [ 'value' => $value, 'labels' => $labels, ]; } else { $this->countersData[$name] = [ 'namespace' => $namespace, 'name' => $name, 'description' => $description, 'increments' => [ [ 'value' => $value, 'labels' => $labels, ], ], ]; } return $this; } public function setHistogram( string $namespace, string $name, string $description = '', float $value = 1, array $labels = [], ?array $buckets = null, ): PrometheusWrapper { if (isset($this->histogramData[$name])) { $this->histogramData[$name]['increments'][] = [ 'value' => $value, 'labels' => $labels, ]; } else { $this->histogramData[$name] = [ 'namespace' => $namespace, 'name' => $name, 'description' => $description, 'buckets' => $buckets, 'increments' => [ [ 'value' => $value, 'labels' => $labels, ], ], ]; } return $this; } /** * @throws \Prometheus\Exception\MetricsRegistrationException * *@internal */ public function flush(): self { $this->flushCounters(); $this->flushHistograms(); return $this; } private function flushCounters() { foreach ($this->countersData as $name => $counterData) { if (!isset($this->registeredCounters[$name])) { $this->registeredCounters[$name] = $this->registry->registerCounter( $counterData['namespace'], $counterData['name'], $counterData['description'], array_keys(reset($counterData['increments'])['labels']) ); } /** @var $counter Counter */ $counter = $this->registeredCounters[$name]; foreach ($counterData['increments'] as $increment) { $counter->incBy($increment['value'], array_values($increment['labels'])); } $this->countersData[$name]['increments'] = []; } } private function flushHistograms() { foreach ($this->histogramData as $name => $histData) { $histogram = $this->registry->getOrRegisterHistogram( $histData['namespace'], $histData['name'], $histData['description'], array_keys(reset($histData['increments'])['labels']), $histData['buckets'] ); foreach ($histData['increments'] as $increment) { $histogram->observe($increment['value'], array_values($increment['labels'])); } $this->histogramData[$name]['increments'] = []; } } }