summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/exporter-otlp/ProtobufSerializer.php
blob: c244d0066b02cd2c68b73ec5a0e9d053083b2e26 (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
<?php

declare(strict_types=1);

namespace OpenTelemetry\Contrib\Otlp;

use AssertionError;
use function base64_decode;
use function bin2hex;
use Exception;
use Google\Protobuf\Internal\Message;
use InvalidArgumentException;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use function sprintf;

/**
 * @internal
 *
 * @psalm-type SUPPORTED_CONTENT_TYPES = self::PROTOBUF|self::JSON|self::NDJSON
 */
final class ProtobufSerializer
{
    private const PROTOBUF = 'application/x-protobuf';
    private const JSON = 'application/json';
    private const NDJSON = 'application/x-ndjson';

    private string $contentType;

    private function __construct(string $contentType)
    {
        $this->contentType = $contentType;
    }

    public static function getDefault(): ProtobufSerializer
    {
        return new self(self::PROTOBUF);
    }

    /**
     * @psalm-param TransportInterface<SUPPORTED_CONTENT_TYPES> $transport
     */
    public static function forTransport(TransportInterface $transport): ProtobufSerializer
    {
        switch ($contentType = $transport->contentType()) {
            case self::PROTOBUF:
            case self::JSON:
            case self::NDJSON:
                return new self($contentType);
            default:
                throw new InvalidArgumentException(sprintf('Not supported content type "%s"', $contentType));
        }
    }

    public function serializeTraceId(string $traceId): string
    {
        switch ($this->contentType) {
            case self::PROTOBUF:
                return $traceId;
            case self::JSON:
            case self::NDJSON:
                return base64_decode(bin2hex($traceId));
            default:
                throw new AssertionError();
        }
    }

    public function serializeSpanId(string $spanId): string
    {
        switch ($this->contentType) {
            case self::PROTOBUF:
                return $spanId;
            case self::JSON:
            case self::NDJSON:
                return base64_decode(bin2hex($spanId));
            default:
                throw new AssertionError();
        }
    }

    public function serialize(Message $message): string
    {
        switch ($this->contentType) {
            case self::PROTOBUF:
                return $message->serializeToString();
            case self::JSON:
                //@todo https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#json-protobuf-encoding
                return $message->serializeToJsonString();
            case self::NDJSON:
                return $message->serializeToJsonString() . "\n";
            default:
                throw new AssertionError();
        }
    }

    /**
     * @throws Exception
     */
    public function hydrate(Message $message, string $payload): void
    {
        switch ($this->contentType) {
            case self::PROTOBUF:
                $message->mergeFromString($payload);

                break;
            case self::JSON:
            case self::NDJSON:
                // @phan-suppress-next-line PhanParamTooManyInternal
                $message->mergeFromJsonString($payload, true);

                break;
            default:
                throw new AssertionError();
        }
    }
}