BasisExporter::convertEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OpenTelemetry\Exporter;
6
7
use OpenTelemetry\Exporter;
8
use OpenTelemetry\Tracing\Event;
9
use OpenTelemetry\Tracing\Span;
10
use OpenTelemetry\Tracing\Status;
11
use OpenTelemetry\Tracing\SpanContext;
12
use OpenTelemetry\Tracing\Tracer;
13
14
class BasisExporter extends Exporter
15
{
16 1
    public function restoreSpan(array $source): Span
17
    {
18 1
        [$parentSpanId, $traceId, $spanId, $name, $start, $end, $statusCode, $attributes, $events] = $source;
19 1
        $context = SpanContext::restore($traceId, $spanId);
20
21 1
        $parent = null;
22 1
        if ($parentSpanId) {
23 1
            $parent = SpanContext::restore($traceId, $parentSpanId);
24
        }
25
26 1
        $span = (new Span($name ?: 'tracer', $context, $parent));
27
28 1
        if ($attributes) {
29 1
            if (is_object($attributes)) {
30
                $attributes = get_object_vars($attributes);
31
            }
32 1
            $span->setAttributes($attributes);
33 1
        }
34 1
35 1
        if ($events) {
36
            foreach ($events as $info) {
37
                @[$name, $timestamp, $attributes] = $info;
38
                $span->addEvent($name, $attributes ?: [], $timestamp);
39 1
            }
40
        }
41 1
42 1
        $span->setInterval($start, $end);
43
44
        if ($end !== null && !$span->getStatus()) {
45 1
            $span->setStatus(new Status(Status::OK));
46
        }
47
48
        if ($statusCode) {
49
            $span->setStatus(new Status($statusCode));
50 1
        }
51
52
53 1
        return $span;
54
    }
55
56 1
    public function convertSpan(Span $span): array
57 1
    {
58 1
        return [
59 1
            $span->getParentSpanContext() ? $span->getParentSpanContext()->getSpanId() : null,
60 1
            $span->getSpanContext()->getTraceId(),
61 1
            $span->getSpanContext()->getSpanId(),
62 1
            $span->getName(),
63 1
            $span->getStart(),
64 1
            $span->getEnd(),
65
            $span->getStatus() ? $span->getStatus()->getCanonicalCode() : null,
66
            $span->getAttributes() ?: null,
67
            array_map([$this, 'convertEvent'], $span->getEvents()) ?: null
68 1
        ];
69
    }
70
71 1
    public function convertEvent(Event $event): array
72 1
    {
73
        $result = [
74
            $event->getName(),
75 1
            $event->getTimestamp(),
76 1
        ];
77
78
        if (count($event->getAttributes())) {
79 1
            $result[] = $event->getAttributes();
80
        }
81
82
        return $result;
83
    }
84
}
85