summaryrefslogtreecommitdiff
path: root/vendor/opentracing/opentracing/src/OpenTracing/StartSpanOptions.php
blob: 28b73ff0a74ba3442edae39d8b9daf6e82dba49f (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

declare(strict_types=1);

namespace OpenTracing;

use DateTime;
use DateTimeInterface;
use OpenTracing\InvalidReferencesSetException;
use OpenTracing\InvalidSpanOptionException;

final class StartSpanOptions
{
    /**
     * @var Reference[]
     */
    private $references = [];

    /**
     * @var array
     */
    private $tags = [];

    /**
     * @var int|float|DateTimeInterface
     */
    private $startTime;

    /**
     * Only used for spans that are actively managed by scope manager.
     *
     * @var bool
     */
    private $finishSpanOnClose = ScopeManager::DEFAULT_FINISH_SPAN_ON_CLOSE;

    /**
     * @var bool
     */
    private $ignoreActiveSpan = false;

    /**
     * @param array $options
     * @return StartSpanOptions
     * @throws InvalidReferencesSetException when there are inconsistencies about the references
     * @throws InvalidSpanOptionException when one of the options is invalid
     */
    public static function create(array $options): StartSpanOptions
    {
        $spanOptions = new self();

        foreach ($options as $key => $value) {
            switch ($key) {
                case 'child_of':
                    if (!empty($spanOptions->references)) {
                        throw InvalidSpanOptionException::forIncludingBothChildOfAndReferences();
                    }

                    $spanOptions->references[] = self::buildChildOf($value);
                    break;

                case 'references':
                    if (!empty($spanOptions->references)) {
                        throw InvalidSpanOptionException::forIncludingBothChildOfAndReferences();
                    }

                    if ($value instanceof Reference) {
                        $spanOptions->references = [$value];
                    } elseif (is_array($value)) {
                        $spanOptions->references = self::buildReferences($value);
                    } else {
                        throw InvalidSpanOptionException::forInvalidReferenceSet($value);
                    }

                    break;

                case 'tags':
                    if (!is_array($value)) {
                        throw InvalidSpanOptionException::forInvalidTags($value);
                    }

                    foreach ($value as $tag => $tagValue) {
                        if ($tag !== (string)$tag) {
                            throw InvalidSpanOptionException::forInvalidTag($tag);
                        }

                        $spanOptions->tags[$tag] = $tagValue;
                    }
                    break;

                case 'start_time':
                    if (is_scalar($value) && !is_numeric($value)) {
                        throw InvalidSpanOptionException::forInvalidStartTime();
                    }

                    $spanOptions->startTime = $value;
                    break;

                case 'finish_span_on_close':
                    if (!is_bool($value)) {
                        throw InvalidSpanOptionException::forFinishSpanOnClose($value);
                    }

                    $spanOptions->finishSpanOnClose = $value;
                    break;

                case 'ignore_active_span':
                    if (!is_bool($value)) {
                        throw InvalidSpanOptionException::forIgnoreActiveSpan($value);
                    }

                    $spanOptions->ignoreActiveSpan = $value;
                    break;

                default:
                    throw InvalidSpanOptionException::forUnknownOption($key);
            }
        }

        return $spanOptions;
    }

    /**
     * @param Span|SpanContext $parent
     * @return StartSpanOptions
     */
    public function withParent($parent): StartSpanOptions
    {
        $newSpanOptions = new StartSpanOptions();
        $newSpanOptions->references[] = self::buildChildOf($parent);
        $newSpanOptions->tags = $this->tags;
        $newSpanOptions->startTime = $this->startTime;
        $newSpanOptions->finishSpanOnClose = $this->finishSpanOnClose;
        $newSpanOptions->ignoreActiveSpan = $this->ignoreActiveSpan;

        return $newSpanOptions;
    }

    /**
     * @return Reference[]
     */
    public function getReferences(): array
    {
        return $this->references;
    }

    /**
     * @return array
     */
    public function getTags(): array
    {
        return $this->tags;
    }

    /**
     * @return int|float|DateTime|null if returning float or int it should represent
     * the timestamp (including as many decimal places as you need)
     */
    public function getStartTime()
    {
        return $this->startTime;
    }

    /**
     * @return bool
     */
    public function shouldFinishSpanOnClose(): bool
    {
        return $this->finishSpanOnClose;
    }

    /**
     * @return bool
     */
    public function shouldIgnoreActiveSpan(): bool
    {
        return $this->ignoreActiveSpan;
    }

    private static function buildChildOf($value): Reference
    {
        if ($value instanceof Span) {
            return Reference::createForSpan(Reference::CHILD_OF, $value);
        }

        if ($value instanceof SpanContext) {
            return new Reference(Reference::CHILD_OF, $value);
        }

        throw InvalidSpanOptionException::forInvalidChildOf($value);
    }

    private static function buildReferences(array $referencesArray): array
    {
        $references = [];

        foreach ($referencesArray as $reference) {
            if (!($reference instanceof Reference)) {
                throw InvalidSpanOptionException::forInvalidReference($reference);
            }

            $references[] = $reference;
        }

        return $references;
    }
}