From 8f3646a9c93a06f76f6abb31020fdb74b4b1fc59 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 9 Apr 2023 20:50:33 +0300 Subject: exp: jaeger tracing --- .../src/Jaeger/Codec/TextCodec.php | 184 +++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/TextCodec.php (limited to 'vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/TextCodec.php') diff --git a/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/TextCodec.php b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/TextCodec.php new file mode 100644 index 000000000..d437f3812 --- /dev/null +++ b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/TextCodec.php @@ -0,0 +1,184 @@ +urlEncoding = $urlEncoding; + $this->traceIdHeader = str_replace('_', '-', strtolower($traceIdHeader)); + $this->baggagePrefix = str_replace('_', '-', strtolower($baggageHeaderPrefix)); + $this->debugIdHeader = str_replace('_', '-', strtolower($debugIdHeader)); + $this->prefixLength = strlen($baggageHeaderPrefix); + } + + /** + * {@inheritdoc} + * + * @see \Jaeger\Tracer::inject + * + * @param SpanContext $spanContext + * @param mixed $carrier + * + * @return void + */ + public function inject(SpanContext $spanContext, &$carrier) + { + $carrier[$this->traceIdHeader] = $this->spanContextToString( + $spanContext->getTraceId(), + $spanContext->getSpanId(), + $spanContext->getParentId(), + $spanContext->getFlags() + ); + + $baggage = $spanContext->getBaggage(); + if (empty($baggage)) { + return; + } + + foreach ($baggage as $key => $value) { + $encodedValue = $value; + + if ($this->urlEncoding) { + $encodedValue = urlencode($value); + } + + $carrier[$this->baggagePrefix . $key] = $encodedValue; + } + } + + /** + * {@inheritdoc} + * + * @see \Jaeger\Tracer::extract + * + * @param mixed $carrier + * @return SpanContext|null + * + * @throws Exception + */ + public function extract($carrier) + { + $traceId = null; + $spanId = null; + $parentId = null; + $flags = null; + $baggage = null; + $debugId = null; + + foreach ($carrier as $key => $value) { + $ucKey = strtolower($key); + + if ($ucKey === $this->traceIdHeader) { + if ($this->urlEncoding) { + $value = urldecode($value); + } + list($traceId, $spanId, $parentId, $flags) = + $this->spanContextFromString($value); + } elseif ($this->startsWith($ucKey, $this->baggagePrefix)) { + if ($this->urlEncoding) { + $value = urldecode($value); + } + $attrKey = substr($key, $this->prefixLength); + if ($baggage === null) { + $baggage = [strtolower($attrKey) => $value]; + } else { + $baggage[strtolower($attrKey)] = $value; + } + } elseif ($ucKey === $this->debugIdHeader) { + if ($this->urlEncoding) { + $value = urldecode($value); + } + $debugId = $value; + } + } + + if ($traceId === null && $baggage !== null) { + throw new Exception('baggage without trace ctx'); + } + + if ($traceId === null) { + if ($debugId !== null) { + return new SpanContext(null, null, null, null, [], $debugId); + } + return null; + } + + return new SpanContext($traceId, $spanId, $parentId, $flags, $baggage); + } + + /** + * Store a span context to a string. + * + * @param int $traceId + * @param int $spanId + * @param int $parentId + * @param int $flags + * @return string + */ + private function spanContextToString($traceId, $spanId, $parentId, $flags) + { + $parentId = $parentId ?? 0; + return sprintf('%x:%x:%x:%x', $traceId, $spanId, $parentId, $flags); + } + + /** + * Create a span context from a string. + * + * @param string $value + * @return array + * + * @throws Exception + */ + private function spanContextFromString($value): array + { + $parts = explode(':', $value); + + if (count($parts) != 4) { + throw new Exception('Malformed tracer state string.'); + } + + return [ + CodecUtility::hexToInt64($parts[0]), + CodecUtility::hexToInt64($parts[1]), + CodecUtility::hexToInt64($parts[2]), + $parts[3], + ]; + } + + /** + * Checks that a string ($haystack) starts with a given prefix ($needle). + * + * @param string $haystack + * @param string $needle + * @return bool + */ + private function startsWith(string $haystack, string $needle): bool + { + return substr($haystack, 0, strlen($needle)) == $needle; + } +} -- cgit v1.2.3