PrometheusExporter::incCounter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 13
rs 10
ccs 5
cts 7
cp 0.7143
crap 2.0932
1
<?php
0 ignored issues
show
Coding Style introduced by
Filename "PrometheusExporter.php" doesn't match the expected filename "prometheusexporter.php"
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
Coding Style introduced by
Missing short description in doc comment
Loading history...
13
     * @var CollectorRegistry
14
     */
15
    private $registry;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "registry" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "registry" must be prefixed with an underscore
Loading history...
16
17
    /**
18
     * LpeManager constructor.
19
     *
20
     * @param CollectorRegistry $registry
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
21 2
     */
22
    public function __construct(CollectorRegistry $registry)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
23 2
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
24 2
        $this->registry = $registry;
25
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
26
27
    /**
28
     * Get metric family samples
29
     *
30
     * @return MetricFamilySamples[]
31 2
     */
32
    public function getMetricFamilySamples()
33 2
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
34
        return $this->registry->getMetricFamilySamples();
35
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end getMetricFamilySamples()
Loading history...
36
    
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $help should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $namespace should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelKeys should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelValues should have a doc-comment as per coding-style.
Loading history...
38
     * @inheritdoc
39 2
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
40
    public function incCounter($name, $help, $namespace = null, array $labelKeys = [], array $labelValues = [])
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$namespace" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$namespace"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$labelKeys" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelKeys"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$labelValues" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelValues"; expected 0 but found 1
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 111 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
41 2
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
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
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end incCounter()
Loading history...
54
    
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $help should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $namespace should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelKeys should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelValues should have a doc-comment as per coding-style.
Loading history...
56
     * @inheritdoc
57
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
58
    public function incByCounter(
59
        $name,
60
        $help,
61
        $value,
62
        $namespace = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$namespace" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$namespace"; expected 0 but found 1
Loading history...
63
        array $labelKeys = [],
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$labelKeys" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelKeys"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
64
        array $labelValues = []
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$labelValues" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelValues"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
65
    ) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
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
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end incByCounter()
Loading history...
78
    
79
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $help should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $namespace should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelKeys should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelValues should have a doc-comment as per coding-style.
Loading history...
80
     * @inheritdoc
81
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
82
    public function setGauge($name, $help, $value, $namespace = null, array $labelKeys = [], array $labelValues = [])
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$namespace" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$namespace"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$labelKeys" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelKeys"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$labelValues" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelValues"; expected 0 but found 1
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 117 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
83
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
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
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end setGauge()
Loading history...
96
    
97
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $help should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $namespace should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelKeys should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelValues should have a doc-comment as per coding-style.
Loading history...
98
     * @inheritdoc
99
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
100
    public function incGauge($name, $help, $namespace = null, array $labelKeys = [], array $labelValues = [])
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$namespace" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$namespace"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$labelKeys" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelKeys"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$labelValues" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelValues"; expected 0 but found 1
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 109 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
101
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
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
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end incGauge()
Loading history...
114
    
115
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $help should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $namespace should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelKeys should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelValues should have a doc-comment as per coding-style.
Loading history...
116
     * @inheritdoc
117
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
118
    public function incByGauge(
119
        $name,
120
        $help,
121
        $value,
122
        $namespace = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$namespace" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$namespace"; expected 0 but found 1
Loading history...
123
        array $labelKeys = [],
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$labelKeys" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelKeys"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
124
        array $labelValues = []
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$labelValues" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelValues"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
125
    ) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
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
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end incByGauge()
Loading history...
138
    
139
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $help should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $namespace should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelKeys should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $labelValues should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $buckets should have a doc-comment as per coding-style.
Loading history...
140
     * @inheritdoc
141
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
142
    public function setHistogram(
143
        $name,
144
        $help,
145
        $value,
146
        $namespace = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$namespace" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$namespace"; expected 0 but found 1
Loading history...
147
        array $labelKeys = [],
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$labelKeys" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelKeys"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
148
        array $labelValues = [],
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$labelValues" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$labelValues"; expected 0 but found 1
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
149
        ?array $buckets = null
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$buckets" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$buckets"; expected 0 but found 1
Loading history...
150
    ) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
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
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
157
        }
158
159
        $histogram->observe($value, $labelValues);
160
161
        $this->pushGateway($this->registry, 'histogram');
162
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end setHistogram()
Loading history...
163 2
    
164
    private function getNamespace(?string $namespace = null) : string
0 ignored issues
show
Coding Style introduced by
Private method name "PrometheusExporter::getNamespace" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$namespace" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$namespace"; expected 0 but found 1
Loading history...
165 2
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
166 2
        if (!$namespace) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
167
            $namespace = config('prometheus-exporter.namespace');
168
        }
169 2
170
        return $namespace;
171
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end getNamespace()
Loading history...
172
    
173
    private function pushGateway(CollectorRegistry $registry, string $job, ?array $groupingKey = null)
0 ignored issues
show
Coding Style introduced by
Private method name "PrometheusExporter::pushGateway" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$groupingKey" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$groupingKey"; expected 0 but found 1
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 102 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
174
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
175
        if (config('prometheus-exporter.adapter') == 'push') {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
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
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end pushGateway()
Loading history...
185
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
186