PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / prometheus / Collector.php

Collector.php in DecaLog 4.4.0, at includes/libraries/prometheus/Collector.php

105 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Prometheus;
6
7 use InvalidArgumentException;
8 use Prometheus\Storage\Adapter;
9
10 abstract class Collector
11 {
12 const RE_METRIC_LABEL_NAME = '/^[a-zA-Z_:][a-zA-Z0-9_:]*$/';
13
14 /**
15 * @var Adapter
16 */
17 protected $storageAdapter;
18
19 /**
20 * @var string
21 */
22 protected $name;
23
24 /**
25 * @var string
26 */
27 protected $help;
28
29 /**
30 * @var string[]
31 */
32 protected $labels;
33
34 /**
35 * @param Adapter $storageAdapter
36 * @param string $namespace
37 * @param string $name
38 * @param string $help
39 * @param string[] $labels
40 */
41 public function __construct(Adapter $storageAdapter, string $namespace, string $name, string $help, array $labels = [])
42 {
43 $this->storageAdapter = $storageAdapter;
44 $metricName = ($namespace !== '' ? $namespace . '_' : '') . $name;
45 if (preg_match(self::RE_METRIC_LABEL_NAME, $metricName) !== 1) {
46 throw new InvalidArgumentException("Invalid metric name: '" . $metricName . "'");
47 }
48 $this->name = $metricName;
49 $this->help = $help;
50 foreach ($labels as $label) {
51 if (preg_match(self::RE_METRIC_LABEL_NAME, $label) !== 1) {
52 throw new InvalidArgumentException("Invalid label name: '" . $label . "'");
53 }
54 }
55 $this->labels = $labels;
56 }
57
58 /**
59 * @return string
60 */
61 abstract public function getType(): string;
62
63 /**
64 * @return string
65 */
66 public function getName(): string
67 {
68 return $this->name;
69 }
70
71 /**
72 * @return string[]
73 */
74 public function getLabelNames(): array
75 {
76 return $this->labels;
77 }
78
79 /**
80 * @return string
81 */
82 public function getHelp(): string
83 {
84 return $this->help;
85 }
86
87 /**
88 * @return string
89 */
90 public function getKey(): string
91 {
92 return sha1($this->getName() . serialize($this->getLabelNames()));
93 }
94
95 /**
96 * @param string[] $labels
97 */
98 protected function assertLabelsAreDefinedCorrectly(array $labels): void
99 {
100 if (count($labels) !== count($this->labels)) {
101 throw new InvalidArgumentException(sprintf('Labels are not defined correctly: %s', print_r($labels, true)));
102 }
103 }
104 }
105