summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Mapper/SpanToJaegerMapper.php
blob: da5871904aa66bc549fd5f7cf2960c4a6cc0dd0d (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php

namespace Jaeger\Mapper;

use Jaeger\Span;
use Jaeger\Thrift\Agent\Zipkin\AnnotationType;
use Jaeger\Thrift\Agent\Zipkin\BinaryAnnotation;
use Jaeger\Thrift\Log;
use Jaeger\Thrift\Span as JaegerThriftSpan;
use Jaeger\Thrift\Tag;
use Jaeger\Thrift\TagType;
use const OpenTracing\Tags\COMPONENT;
use const OpenTracing\Tags\PEER_HOST_IPV4;
use const OpenTracing\Tags\PEER_PORT;
use const OpenTracing\Tags\PEER_SERVICE;
use const OpenTracing\Tags\SPAN_KIND;

class SpanToJaegerMapper
{
    private $specialSpanTags = ["jaeger.hostname", "jaeger.version"];

    private $processTagsPrefix = "process.";

    /**
     * @return string[]
     */
    public function getSpecialSpanTags(): array
    {
        return $this->specialSpanTags;
    }

    /**
     * @return string
     */
    public function getProcessTagsPrefix(): string
    {
        return $this->processTagsPrefix;
    }

    public function mapSpanToJaeger(Span $span) : JaegerThriftSpan
    {
        $timestamp = $span->getStartTime();
        $duration = $span->getEndTime() - $span->getStartTime();

        /** @var Tag[] $tags */
        $tags = [];

        $tags[] = new Tag([
            "key" => COMPONENT,
            "vType" => TagType::STRING,
            "vStr" => $span->getComponent() ?? $span->getTracer()->getServiceName(),
        ]);

        // Handle special tags
        $peerService = $span->peer['service_name'] ?? null;
        if ($peerService !== null) {
            $tags[] = new Tag([
                "key" => PEER_SERVICE,
                "vType" => TagType::STRING,
                "vStr" => $peerService,
            ]);
        }

        $peerHostIpv4 = $span->peer['ipv4'] ?? null;
        if ($peerHostIpv4 !== null) {
            $tags[] = new Tag([
                "key" => PEER_HOST_IPV4,
                "vType" => TagType::STRING,
                "vStr" => $peerHostIpv4,
            ]);
        }

        $peerPort = $span->peer['port'] ?? null;
        if ($peerPort !== null) {
            $tags[] = new Tag([
                "key" => PEER_PORT,
                "vType" => TagType::LONG,
                "vLong" => $peerPort,
            ]);
        }

        $spanKind = $span->getKind();
        if ($spanKind !== null) {
            $tags[] = new Tag([
                "key" => SPAN_KIND,
                "vType" => TagType::STRING,
                "vStr" => $spanKind,
            ]);
        }

        /** @var BinaryAnnotation[] $binaryAnnotationTags */
        $binaryAnnotationTags = $span->getTags();
        foreach ($binaryAnnotationTags as $binaryAnnotationTag) {
            if (in_array($binaryAnnotationTag->key, $this->specialSpanTags, true)) {
                continue ;
            }

            if (strpos($binaryAnnotationTag->key, $this->processTagsPrefix) === 0) {
                continue;
            }

            $type = "";
            $vkey = "";
            switch ($binaryAnnotationTag->annotation_type) {
                case AnnotationType::BOOL:
                    $type = TagType::BOOL;
                    $vkey = "vBool";
                    break;
                case AnnotationType::BYTES:
                    $type = TagType::BINARY;
                    $vkey = "vBinary";
                    break;
                case AnnotationType::DOUBLE:
                    $type = TagType::DOUBLE;
                    $vkey = "vDouble";
                    break;
                case AnnotationType::I16:
                case AnnotationType::I32:
                case AnnotationType::I64:
                    $type = TagType::LONG;
                    $vkey = "vLong";
                    break;
                default:
                    $type = TagType::STRING;
                    $vkey = "vStr";
            }

            $tags[] = new Tag([
                "key" => $binaryAnnotationTag->key,
                "vType" => $type,
                $vkey => $binaryAnnotationTag->value,
            ]);
        }

        /** @var Log[] $logs */
        $logs = [];

        $spanLogs = $span->getLogs();

        foreach ($spanLogs as $spanLog) {
            /** @var Tag $fields */
            $fields = [];

            if (!empty($spanLog["fields"])) {
                $fields[] = new Tag([
                    "key" => "event",
                    "vType" => TagType::STRING,
                    "vStr" => json_encode($spanLog["fields"])
                ]);
            }

            $logs[] = new Log([
                "timestamp" => $spanLog["timestamp"],
                "fields" => $fields
            ]);
        }

        return new JaegerThriftSpan([
            "traceIdLow" => (int)$span->getContext()->getTraceId(),
            "traceIdHigh" => 0,
            "spanId" => (int)$span->getContext()->getSpanId(),
            "parentSpanId" => (int)$span->getContext()->getParentId(),
            "operationName" => $span->getOperationName(),
            "startTime" => $timestamp,
            "duration" => $duration,
            "flags" => (int)$span->isDebug(),
            "tags" => $tags,
            "logs" => $logs
        ]);
    }
}