summaryrefslogtreecommitdiff
path: root/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpan.php
blob: db53649bff43f1d2a1acebc5ed3d27758ccda845 (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
<?php

declare(strict_types=1);

namespace OpenTracing\Mock;

use OpenTracing\Span;
use OpenTracing\SpanContext;

final class MockSpan implements Span
{
    /**
     * @var string
     */
    private $operationName;

    /**
     * @var SpanContext
     */
    private $context;

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

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

    /**
     * @var int
     */
    private $startTime;

    /**
     * @var int|null
     */
    private $duration;

    public function __construct(
        string $operationName,
        SpanContext $context,
        ?int $startTime = null
    ) {
        $this->operationName = $operationName;
        $this->context = $context;
        $this->startTime = $startTime ?: time();
    }

    /**
     * {@inheritdoc}
     */
    public function getOperationName(): string
    {
        return $this->operationName;
    }

    /**
     * {@inheritdoc}
     * @return SpanContext|MockSpanContext
     */
    public function getContext(): SpanContext
    {
        return $this->context;
    }

    public function getStartTime(): ?int
    {
        return $this->startTime;
    }

    /**
     * {@inheritdoc}
     */
    public function finish($finishTime = null): void
    {
        $finishTime = ($finishTime ?: time());
        $this->duration = $finishTime - $this->startTime;
    }

    public function isFinished(): bool
    {
        return $this->duration !== null;
    }

    public function getDuration(): ?int
    {
        return $this->duration;
    }

    /**
     * {@inheritdoc}
     */
    public function overwriteOperationName(string $newOperationName): void
    {
        $this->operationName = (string)$newOperationName;
    }

    /**
     * {@inheritdoc}
     */
    public function setTag(string $key, $value): void
    {
        $this->tags[$key] = $value;
    }

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

    /**
     * {@inheritdoc}
     */
    public function log(array $fields = [], $timestamp = null): void
    {
        $this->logs[] = [
            'timestamp' => $timestamp ?: time(),
            'fields' => $fields,
        ];
    }

    public function getLogs(): array
    {
        return $this->logs;
    }

    /**
     * {@inheritdoc}
     */
    public function addBaggageItem(string $key, string $value): void
    {
        $this->context = $this->context->withBaggageItem($key, $value);
    }

    /**
     * {@inheritdoc}
     */
    public function getBaggageItem(string $key): ?string
    {
        return $this->context->getBaggageItem($key);
    }
}