LogicService.php
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace AukeySwrpc;
use AukeySwrpc\Tracer\TracerContext;
use Zipkin\Endpoint;
use Zipkin\Reporters\Http;
use Zipkin\Samplers\BinarySampler;
use Zipkin\TracingBuilder;
/**
* Class LogicService
*
* @package AukeySwrpc
* @author pengjch 2024311 11:10:18
*/
class LogicService
{
protected $params;
protected $module;
protected $tracerUrl;
protected $tracerContext;
protected $clients = [];
/**
* @return static
* @author pengjch 2024311 11:4:5
*/
public static function factory()
{
return new static();
}
/**
* 初始化链路追踪器
*
* @param $func
* @author pengjch 2024311 11:3:36
*/
public function initTracer($func)
{
$reporterUrl = $this->tracerUrl ?: 'http://127.0.0.1:9411/api/v2/spans';
$endpoint = Endpoint::create($this->module);
$reporter = new Http(['endpoint_url' => $reporterUrl]);
$sampler = BinarySampler::createAsAlwaysSample();
$tracing = TracingBuilder::create()
->havingLocalEndpoint($endpoint)
->havingSampler($sampler)
->havingReporter($reporter)
->build();
$tracer = $tracing->getTracer();
$span = $tracer->newTrace();
$span->setName($func);
$span->start();
$span->finish();
$tracer->flush();
$ctx = $span->getContext();
if ($this->tracerContext) {
$this->tracerContext->setTraceID($ctx->getTraceId());
$this->tracerContext->setParentID($ctx->getSpanId());
$this->tracerContext->setReporterUrl($reporterUrl);
} else {
$this->tracerContext = TracerContext::create($ctx->getTraceId(), $ctx->getSpanId(), $reporterUrl);
}
}
/**
* @param $context
* @return $this
* @author pengjch 2024311 11:18:43
*/
public function setTracerContext($context)
{
$this->tracerContext = $context;
return $this;
}
/**
* @param $func
* @return null
* @author pengjch 2024311 11:4:1
*/
public function getTracerContext($func)
{
if (empty($this->tracerUrl)) {
return null;
}
if (empty($this->tracerContext)) {
$this->initTracer($func);
}
return $this->tracerContext;
}
/**
* @param array $params
* @return $this
* @author pengjch 2024311 11:15:47
*/
public function setParams(array $params)
{
$this->params = $params;
return $this;
}
/**
* @param string $url
* @return static $this
* @author pengjch 2024311 11:15:35
*/
public function setTracerUrl(string $url)
{
$this->tracerUrl = $url;
return $this;
}
/**
* @param string $name
* @return $this
* @author pengjch 2024311 11:59:4
*/
public function setModule(string $name)
{
$this->module = $name;
return $this;
}
}