Passed
Push — master ( 918514...b89e50 )
by Biao
03:27
created

HttpRequestCollector::collect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 15
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 27
rs 9.7666
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Components\Prometheus\Collectors;
4
5
use Hhxsv5\LaravelS\Components\Prometheus\PrometheusCollector;
6
7
class HttpRequestCollector extends PrometheusCollector
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class HttpRequestCollector
Loading history...
8
{
9
    public function collect(array $params = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function collect()
Loading history...
10
    {
11
        /**@var \Illuminate\Http\Request $request */
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
12
        /**@var \Illuminate\Http\Response $response */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
13
        list($request, $response) = $params;
14
        if (!$this->config['observe_request']) {
15
            return;
16
        }
17
18
        $cost = microtime(true) - $request->server('REQUEST_TIME_FLOAT');
19
        $status = $response->getStatusCode();
20
        if (isset($this->config['ignored_http_codes'][$status])) {
21
            // Ignore the requests.
22
            return;
23
        }
24
25
        // Http Request Stats
26
        $requestLabels = http_build_query([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
27
            'method' => $request->getMethod(),
28
            'uri'    => $request->getPathInfo(),
29
            'status' => $status,
30
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
31
        // Key Format: prefix+metric_name+metric_type+metric_labels
32
        $countKey = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'http_server_requests_seconds_count', 'summary', $requestLabels]);
33
        $sumKey = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'http_server_requests_seconds_sum', 'summary', $requestLabels]);
34
        apcu_inc($countKey, 1, $success, $this->config['apcu_key_max_age']);
35
        apcu_inc($sumKey, round($cost * 1000000), $success, $this->config['apcu_key_max_age']); // Time unit: μs
0 ignored issues
show
Bug introduced by
round($cost * 1000000) of type double is incompatible with the type integer expected by parameter $step of apcu_inc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        apcu_inc($sumKey, /** @scrutinizer ignore-type */ round($cost * 1000000), $success, $this->config['apcu_key_max_age']); // Time unit: μs
Loading history...
36
    }
37
}