|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Krenor\Prometheus\Metrics; |
|
4
|
|
|
|
|
5
|
|
|
use Krenor\Prometheus\Contracts\Storage; |
|
6
|
|
|
use Tightenco\Collect\Support\Collection; |
|
7
|
|
|
use Krenor\Prometheus\Exceptions\LabelException; |
|
8
|
|
|
use Krenor\Prometheus\Exceptions\PrometheusException; |
|
9
|
|
|
use Krenor\Prometheus\Contracts\Metric as MetricContract; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Metric implements MetricContract |
|
12
|
|
|
{ |
|
13
|
|
|
protected string $namespace; |
|
14
|
|
|
|
|
15
|
|
|
protected string $name; |
|
16
|
|
|
|
|
17
|
|
|
protected string $description; |
|
18
|
|
|
|
|
19
|
|
|
protected array $labels = []; |
|
20
|
|
|
|
|
21
|
|
|
protected static Storage $storage; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Metric constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @throws LabelException |
|
27
|
|
|
*/ |
|
28
|
108 |
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
108 |
|
foreach ($this->labels as $label) { |
|
31
|
76 |
|
if (!preg_match('/^(?![_]{2})[a-zA-Z_][a-zA-Z0-9_]*$/', $label)) { |
|
32
|
1 |
|
throw new LabelException("The label `{$label}` contains invalid characters."); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
107 |
|
if (!preg_match('/^[a-zA-Z_:][a-zA-Z0-9_:]*$/', $this->key())) { |
|
37
|
1 |
|
throw new PrometheusException("The metric name `{$this->key()}` contains invalid characters."); |
|
38
|
|
|
} |
|
39
|
106 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
107 |
|
public function key(): string |
|
45
|
|
|
{ |
|
46
|
107 |
|
return "{$this->namespace()}_{$this->name()}"; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract public function type(): string; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
107 |
|
public function namespace(): string |
|
58
|
|
|
{ |
|
59
|
107 |
|
return $this->namespace; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
107 |
|
public function name(): string |
|
66
|
|
|
{ |
|
67
|
107 |
|
return $this->name; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
26 |
|
public function description(): string |
|
74
|
|
|
{ |
|
75
|
26 |
|
return $this->description; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Collection |
|
80
|
|
|
*/ |
|
81
|
91 |
|
public function labels(): Collection |
|
82
|
|
|
{ |
|
83
|
91 |
|
return new Collection($this->labels); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Storage $storage |
|
88
|
|
|
*/ |
|
89
|
25 |
|
public static function storeUsing(Storage $storage): void |
|
90
|
|
|
{ |
|
91
|
25 |
|
static::$storage = $storage; |
|
92
|
25 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return Storage |
|
96
|
|
|
*/ |
|
97
|
25 |
|
public static function storage(): Storage |
|
98
|
|
|
{ |
|
99
|
25 |
|
return static::$storage; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|