1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Request tracing |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 2018/6/6 |
6
|
|
|
* Time: 2:10 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Web\Handlers; |
10
|
|
|
|
11
|
|
|
use Carno\Chain\Layered; |
12
|
|
|
use Carno\Coroutine\Context; |
13
|
|
|
use Carno\HTTP\Server\Connection; |
14
|
|
|
use Carno\HTTP\Standard\Response; |
15
|
|
|
use Carno\Tracing\Contracts\Platform; |
16
|
|
|
use Carno\Tracing\Contracts\Vars\EXT; |
17
|
|
|
use Carno\Tracing\Contracts\Vars\FMT; |
18
|
|
|
use Carno\Tracing\Contracts\Vars\TAG; |
19
|
|
|
use Carno\Tracing\Standard\Endpoint; |
20
|
|
|
use Carno\Tracing\Utils\SpansCreator; |
21
|
|
|
use Carno\Tracing\Utils\SpansExporter; |
22
|
|
|
use Throwable; |
23
|
|
|
|
24
|
|
|
class InboundTracing implements Layered |
25
|
|
|
{ |
26
|
|
|
use SpansCreator, SpansExporter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Platform |
30
|
|
|
*/ |
31
|
|
|
private $platform = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* RequestTracing constructor. |
35
|
|
|
* @param Platform $platform |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Platform $platform) |
38
|
|
|
{ |
39
|
|
|
$this->platform = $platform; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Connection $ingress |
44
|
|
|
* @param Context $ctx |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function inbound($ingress, Context $ctx) |
48
|
|
|
{ |
49
|
|
|
$this->newSpan( |
50
|
|
|
$ctx, |
51
|
|
|
$ingress->request()->getUri()->getPath(), |
52
|
|
|
[ |
53
|
|
|
TAG::SPAN_KIND => TAG::SPAN_KIND_RPC_SERVER, |
54
|
|
|
TAG::HTTP_URL => (string) $ingress->request()->getUri(), |
55
|
|
|
TAG::HTTP_METHOD => $ingress->request()->getMethod(), |
56
|
|
|
EXT::LOCAL_ENDPOINT => new Endpoint($ingress->serviced(), $ingress->local()), |
57
|
|
|
EXT::REMOTE_ENDPOINT => new Endpoint($ingress->serviced(), $ingress->remote()), |
58
|
|
|
], |
59
|
|
|
[], |
60
|
|
|
FMT::HTTP_HEADERS, |
61
|
|
|
$ingress->request(), |
62
|
|
|
null, |
63
|
|
|
$this->platform |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return $ingress; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $response |
71
|
|
|
* @param Context $ctx |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function outbound($response, Context $ctx) |
75
|
|
|
{ |
76
|
|
|
$this->closeSpan($ctx, $this->tags($ctx)); |
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Throwable $e |
82
|
|
|
* @param Context $ctx |
83
|
|
|
* @throws Throwable |
84
|
|
|
*/ |
85
|
|
|
public function exception(Throwable $e, Context $ctx) |
86
|
|
|
{ |
87
|
|
|
$this->errorSpan($ctx, $e, $this->tags($ctx)); |
88
|
|
|
throw $e; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Context $ctx |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
private function tags(Context $ctx) : array |
96
|
|
|
{ |
97
|
|
|
/** |
98
|
|
|
* @var Response $response |
99
|
|
|
*/ |
100
|
|
|
|
101
|
|
|
if ($response = $ctx->get(IngressWrapper::RESPONDING)) { |
102
|
|
|
$this->spanToHResponse($ctx, $response); |
103
|
|
|
return [ |
104
|
|
|
TAG::HTTP_STATUS_CODE => $response->getStatusCode(), |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|