| Conditions | 1 |
| Paths | 1 |
| Total Lines | 3 |
| Code Lines | 1 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 2 |
| CRAP Score | 1 |
| Changes | 0 | ||
| Metric | Value |
|---|---|
| cc | 1 |
| eloc | 1 |
| nc | 1 |
| nop | 1 |
| dl | 0 |
| loc | 3 |
| ccs | 2 |
| cts | 2 |
| cp | 1 |
| crap | 1 |
| rs | 10 |
| c | 0 |
| b | 0 |
| f | 0 |
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | namespace Triadev\PrometheusExporter; |
||
| 3 | |||
| 4 | use Prometheus\PushGateway; |
||
| 5 | use Triadev\PrometheusExporter\Contract\PrometheusExporterContract; |
||
| 6 | use Prometheus\CollectorRegistry; |
||
| 7 | use Prometheus\MetricFamilySamples; |
||
| 8 | use Prometheus\Exception\MetricNotFoundException; |
||
| 9 | |||
| 10 | class PrometheusExporter implements PrometheusExporterContract |
||
| 11 | { |
||
| 12 | /** |
||
|
0 ignored issues
–
show
|
|||
| 13 | * @var CollectorRegistry |
||
| 14 | */ |
||
| 15 | private $registry; |
||
|
0 ignored issues
–
show
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * LpeManager constructor. |
||
| 19 | * |
||
| 20 | * @param CollectorRegistry $registry |
||
|
0 ignored issues
–
show
|
|||
| 21 | 2 | */ |
|
| 22 | public function __construct(CollectorRegistry $registry) |
||
|
0 ignored issues
–
show
|
|||
| 23 | 2 | { |
|
|
0 ignored issues
–
show
|
|||
| 24 | 2 | $this->registry = $registry; |
|
| 25 | } |
||
|
0 ignored issues
–
show
|
|||
| 26 | |||
| 27 | /** |
||
| 28 | * Get metric family samples |
||
| 29 | * |
||
| 30 | * @return MetricFamilySamples[] |
||
| 31 | 2 | */ |
|
| 32 | public function getMetricFamilySamples() |
||
| 33 | 2 | { |
|
|
0 ignored issues
–
show
|
|||
| 34 | return $this->registry->getMetricFamilySamples(); |
||
| 35 | } |
||
|
0 ignored issues
–
show
|
|||
| 36 | |||
| 37 | /** |
||
|
0 ignored issues
–
show
|
|||
| 38 | * @inheritdoc |
||
| 39 | 2 | */ |
|
|
0 ignored issues
–
show
|
|||
| 40 | public function incCounter($name, $help, $namespace = null, array $labelKeys = [], array $labelValues = []) |
||
|
0 ignored issues
–
show
|
|||
| 41 | 2 | { |
|
|
0 ignored issues
–
show
|
|||
| 42 | $namespace = $this->getNamespace($namespace); |
||
| 43 | |||
| 44 | 2 | try { |
|
| 45 | 2 | $counter = $this->registry->getCounter($namespace, $name); |
|
| 46 | 2 | } catch (MetricNotFoundException $e) { |
|
| 47 | $counter = $this->registry->registerCounter($namespace, $name, $help, $labelKeys); |
||
| 48 | } |
||
| 49 | 2 | ||
| 50 | $counter->inc($labelValues); |
||
| 51 | |||
| 52 | $this->pushGateway($this->registry, 'inc'); |
||
| 53 | } |
||
|
0 ignored issues
–
show
|
|||
| 54 | |||
| 55 | /** |
||
|
0 ignored issues
–
show
|
|||
| 56 | * @inheritdoc |
||
| 57 | */ |
||
|
0 ignored issues
–
show
|
|||
| 58 | public function incByCounter( |
||
| 59 | $name, |
||
| 60 | $help, |
||
| 61 | $value, |
||
| 62 | $namespace = null, |
||
|
0 ignored issues
–
show
|
|||
| 63 | array $labelKeys = [], |
||
|
0 ignored issues
–
show
|
|||
| 64 | array $labelValues = [] |
||
|
0 ignored issues
–
show
|
|||
| 65 | ) { |
||
|
0 ignored issues
–
show
|
|||
| 66 | $namespace = $this->getNamespace($namespace); |
||
| 67 | |||
| 68 | try { |
||
| 69 | $counter = $this->registry->getCounter($namespace, $name); |
||
| 70 | } catch (MetricNotFoundException $e) { |
||
| 71 | $counter = $this->registry->registerCounter($namespace, $name, $help, $labelKeys); |
||
| 72 | } |
||
| 73 | |||
| 74 | $counter->incBy($value, $labelValues); |
||
| 75 | |||
| 76 | $this->pushGateway($this->registry, 'incBy'); |
||
| 77 | } |
||
|
0 ignored issues
–
show
|
|||
| 78 | |||
| 79 | /** |
||
|
0 ignored issues
–
show
|
|||
| 80 | * @inheritdoc |
||
| 81 | */ |
||
|
0 ignored issues
–
show
|
|||
| 82 | public function setGauge($name, $help, $value, $namespace = null, array $labelKeys = [], array $labelValues = []) |
||
|
0 ignored issues
–
show
|
|||
| 83 | { |
||
|
0 ignored issues
–
show
|
|||
| 84 | $namespace = $this->getNamespace($namespace); |
||
| 85 | |||
| 86 | try { |
||
| 87 | $gauge = $this->registry->getGauge($namespace, $name); |
||
| 88 | } catch (MetricNotFoundException $e) { |
||
| 89 | $gauge = $this->registry->registerGauge($namespace, $name, $help, $labelKeys); |
||
| 90 | } |
||
| 91 | |||
| 92 | $gauge->set($value, $labelValues); |
||
| 93 | |||
| 94 | $this->pushGateway($this->registry, 'gauge'); |
||
| 95 | } |
||
|
0 ignored issues
–
show
|
|||
| 96 | |||
| 97 | /** |
||
|
0 ignored issues
–
show
|
|||
| 98 | * @inheritdoc |
||
| 99 | */ |
||
|
0 ignored issues
–
show
|
|||
| 100 | public function incGauge($name, $help, $namespace = null, array $labelKeys = [], array $labelValues = []) |
||
|
0 ignored issues
–
show
|
|||
| 101 | { |
||
|
0 ignored issues
–
show
|
|||
| 102 | $namespace = $this->getNamespace($namespace); |
||
| 103 | |||
| 104 | try { |
||
| 105 | $gauge = $this->registry->getGauge($namespace, $name); |
||
| 106 | } catch (MetricNotFoundException $e) { |
||
| 107 | $gauge = $this->registry->registerGauge($namespace, $name, $help, $labelKeys); |
||
| 108 | } |
||
| 109 | |||
| 110 | $gauge->inc($labelValues); |
||
| 111 | |||
| 112 | $this->pushGateway($this->registry, 'inc'); |
||
| 113 | } |
||
|
0 ignored issues
–
show
|
|||
| 114 | |||
| 115 | /** |
||
|
0 ignored issues
–
show
|
|||
| 116 | * @inheritdoc |
||
| 117 | */ |
||
|
0 ignored issues
–
show
|
|||
| 118 | public function incByGauge( |
||
| 119 | $name, |
||
| 120 | $help, |
||
| 121 | $value, |
||
| 122 | $namespace = null, |
||
|
0 ignored issues
–
show
|
|||
| 123 | array $labelKeys = [], |
||
|
0 ignored issues
–
show
|
|||
| 124 | array $labelValues = [] |
||
|
0 ignored issues
–
show
|
|||
| 125 | ) { |
||
|
0 ignored issues
–
show
|
|||
| 126 | $namespace = $this->getNamespace($namespace); |
||
| 127 | |||
| 128 | try { |
||
| 129 | $gauge = $this->registry->getGauge($namespace, $name); |
||
| 130 | } catch (MetricNotFoundException $e) { |
||
| 131 | $gauge = $this->registry->registerGauge($namespace, $name, $help, $labelKeys); |
||
| 132 | } |
||
| 133 | |||
| 134 | $gauge->incBy($value, $labelValues); |
||
| 135 | |||
| 136 | $this->pushGateway($this->registry, 'incBy'); |
||
| 137 | } |
||
|
0 ignored issues
–
show
|
|||
| 138 | |||
| 139 | /** |
||
|
0 ignored issues
–
show
|
|||
| 140 | * @inheritdoc |
||
| 141 | */ |
||
|
0 ignored issues
–
show
|
|||
| 142 | public function setHistogram( |
||
| 143 | $name, |
||
| 144 | $help, |
||
| 145 | $value, |
||
| 146 | $namespace = null, |
||
|
0 ignored issues
–
show
|
|||
| 147 | array $labelKeys = [], |
||
|
0 ignored issues
–
show
|
|||
| 148 | array $labelValues = [], |
||
|
0 ignored issues
–
show
|
|||
| 149 | ?array $buckets = null |
||
|
0 ignored issues
–
show
|
|||
| 150 | ) { |
||
|
0 ignored issues
–
show
|
|||
| 151 | $namespace = $this->getNamespace($namespace); |
||
| 152 | |||
| 153 | try { |
||
| 154 | $histogram = $this->registry->getHistogram($namespace, $name); |
||
| 155 | } catch (MetricNotFoundException $e) { |
||
| 156 | $histogram = $this->registry->registerHistogram($namespace, $name, $help, $labelKeys, $buckets); |
||
|
0 ignored issues
–
show
|
|||
| 157 | } |
||
| 158 | |||
| 159 | $histogram->observe($value, $labelValues); |
||
| 160 | |||
| 161 | $this->pushGateway($this->registry, 'histogram'); |
||
| 162 | } |
||
|
0 ignored issues
–
show
|
|||
| 163 | 2 | ||
| 164 | private function getNamespace(?string $namespace = null) : string |
||
|
0 ignored issues
–
show
|
|||
| 165 | 2 | { |
|
|
0 ignored issues
–
show
|
|||
| 166 | 2 | if (!$namespace) { |
|
|
0 ignored issues
–
show
|
|||
| 167 | $namespace = config('prometheus-exporter.namespace'); |
||
| 168 | } |
||
| 169 | 2 | ||
| 170 | return $namespace; |
||
| 171 | } |
||
|
0 ignored issues
–
show
|
|||
| 172 | |||
| 173 | private function pushGateway(CollectorRegistry $registry, string $job, ?array $groupingKey = null) |
||
|
0 ignored issues
–
show
|
|||
| 174 | { |
||
|
0 ignored issues
–
show
|
|||
| 175 | if (config('prometheus-exporter.adapter') == 'push') { |
||
|
0 ignored issues
–
show
|
|||
| 176 | $pushGateway = new PushGateway(config('prometheus-exporter.push_gateway.address')); |
||
| 177 | |||
| 178 | $pushGateway->push( |
||
| 179 | $registry, |
||
| 180 | $job, |
||
| 181 | $groupingKey |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | } |
||
|
0 ignored issues
–
show
|
|||
| 185 | } |
||
|
0 ignored issues
–
show
|
|||
| 186 |