Issues (22)

src/Standard/Carriers/HTTP.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * Standard HTTP carrier (zipkin styles)
4
 * User: moyo
5
 * Date: 24/11/2017
6
 * Time: 11:37 AM
7
 */
8
9
namespace Carno\Tracing\Standard\Carriers;
10
11
use Carno\HTTP\Standard\Helper;
0 ignored issues
show
The type Carno\HTTP\Standard\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Carno\Tracing\Chips\StandardCC;
13
use Carno\Tracing\Contracts\Carrier;
14
use Psr\Http\Message\MessageInterface;
0 ignored issues
show
The type Psr\Http\Message\MessageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class HTTP implements Carrier
17
{
18
    use Helper;
19
    use StandardCC;
20
21
    /**
22
     * prefixes
23
     */
24
    private const STATE_PREFIX = 'x-b3-';
25
    private const BAGGAGE_PREFIX = 'ot-baggage-';
26
27
    /**
28
     * extend vars
29
     */
30
    private const EXT_TRACE_ID = 'traceid';
31
    private const EXT_SPAN_ID = 'spanid';
32
    private const EXT_PARENT_SPAN_ID = 'parentspanid';
33
    private const EXT_SAMPLED = 'sampled';
34
35
    /**
36
     * @var MessageInterface
37
     */
38
    private $message = null;
39
40
    /**
41
     * HTTP constructor.
42
     * @param MessageInterface $message
43
     */
44
    public function __construct(MessageInterface $message = null)
45
    {
46
        $this->message = $message;
47
    }
48
49
    /**
50
     * @param MessageInterface $message
51
     * @return Carrier
52
     */
53
    public function http(MessageInterface $message) : Carrier
54
    {
55
        return new static($message);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getTraceID() : string
62
    {
63
        return $this->message->getHeaderLine($this->getStateKey(self::EXT_TRACE_ID));
64
    }
65
66
    /**
67
     * @param string $id
68
     */
69
    public function setTraceID(string $id) : void
70
    {
71
        $this->message->withHeader($this->getStateKey(self::EXT_TRACE_ID), $id);
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getSpanID() : string
78
    {
79
        return $this->message->getHeaderLine($this->getStateKey(self::EXT_SPAN_ID));
80
    }
81
82
    /**
83
     * @param string $id
84
     */
85
    public function setSpanID(string $id) : void
86
    {
87
        $this->message->withHeader($this->getStateKey(self::EXT_SPAN_ID), $id);
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getParentSpanID() : string
94
    {
95
        return $this->message->getHeaderLine($this->getStateKey(self::EXT_PARENT_SPAN_ID));
96
    }
97
98
    /**
99
     * @param string $id
100
     */
101
    public function setParentSpanID(string $id) : void
102
    {
103
        $this->message->withHeader($this->getStateKey(self::EXT_PARENT_SPAN_ID), $id);
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getBaggageItems() : array
110
    {
111
        $items = [];
112
        foreach ($this->getHeaderLines($this->message) as $name => $value) {
113
            if (substr($name, 0, strlen(self::BAGGAGE_PREFIX)) === self::BAGGAGE_PREFIX) {
114
                $items[substr($name, strlen(self::BAGGAGE_PREFIX))] = $value;
115
            }
116
        }
117
        return $items;
118
    }
119
120
    /**
121
     * @param array $items
122
     */
123
    public function setBaggageItems(array $items) : void
124
    {
125
        foreach ($items as $name => $value) {
126
            if (is_scalar($value)) {
127
                $this->message->withHeader(sprintf('%s%s', self::BAGGAGE_PREFIX, $name), $value);
128
            }
129
        }
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function checkIsSampled() : bool
136
    {
137
        return $this->message->getHeaderLine($this->getStateKey(self::EXT_SAMPLED)) === '1';
138
    }
139
140
    /**
141
     * @param bool $sampled
142
     */
143
    public function markIsSampled(bool $sampled) : void
144
    {
145
        $this->message->withHeader($this->getStateKey(self::EXT_SAMPLED), $sampled ? 1 : 0);
146
    }
147
148
    /**
149
     * @param string $ext
150
     * @return string
151
     */
152
    private function getStateKey(string $ext) : string
153
    {
154
        return sprintf('%s%s', self::STATE_PREFIX, $ext);
155
    }
156
}
157