Lines 100.00% 6 / 6
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
PrometheusNumber 100.00% 6 / 6 100.00% 2 / 2 8 100.00% 1 / 1
 formatValue 100.00% 5 / 5 100.00% 1 / 1 7
 convertToEpochSeconds 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Metrics\Exposition;
6
7use Stewart\Contracts\Time\Instant;
8
9final class PrometheusNumber
10{
11    private const float LARGEST_EXACT_INTEGER = 9_007_199_254_740_992.0;
12
13    private const int MICROSECONDS_PER_SECOND = 1_000_000;
14
15    public static function formatValue(float $value): string
16    {
17        return match (true) {
18            is_nan($value) => 'NaN',
19            $value === INF => '+Inf',
20            $value === -INF => '-Inf',
21            floor($value) === $value && abs($value) < self::LARGEST_EXACT_INTEGER => \sprintf('%d', $value),
22            default => var_export($value, true),
23        };
24    }
25
26    public static function convertToEpochSeconds(Instant $instant): float
27    {
28        return $instant->toEpochMicroseconds() / self::MICROSECONDS_PER_SECOND;
29    }
30}