Zipkin::serializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Zipkin implementer
4
 * User: moyo
5
 * Date: 23/11/2017
6
 * Time: 4:22 PM
7
 */
8
9
namespace Carno\Traced\Platform;
10
11
use Carno\Config\Config;
12
use Carno\Promise\Promise;
13
use Carno\Promise\Promised;
14
use Carno\Traced\Chips\TransportObserve;
15
use Carno\Traced\Chips\TransportSync;
16
use Carno\Traced\Contracts\Observer;
17
use Carno\Traced\Protocol\AlwaysNone;
18
use Carno\Traced\Protocol\ZipkinJFV2;
19
use Carno\Traced\Transport\Blackhole;
20
use Carno\Traced\Transport\UDPRelays;
21
use Carno\Traced\Transport\ZipkinHAV2;
22
use Carno\Traced\Utils\Environment;
23
use Carno\Tracing\Contracts\Carrier;
24
use Carno\Tracing\Contracts\Env;
25
use Carno\Tracing\Contracts\Platform;
26
use Carno\Tracing\Contracts\Protocol;
27
use Carno\Tracing\Contracts\Transport;
28
use Carno\Tracing\Standard\Carriers\HTTP;
29
use Throwable;
30
31
class Zipkin implements Platform, Observer
32
{
33
    use TransportSync;
34
    use TransportObserve;
35
36
    /**
37
     * @var Environment
38
     */
39
    private $env = null;
40
41
    /**
42
     * @var Config
43
     */
44
    private $conf = null;
45
46
    /**
47
     * @var Carrier
48
     */
49
    private $carrier = null;
50
51
    /**
52
     * @var Protocol
53
     */
54
    private $protocol = null;
55
56
    /**
57
     * @var Transport
58
     */
59
    private $transport = null;
60
61
    /**
62
     * Zipkin constructor.
63
     * @param Config $config
64
     */
65
    public function __construct(Config $config)
66
    {
67
        $this->conf = $config;
68
69
        $this->env = new Environment();
70
        $this->carrier = new HTTP();
71
    }
72
73
    /**
74
     * @return Promised
75
     */
76
    public function init() : Promised
77
    {
78
        $this->syncing(
79
            $this->conf,
80
            'tracing.addr',
81
            [
82
                'zipkin' => [ZipkinHAV2::class, ZipkinJFV2::class],
83
                'udp' => [UDPRelays::class, ZipkinJFV2::class],
84
            ],
85
            function (Transport $transport = null, Protocol $protocol = null) {
86
                $this->transport && $this->transport->disconnect();
87
                $this->transport = $transport;
88
                $this->protocol = $protocol;
89
                $this->changed($transport);
90
            },
91
            static function (Throwable $e) {
92
                logger('traced')->warning('Transport initialize failed', [
93
                    'p' => 'zipkin',
94
                    'ec' => get_class($e),
95
                    'em' => $e->getMessage(),
96
                ]);
97
            }
98
        );
99
100
        return Promise::resolved();
101
    }
102
103
    /**
104
     * @return Environment
105
     */
106
    public function env() : Env
107
    {
108
        return $this->env;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function joined() : bool
115
    {
116
        return $this->carrier && $this->protocol && $this->transport;
117
    }
118
119
    /**
120
     * @return Promised
121
     */
122
    public function leave() : Promised
123
    {
124
        return $this->transport ? $this->transport->disconnect() : Promise::resolved();
125
    }
126
127
    /**
128
     * @return Carrier
129
     */
130
    public function carrier() : Carrier
131
    {
132
        return $this->carrier;
133
    }
134
135
    /**
136
     * @return Protocol
137
     */
138
    public function serializer() : Protocol
139
    {
140
        return $this->protocol ?? new AlwaysNone();
141
    }
142
143
    /**
144
     * @return Transport
145
     */
146
    public function transporter() : Transport
147
    {
148
        return $this->transport ?? new Blackhole();
149
    }
150
}
151