TrafficMonitor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 29
c 1
b 0
f 1
dl 0
loc 101
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A requestEnd() 0 3 1
A outbound() 0 6 1
A inbound() 0 7 1
A requestBegin() 0 3 1
A exception() 0 6 2
A __construct() 0 8 1
1
<?php
2
/**
3
 * Traffic monitor
4
 * User: moyo
5
 * Date: 23/10/2017
6
 * Time: 10:11 AM
7
 */
8
9
namespace Carno\HRPC\Handlers;
10
11
use Carno\Chain\Layered;
12
use Carno\Coroutine\Context;
13
use Carno\HRPC\Client\Chips\ErrorsClassify;
14
use Carno\Monitor\Metrics;
0 ignored issues
show
Bug introduced by
The type Carno\Monitor\Metrics was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Carno\Monitor\Metrics\Counter;
0 ignored issues
show
Bug introduced by
The type Carno\Monitor\Metrics\Counter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Carno\Monitor\Metrics\Gauge;
0 ignored issues
show
Bug introduced by
The type Carno\Monitor\Metrics\Gauge was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Carno\Monitor\Metrics\Histogram;
0 ignored issues
show
Bug introduced by
The type Carno\Monitor\Metrics\Histogram was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Carno\RPC\Protocol\Request;
19
use Carno\RPC\Protocol\Response;
20
use Throwable;
21
22
class TrafficMonitor implements Layered
23
{
24
    use ErrorsClassify;
25
26
    /**
27
     * @var Counter
28
     */
29
    private $rxBytes = null;
30
31
    /**
32
     * @var Counter
33
     */
34
    private $txBytes = null;
35
36
    /**
37
     * @var Counter
38
     */
39
    private $ttRequests = null;
40
41
    /**
42
     * @var Histogram
43
     */
44
    private $ttResponses = null;
45
46
    /**
47
     * @var Counter
48
     */
49
    private $ttExceptions = null;
50
51
    /**
52
     * @var Gauge
53
     */
54
    private $ttProcessing = null;
55
56
    /**
57
     * TrafficMonitor constructor.
58
     */
59
    public function __construct()
60
    {
61
        $this->rxBytes = Metrics::counter()->named('rpc.rx.bytes');
62
        $this->txBytes = Metrics::counter()->named('rpc.tx.bytes');
63
        $this->ttRequests = Metrics::counter()->named('rpc.requests.all');
64
        $this->ttResponses = Metrics::histogram()->named('rpc.responses.time')->fixed(5, 20, 50, 200, 500, 1000);
65
        $this->ttExceptions = Metrics::counter()->named('rpc.exceptions.all');
66
        $this->ttProcessing = Metrics::gauge()->named('rpc.processing.now');
67
    }
68
69
    /**
70
     * @param Request $request
71
     * @param Context $ctx
72
     * @return Request
73
     */
74
    public function inbound($request, Context $ctx) : Request
75
    {
76
        $this->requestBegin($ctx);
77
        $this->rxBytes->inc(strlen($request->getPayload()));
78
        $this->ttRequests->inc();
79
        $this->ttProcessing->inc();
80
        return $request;
81
    }
82
83
    /**
84
     * @param Response $response
85
     * @param Context $ctx
86
     * @return Response
87
     */
88
    public function outbound($response, Context $ctx) : Response
89
    {
90
        $this->txBytes->inc(strlen($response->getPayload()));
91
        $this->ttProcessing->dec();
92
        $this->requestEnd($ctx);
93
        return $response;
94
    }
95
96
    /**
97
     * @param Throwable $e
98
     * @param Context $ctx
99
     * @throws Throwable
100
     */
101
    public function exception(Throwable $e, Context $ctx) : void
102
    {
103
        $this->ttProcessing->dec();
104
        $this->isGenericException($e) || $this->ttExceptions->inc();
105
        $this->requestEnd($ctx);
106
        throw $e;
107
    }
108
109
    /**
110
     * @param Context $ctx
111
     */
112
    private function requestBegin(Context $ctx) : void
113
    {
114
        $ctx->set('tm-r-begin', microtime(true));
115
    }
116
117
    /**
118
     * @param Context $ctx
119
     */
120
    private function requestEnd(Context $ctx) : void
121
    {
122
        $this->ttResponses->observe((microtime(true) - $ctx->get('tm-r-begin') ?? 0) * 1000);
123
    }
124
}
125