|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Jaeger\Codec; |
| 4 | + |
| 5 | +use Hyperf\Tracer\Support\TextCodecOtel; |
| 6 | +use Exception; |
| 7 | +use Jaeger\SpanContext; |
| 8 | + |
| 9 | +use const Jaeger\TRACE_ID_HEADER; |
| 10 | +use const Jaeger\BAGGAGE_HEADER_PREFIX; |
| 11 | +use const Jaeger\DEBUG_ID_HEADER_KEY; |
| 12 | + |
| 13 | +/** |
| 14 | + * @codeCoverageIgnore |
| 15 | + */ |
| 16 | +class TextCodec implements CodecInterface |
| 17 | +{ |
| 18 | + private $urlEncoding; |
| 19 | + private $traceIdHeader; |
| 20 | + private $baggagePrefix; |
| 21 | + private $debugIdHeader; |
| 22 | + private $prefixLength; |
| 23 | + |
| 24 | + private TextCodecOtel $openTelemetryCodec; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param bool $urlEncoding |
| 28 | + * @param string $traceIdHeader |
| 29 | + * @param string $baggageHeaderPrefix |
| 30 | + * @param string $debugIdHeader |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + bool $urlEncoding = false, |
| 34 | + string $traceIdHeader = TRACE_ID_HEADER, |
| 35 | + string $baggageHeaderPrefix = BAGGAGE_HEADER_PREFIX, |
| 36 | + string $debugIdHeader = DEBUG_ID_HEADER_KEY |
| 37 | + ) |
| 38 | + { |
| 39 | + $this->urlEncoding = $urlEncoding; |
| 40 | + $this->traceIdHeader = str_replace('_', '-', strtolower($traceIdHeader)); |
| 41 | + $this->baggagePrefix = str_replace('_', '-', strtolower($baggageHeaderPrefix)); |
| 42 | + $this->debugIdHeader = str_replace('_', '-', strtolower($debugIdHeader)); |
| 43 | + $this->prefixLength = strlen($baggageHeaderPrefix); |
| 44 | + $this->openTelemetryCodec = new TextCodecOtel(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + * |
| 50 | + * @param SpanContext $spanContext |
| 51 | + * @param mixed $carrier |
| 52 | + * |
| 53 | + * @return void |
| 54 | + * @see \Jaeger\Tracer::inject |
| 55 | + * |
| 56 | + */ |
| 57 | + public function inject(SpanContext $spanContext, &$carrier) |
| 58 | + { |
| 59 | + $this->openTelemetryCodec->inject($spanContext, $carrier); |
| 60 | + |
| 61 | + $carrier[$this->traceIdHeader] = $this->spanContextToString( |
| 62 | + $spanContext->getTraceId(), |
| 63 | + $spanContext->getSpanId(), |
| 64 | + $spanContext->getParentId(), |
| 65 | + $spanContext->getFlags() |
| 66 | + ); |
| 67 | + |
| 68 | + $baggage = $spanContext->getBaggage(); |
| 69 | + if (empty($baggage)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($baggage as $key => $value) { |
| 74 | + $encodedValue = $value; |
| 75 | + |
| 76 | + if ($this->urlEncoding) { |
| 77 | + $encodedValue = urlencode($value); |
| 78 | + } |
| 79 | + |
| 80 | + $carrier[$this->baggagePrefix . $key] = $encodedValue; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * {@inheritdoc} |
| 86 | + * |
| 87 | + * @param mixed $carrier |
| 88 | + * @return SpanContext|null |
| 89 | + * |
| 90 | + * @throws Exception |
| 91 | + * @see \Jaeger\Tracer::extract |
| 92 | + * |
| 93 | + */ |
| 94 | + public function extract($carrier) |
| 95 | + { |
| 96 | + $spanContext = $this->openTelemetryCodec->extract($carrier); |
| 97 | + if ($spanContext !== null) { |
| 98 | + return $spanContext; |
| 99 | + } |
| 100 | + |
| 101 | + $traceId = null; |
| 102 | + $spanId = null; |
| 103 | + $parentId = null; |
| 104 | + $flags = null; |
| 105 | + $baggage = null; |
| 106 | + $debugId = null; |
| 107 | + |
| 108 | + foreach ((array)$carrier as $key => $value) { |
| 109 | + $ucKey = strtolower($key); |
| 110 | + |
| 111 | + if ($ucKey === $this->traceIdHeader) { |
| 112 | + if ($this->urlEncoding) { |
| 113 | + $value = urldecode($value); |
| 114 | + } |
| 115 | + [$traceId, $spanId, $parentId, $flags] = |
| 116 | + $this->spanContextFromString($value); |
| 117 | + } elseif ($this->startsWith($ucKey, $this->baggagePrefix)) { |
| 118 | + if ($this->urlEncoding) { |
| 119 | + $value = urldecode($value); |
| 120 | + } |
| 121 | + $attrKey = substr($key, $this->prefixLength); |
| 122 | + if ($baggage === null) { |
| 123 | + $baggage = [strtolower($attrKey) => $value]; |
| 124 | + } else { |
| 125 | + $baggage[strtolower($attrKey)] = $value; |
| 126 | + } |
| 127 | + } elseif ($ucKey === $this->debugIdHeader) { |
| 128 | + if ($this->urlEncoding) { |
| 129 | + $value = urldecode($value); |
| 130 | + } |
| 131 | + $debugId = $value; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if ($traceId === null && $baggage !== null) { |
| 136 | + throw new Exception('baggage without trace ctx'); |
| 137 | + } |
| 138 | + |
| 139 | + if ($traceId === null) { |
| 140 | + if ($debugId !== null) { |
| 141 | + return new SpanContext(null, null, null, null, [], $debugId); |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + return new SpanContext($traceId, $spanId, $parentId, $flags, $baggage); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Store a span context to a string. |
| 151 | + * |
| 152 | + * @param int $traceId |
| 153 | + * @param int $spanId |
| 154 | + * @param int $parentId |
| 155 | + * @param int $flags |
| 156 | + * @return string |
| 157 | + */ |
| 158 | + private function spanContextToString($traceId, $spanId, $parentId, $flags) |
| 159 | + { |
| 160 | + $parentId = $parentId ?? 0; |
| 161 | + if (is_int($traceId)) { |
| 162 | + $traceId = sprintf('%016x', $traceId); |
| 163 | + } |
| 164 | + return sprintf('%s:%x:%x:%x', $traceId, $spanId, $parentId, $flags); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Create a span context from a string. |
| 169 | + * |
| 170 | + * @param string $value |
| 171 | + * @return array |
| 172 | + * |
| 173 | + * @throws Exception |
| 174 | + */ |
| 175 | + private function spanContextFromString($value): array |
| 176 | + { |
| 177 | + $parts = explode(':', $value); |
| 178 | + |
| 179 | + if (count($parts) != 4) { |
| 180 | + throw new Exception('Malformed tracer state string.'); |
| 181 | + } |
| 182 | + |
| 183 | + return [ |
| 184 | + $parts[0], |
| 185 | + CodecUtility::hexToInt64($parts[1]), |
| 186 | + CodecUtility::hexToInt64($parts[2]), |
| 187 | + $parts[3], |
| 188 | + ]; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Checks that a string ($haystack) starts with a given prefix ($needle). |
| 193 | + * |
| 194 | + * @param string $haystack |
| 195 | + * @param string $needle |
| 196 | + * @return bool |
| 197 | + */ |
| 198 | + private function startsWith(string $haystack, string $needle): bool |
| 199 | + { |
| 200 | + return substr($haystack, 0, strlen($needle)) == $needle; |
| 201 | + } |
| 202 | +} |
0 commit comments