summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Trace/SpanExporter/FriendlySpanConverter.php
blob: 1f8178e10486670ebb3fa951f7092fa527f4f813 (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
172
173
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Trace\SpanExporter;

use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Trace\EventInterface;
use OpenTelemetry\SDK\Trace\LinkInterface;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
use OpenTelemetry\SDK\Trace\SpanDataInterface;
use OpenTelemetry\SDK\Trace\StatusDataInterface;
use ReflectionClass;

class FriendlySpanConverter implements SpanConverterInterface
{
    private const NAME_ATTR = 'name';
    private const CONTEXT_ATTR = 'context';
    private const TRACE_ID_ATTR = 'trace_id';
    private const SPAN_ID_ATTR = 'span_id';
    private const TRACE_STATE_ATTR = 'trace_state';
    private const RESOURCE_ATTR = 'resource';
    private const PARENT_SPAN_ATTR = 'parent_span_id';
    private const KIND_ATTR = 'kind';
    private const START_ATTR = 'start';
    private const END_ATTR = 'end';
    private const ATTRIBUTES_ATTR = 'attributes';
    private const STATUS_ATTR = 'status';
    private const CODE_ATTR = 'code';
    private const DESCRIPTION_ATTR = 'description';
    private const EVENTS_ATTR = 'events';
    private const TIMESTAMP_ATTR = 'timestamp';
    private const LINKS_ATTR = 'links';

    public function convert(iterable $spans): array
    {
        $aggregate = [];
        foreach ($spans as $span) {
            $aggregate[] = $this->convertSpan($span);
        }

        return $aggregate;
    }

    /**
     * friendlySpan does the heavy lifting converting a span into an array
     *
     * @param SpanDataInterface $span
     * @return array
     */
    private function convertSpan(SpanDataInterface $span): array
    {
        return [
            self::NAME_ATTR => $span->getName(),
            self::CONTEXT_ATTR => $this->convertContext($span->getContext()),
            self::RESOURCE_ATTR => $this->convertResource($span->getResource()),
            self::PARENT_SPAN_ATTR => $this->covertParentContext($span->getParentContext()),
            self::KIND_ATTR => $this->convertKind($span->getKind()),
            self::START_ATTR => $span->getStartEpochNanos(),
            self::END_ATTR => $span->getEndEpochNanos(),
            self::ATTRIBUTES_ATTR => $this->convertAttributes($span->getAttributes()),
            self::STATUS_ATTR => $this->covertStatus($span->getStatus()),
            self::EVENTS_ATTR => $this->convertEvents($span->getEvents()),
            self::LINKS_ATTR => $this->convertLinks($span->getLinks()),
        ];
    }

    /**
     * @param SpanContextInterface $context
     * @return array
     */
    private function convertContext(SpanContextInterface $context): array
    {
        return [
            self::TRACE_ID_ATTR => $context->getTraceId(),
            self::SPAN_ID_ATTR => $context->getSpanId(),
            self::TRACE_STATE_ATTR => (string) $context->getTraceState(),
        ];
    }

    /**
     * @param ResourceInfo $resource
     * @return array
     */
    private function convertResource(ResourceInfo $resource): array
    {
        return $resource->getAttributes()->toArray();
    }

    /**
     * @param SpanContextInterface $context
     * @return string
     */
    private function covertParentContext(SpanContextInterface $context): string
    {
        return $context->isValid() ? $context->getSpanId() : '';
    }

    /**
     * Translates SpanKind from its integer representation to a more human friendly string.
     *
     * @param int $kind
     * @return string
     */
    private function convertKind(int $kind): string
    {
        return  array_flip(
            (new ReflectionClass(SpanKind::class))
                ->getConstants()
        )[$kind];
    }

    /**
     * @param \OpenTelemetry\SDK\Common\Attribute\AttributesInterface $attributes
     * @return array
     */
    private function convertAttributes(AttributesInterface $attributes): array
    {
        return $attributes->toArray();
    }

    /**
     * @param StatusDataInterface $status
     * @return array
     */
    private function covertStatus(StatusDataInterface $status): array
    {
        return [
            self::CODE_ATTR => $status->getCode(),
            self::DESCRIPTION_ATTR => $status->getDescription(),
        ];
    }

    /**
     * @param array<EventInterface> $events
     * @return array
     */
    private function convertEvents(array $events): array
    {
        $result = [];

        foreach ($events as $event) {
            $result[] = [
                self::NAME_ATTR => $event->getName(),
                self::TIMESTAMP_ATTR => $event->getEpochNanos(),
                self::ATTRIBUTES_ATTR => $this->convertAttributes($event->getAttributes()),
            ];
        }

        return $result;
    }

    /**
     * @param array<LinkInterface> $links
     * @return array
     */
    private function convertLinks(array $links): array
    {
        $result = [];

        foreach ($links as $link) {
            $result[] = [
                self::CONTEXT_ATTR => $this->convertContext($link->getSpanContext()),
                self::ATTRIBUTES_ATTR => $this->convertAttributes($link->getAttributes()),
            ];
        }

        return $result;
    }
}