Span::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
ccs 14
cts 14
cp 1
crap 1
rs 9.7998
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Span;
4
5
use ZoiloMora\ElasticAPM\Events\Common\SpanSubtype;
6
use ZoiloMora\ElasticAPM\Events\Common\SpanType;
7
use ZoiloMora\ElasticAPM\Events\Common\TimestampEpoch;
8
use ZoiloMora\ElasticAPM\Events\Duration;
9
use ZoiloMora\ElasticAPM\Events\TraceableEvent;
10
use ZoiloMora\ElasticAPM\Helper\Encoding;
11
use ZoiloMora\ElasticAPM\Helper\Stopwatch\Stopwatch;
12
13
/**
14
 * An event captured by an agent occurring in a monitored service
15
 */
16
final class Span extends TraceableEvent
17
{
18
    use TimestampEpoch;
19
    use SpanType;
20
    use SpanSubtype;
21
    use Duration;
22
23
    /**
24
     * Hex encoded 64 random bits ID of the correlated transaction.
25
     *
26
     * @var string|null
27
     */
28
    private $transactionId;
29
30
    /**
31
     * List of successor transactions and/or spans.
32
     *
33
     * @var array|null
34
     */
35
    private $childIds;
36
37
    /**
38
     * Offset relative to the transaction's timestamp identifying the start of the span, in milliseconds
39
     *
40
     * @var double|null
41
     */
42
    private $start;
43
44
    /**
45
     * The specific kind of event within the sub-type represented by the span (e.g. query, connect)
46
     *
47
     * @var string|null
48
     */
49
    private $action;
50
51
    /**
52
     * @var Context|null
53
     */
54
    private $context;
55
56
    /**
57
     * Generic designation of a span in the scope of a transaction
58
     *
59
     * @var string
60
     */
61
    private $name;
62
63
    /**
64
     * List of stack frames with variable attributes (eg: lineno, filename, etc)
65
     *
66
     * @var array|null
67
     */
68
    private $stacktrace;
69
70
    /**
71
     * @param string $name
72
     * @param string $type
73
     * @param string $traceId
74
     * @param string $parentId
75
     * @param string|null $subtype
76
     * @param string|null $transactionId
77
     * @param string|null $action
78
     * @param Context|null $context
79
     * @param array|null $stacktrace
80
     *
81
     * @throws \Exception
82
     */
83 8
    public function __construct(
84
        $name,
85
        $type,
86
        $traceId,
87
        $parentId,
88
        $subtype = null,
89
        $transactionId = null,
90
        $action = null,
91
        Context $context = null,
92
        $stacktrace = null
93
    ) {
94 8
        parent::__construct($traceId, $parentId);
95
96 8
        $this->timestamp = $this->generateTimestamp();
97 8
        $this->type = $type;
98 8
        $this->subtype = $subtype;
99 8
        $this->transactionId = $transactionId;
100 8
        $this->childIds = null;
101 8
        $this->action = $action;
102 8
        $this->context = $context;
103 8
        $this->name = $name;
104 8
        $this->stacktrace = $stacktrace;
105
106 8
        $this->stopwatch = new Stopwatch();
107 8
        $this->stopwatch->start($this->timestamp);
108 8
    }
109
110
    /**
111
     * @return string|null
112
     */
113 1
    public function transactionId()
114
    {
115 1
        return $this->transactionId;
116
    }
117
118
    /**
119
     * @return void
120
     *
121
     * @throws \ZoiloMora\ElasticAPM\Helper\Stopwatch\Exception\NotStartedException
122
     * @throws \ZoiloMora\ElasticAPM\Helper\Stopwatch\Exception\NotStoppedException
123
     */
124 2
    public function stop()
125
    {
126 2
        $this->stopClock();
127 2
    }
128
129
    /**
130
     * @return Context|null
131
     */
132 1
    public function context()
133
    {
134 1
        return $this->context;
135
    }
136
137
    /**
138
     * @return array
139
     */
140 1
    public function jsonSerialize()
141
    {
142
        return [
143 1
            'span' => array_merge(
144 1
                parent::jsonSerialize(),
145
                [
146 1
                    'timestamp' => $this->timestamp,
147 1
                    'type' => $this->getEncodingType(),
148 1
                    'subtype' => $this->getEncodingSubtype(),
149 1
                    'transaction_id' => Encoding::keywordField($this->transactionId),
150 1
                    'child_ids' => $this->childIds,
151 1
                    'start' => $this->start,
152 1
                    'action' => Encoding::keywordField($this->action),
153 1
                    'context' => $this->context,
154 1
                    'duration' => $this->duration,
155 1
                    'name' => Encoding::keywordField($this->name),
156 1
                    'stacktrace' => $this->stacktrace,
157
                ]
158 1
            )
159 1
        ];
160
    }
161
}
162