Lines
100.00%
12 / 12
Methods
100.00%
6 / 6
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| createWithSample | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| recordSample | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| recordHistogram | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| hasSamples | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| listSamples | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 7 | final class MetricFamily | |
| 8 | { | |
| 9 | private const string BUCKET_BOUND_LABEL = 'le'; | |
| 10 | ||
| 11 | /** @var list<MetricSample> */ | |
| 12 | private array $samples = []; | |
| 13 | ||
| 14 | /** @param non-empty-string $name */ | |
| 15 | public function __construct( | |
| 16 | public readonly string $name, | |
| 17 | public readonly string $help, | |
| 18 | public readonly MetricType $type, | |
| 19 | ) {} | |
| 20 | ||
| 21 | /** @param non-empty-string $name */ | |
| 22 | public static function createWithSample(string $name, string $help, MetricType $type, MetricLabels $labels, int|float|bool $value): self | |
| 23 | { | |
| 24 | $family = new self($name, $help, $type); | |
| 25 | $family->recordSample($labels, $value); | |
| 26 | ||
| 27 | return $family; | |
| 28 | } | |
| 29 | ||
| 30 | public function recordSample(MetricLabels $labels, int|float|bool $value): void | |
| 31 | { | |
| 32 | $this->samples[] = new MetricSample(MetricSampleSuffix::None, $labels, (float) $value); | |
| 33 | } | |
| 34 | ||
| 35 | public function recordHistogram(MetricLabels $labels, Histogram $histogram): void | |
| 36 | { | |
| 37 | foreach ($histogram->buckets as $bucket) { | |
| 38 | $bucketLabels = $labels->withLabel(self::BUCKET_BOUND_LABEL, PrometheusNumber::formatValue($bucket->upperBound)); | |
| 39 | $this->samples[] = new MetricSample(MetricSampleSuffix::Bucket, $bucketLabels, $bucket->cumulativeCount); | |
| 40 | } | |
| 41 | ||
| 42 | $this->samples[] = new MetricSample(MetricSampleSuffix::Sum, $labels, $histogram->sum->toSeconds()); | |
| 43 | $this->samples[] = new MetricSample(MetricSampleSuffix::Count, $labels, $histogram->count); | |
| 44 | } | |
| 45 | ||
| 46 | public function hasSamples(): bool | |
| 47 | { | |
| 48 | return $this->samples !== []; | |
| 49 | } | |
| 50 | ||
| 51 | /** @return list<MetricSample> */ | |
| 52 | public function listSamples(): array | |
| 53 | { | |
| 54 | return $this->samples; | |
| 55 | } | |
| 56 | } |