carno-php /
traced
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Zipkin json format v2 |
||
| 4 | * User: moyo |
||
| 5 | * Date: 24/11/2017 |
||
| 6 | * Time: 11:21 AM |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace Carno\Traced\Protocol; |
||
| 10 | |||
| 11 | use Carno\Tracing\Contracts\Protocol; |
||
| 12 | use Carno\Tracing\Contracts\Vars\CTX; |
||
| 13 | use Carno\Tracing\Contracts\Vars\EXT; |
||
| 14 | use Carno\Tracing\Contracts\Vars\TAG; |
||
| 15 | use Carno\Tracing\Standard\Endpoint; |
||
| 16 | use Carno\Tracing\Standard\Span; |
||
| 17 | |||
| 18 | class ZipkinJFV2 implements Protocol |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @param Span $span |
||
| 22 | * @return string |
||
| 23 | */ |
||
| 24 | public function serialize(Span $span) : string |
||
| 25 | { |
||
| 26 | $data = [ |
||
| 27 | 'traceId' => $span->getBaggageItem(CTX::TRACE_ID), |
||
| 28 | 'id' => $span->getBaggageItem(CTX::SPAN_ID), |
||
| 29 | 'name' => $span->getOperationName(), |
||
| 30 | ]; |
||
| 31 | |||
| 32 | $parentID = $span->getBaggageItem(CTX::PARENT_SPAN_ID); |
||
| 33 | if ($parentID) { |
||
| 34 | $data['parentId'] = str_pad($parentID, 16, '0', STR_PAD_LEFT); |
||
| 35 | } |
||
| 36 | |||
| 37 | $data['timestamp'] = $startTime = $span->getStartTime(); |
||
| 38 | $finishTime = $span->getFinishTime(); |
||
| 39 | if ($finishTime) { |
||
| 40 | $data['duration'] = $finishTime - $startTime; |
||
| 41 | } |
||
| 42 | |||
| 43 | $tags = $span->getTags(); |
||
| 44 | |||
| 45 | $this->extractKind($tags, $data); |
||
| 46 | $this->extractEndpoint(EXT::LOCAL_ENDPOINT, 'localEndpoint', $tags, $data); |
||
| 47 | $this->extractEndpoint(EXT::REMOTE_ENDPOINT, 'remoteEndpoint', $tags, $data); |
||
| 48 | |||
| 49 | array_walk($tags, function (string &$v) { |
||
|
0 ignored issues
–
show
|
|||
| 50 | // values type convert .. do nothing |
||
| 51 | }); |
||
| 52 | |||
| 53 | $tags && $data['tags'] = $tags; |
||
| 54 | |||
| 55 | if ($logs = $span->getLogs()) { |
||
| 56 | foreach ($logs as $log) { |
||
| 57 | list($timestamp, $fields) = $log; |
||
| 58 | |||
| 59 | $items = []; |
||
| 60 | array_walk($fields, function ($v, $k) use (&$items) { |
||
| 61 | $items[] = "{$k}={$v}"; |
||
| 62 | }); |
||
| 63 | |||
| 64 | $data['annotations'][] = [ |
||
| 65 | 'timestamp' => $timestamp, |
||
| 66 | 'value' => implode(' ', $items), |
||
| 67 | ]; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $tags |
||
| 76 | * @param array $data |
||
| 77 | */ |
||
| 78 | private function extractKind(array &$tags, array &$data) : void |
||
| 79 | { |
||
| 80 | if (isset($tags[TAG::SPAN_KIND])) { |
||
| 81 | $data['kind'] = strtoupper($tags[TAG::SPAN_KIND]); |
||
| 82 | unset($tags[TAG::SPAN_KIND]); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $from |
||
| 88 | * @param string $to |
||
| 89 | * @param array $tags |
||
| 90 | * @param array $data |
||
| 91 | */ |
||
| 92 | private function extractEndpoint(string $from, string $to, array &$tags, array &$data) : void |
||
| 93 | { |
||
| 94 | if (isset($tags[$from])) { |
||
| 95 | /** |
||
| 96 | * @var Endpoint $ep |
||
| 97 | */ |
||
| 98 | $ep = $tags[$from]; |
||
| 99 | $data[$to] = [ |
||
| 100 | 'serviceName' => $ep->service(), |
||
| 101 | 'ipv4' => $ep->ipv4(), |
||
| 102 | 'port' => $ep->port(), |
||
| 103 | ]; |
||
| 104 | unset($tags[$from]); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.