Passed
Push — master ( 8a631f...a79a09 )
by Zoilo
01:53
created

Error::transactionId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Error;
4
5
use ZoiloMora\ElasticAPM\Events\Common\Context;
6
use ZoiloMora\ElasticAPM\Events\Common\TimestampEpoch;
7
use ZoiloMora\ElasticAPM\Events\TraceableEvent;
8
use ZoiloMora\ElasticAPM\Helper\Encoding;
9
use ZoiloMora\ElasticAPM\Utils\Assert;
10
11
/**
12
 * An error or a logged error message captured by an agent occurring in a monitored service
13
 */
14
final class Error extends TraceableEvent
15
{
16
    use TimestampEpoch;
17
18
    /**
19
     * Hex encoded 64 random bits ID of the correlated transaction.
20
     *
21
     * @var string|null
22
     */
23
    private $transactionId;
24
25
    /**
26
     * @var Context|null
27
     */
28
    private $context;
29
30
    /**
31
     * Function call which was the primary perpetrator of this event.
32
     *
33
     * @var string|null
34
     */
35
    private $culprit;
36
37
    /**
38
     * Information about the originally thrown error.
39
     *
40
     * @var Exception|null
41
     */
42
    private $exception;
43
44
    /**
45
     * @param string $traceId
46
     * @param string $parentId
47
     * @param \Exception $exception
48
     * @param Context $context
49
     * @param string $transactionId
50
     *
51
     * @throws \Exception
52
     */
53 4
    public function __construct(
54
        $traceId,
55
        $parentId,
56
        $exception,
57
        Context $context = null,
58
        $transactionId = null
59
    ) {
60 4
        parent::__construct($traceId, $parentId);
61
62 4
        Assert::throwable($exception);
63
64 4
        $this->timestamp = $this->generateTimestamp();
65 4
        $this->transactionId = $transactionId;
66 4
        $this->context = $context;
67
68 4
        $this->culprit = sprintf(
69 4
            '%s:%d',
70 4
            $exception->getFile(),
71 4
            $exception->getLine()
72 4
        );
73
74 4
        $this->exception = Exception::fromException($exception);
75 4
    }
76
77
    /**
78
     * @return string|null
79
     */
80 1
    public function transactionId()
81
    {
82 1
        return $this->transactionId;
83
    }
84
85
    /**
86
     * @return array
87
     */
88 1
    public function jsonSerialize()
89
    {
90
        return [
91 1
            'error' => array_merge(
92 1
                parent::jsonSerialize(),
93
                [
94 1
                    'timestamp' => $this->timestamp,
95 1
                    'transaction_id' => Encoding::keywordField($this->transactionId),
96 1
                    'context' => $this->context,
97 1
                    'culprit' => Encoding::keywordField($this->culprit),
98 1
                    'exception' => $this->exception,
99
                ]
100 1
            )
101 1
        ];
102
    }
103
}
104