summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec')
-rw-r--r--vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/BinaryCodec.php39
-rw-r--r--vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecInterface.php36
-rw-r--r--vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecUtility.php41
-rw-r--r--vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/TextCodec.php184
-rw-r--r--vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/ZipkinCodec.php85
5 files changed, 385 insertions, 0 deletions
diff --git a/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/BinaryCodec.php b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/BinaryCodec.php
new file mode 100644
index 000000000..36c2ffe2c
--- /dev/null
+++ b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/BinaryCodec.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Jaeger\Codec;
+
+use Jaeger\SpanContext;
+use OpenTracing\UnsupportedFormatException;
+
+class BinaryCodec implements CodecInterface
+{
+ /**
+ * {@inheritdoc}
+ *
+ * @see \Jaeger\Tracer::inject
+ *
+ * @param SpanContext $spanContext
+ * @param mixed $carrier
+ *
+ * @return void
+ */
+ public function inject(SpanContext $spanContext, &$carrier)
+ {
+ throw new UnsupportedFormatException('Binary encoding not implemented');
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @see \Jaeger\Tracer::extract
+ *
+ * @param mixed $carrier
+ * @return SpanContext|null
+ *
+ * @throws UnsupportedFormatException
+ */
+ public function extract($carrier)
+ {
+ throw new UnsupportedFormatException('Binary encoding not implemented');
+ }
+}
diff --git a/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecInterface.php b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecInterface.php
new file mode 100644
index 000000000..2aa7d690d
--- /dev/null
+++ b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecInterface.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Jaeger\Codec;
+
+use Jaeger\SpanContext;
+
+interface CodecInterface
+{
+ /**
+ * Handle the logic behind injecting propagation scheme specific information into the carrier
+ * (e.g. http request headers, amqp message headers, etc.).
+ *
+ * This method can modify the carrier.
+ *
+ * @see \Jaeger\Tracer::inject
+ *
+ * @param SpanContext $spanContext
+ * @param mixed $carrier
+ *
+ * @return void
+ */
+ public function inject(SpanContext $spanContext, &$carrier);
+
+ /**
+ * Handle the logic behind extracting propagation-scheme specific information from carrier
+ * (e.g. http request headers, amqp message headers, etc.).
+ *
+ * This method must not modify the carrier.
+ *
+ * @see \Jaeger\Tracer::extract
+ *
+ * @param mixed $carrier
+ * @return SpanContext|null
+ */
+ public function extract($carrier);
+}
diff --git a/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecUtility.php b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecUtility.php
new file mode 100644
index 000000000..eb86a1cde
--- /dev/null
+++ b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/CodecUtility.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Jaeger\Codec;
+
+class CodecUtility
+{
+
+ /**
+ * Incoming trace/span IDs are hex representations of 64-bit values. PHP
+ * represents ints internally as signed 32- or 64-bit values, but base_convert
+ * converts to string representations of arbitrarily large positive numbers.
+ * This means at least half the incoming IDs will be larger than PHP_INT_MAX.
+ *
+ * Thrift, while building a binary representation of the IDs, performs bitwise
+ * operations on the string values, implicitly casting to int and capping them
+ * at PHP_INT_MAX. So, incoming IDs larger than PHP_INT_MAX will be serialized
+ * and sent to the agent as PHP_INT_MAX, breaking trace/span correlation.
+ *
+ * This method therefore, on 64-bit architectures, splits the hex string into
+ * high and low values, converts them separately to ints, and manually combines
+ * them into a proper signed int. This int is then handled properly by the
+ * Thrift package.
+ *
+ * On 32-bit architectures, it falls back to base_convert.
+ *
+ * @param string $hex
+ * @return string|int
+ */
+ public static function hexToInt64($hex)
+ {
+ // If we're on a 32-bit architecture, fall back to base_convert.
+ if (PHP_INT_SIZE === 4) {
+ return base_convert($hex, 16, 10);
+ }
+
+ $hi = intval(substr($hex, -16, -8), 16);
+ $lo = intval(substr($hex, -8, 8), 16);
+
+ return $hi << 32 | $lo;
+ }
+}
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 @@
+<?php
+
+namespace Jaeger\Codec;
+
+use Exception;
+use Jaeger\SpanContext;
+
+use const Jaeger\TRACE_ID_HEADER;
+use const Jaeger\BAGGAGE_HEADER_PREFIX;
+use const Jaeger\DEBUG_ID_HEADER_KEY;
+
+class TextCodec implements CodecInterface
+{
+ private $urlEncoding;
+ private $traceIdHeader;
+ private $baggagePrefix;
+ private $debugIdHeader;
+ private $prefixLength;
+
+ /**
+ * @param bool $urlEncoding
+ * @param string $traceIdHeader
+ * @param string $baggageHeaderPrefix
+ * @param string $debugIdHeader
+ */
+ public function __construct(
+ bool $urlEncoding = false,
+ string $traceIdHeader = TRACE_ID_HEADER,
+ string $baggageHeaderPrefix = BAGGAGE_HEADER_PREFIX,
+ string $debugIdHeader = DEBUG_ID_HEADER_KEY
+ ) {
+ $this->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;
+ }
+}
diff --git a/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/ZipkinCodec.php b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/ZipkinCodec.php
new file mode 100644
index 000000000..1ea5d7c0b
--- /dev/null
+++ b/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Codec/ZipkinCodec.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Jaeger\Codec;
+
+use Jaeger\SpanContext;
+
+use const Jaeger\DEBUG_FLAG;
+use const Jaeger\SAMPLED_FLAG;
+
+class ZipkinCodec implements CodecInterface
+{
+ const SAMPLED_NAME = 'X-B3-Sampled';
+ const TRACE_ID_NAME = 'X-B3-TraceId';
+ const SPAN_ID_NAME = 'X-B3-SpanId';
+ const PARENT_ID_NAME = 'X-B3-ParentSpanId';
+ const FLAGS_NAME = 'X-B3-Flags';
+
+ /**
+ * {@inheritdoc}
+ *
+ * @see \Jaeger\Tracer::inject
+ *
+ * @param SpanContext $spanContext
+ * @param mixed $carrier
+ *
+ * @return void
+ */
+ public function inject(SpanContext $spanContext, &$carrier)
+ {
+ $carrier[self::TRACE_ID_NAME] = dechex($spanContext->getTraceId());
+ $carrier[self::SPAN_ID_NAME] = dechex($spanContext->getSpanId());
+ if ($spanContext->getParentId() != null) {
+ $carrier[self::PARENT_ID_NAME] = dechex($spanContext->getParentId());
+ }
+ $carrier[self::FLAGS_NAME] = (int) $spanContext->getFlags();
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @see \Jaeger\Tracer::extract
+ *
+ * @param mixed $carrier
+ * @return SpanContext|null
+ */
+ public function extract($carrier)
+ {
+ $traceId = "0";
+ $spanId = "0";
+ $parentId = "0";
+ $flags = 0;
+
+ if (isset($carrier[strtolower(self::SAMPLED_NAME)])) {
+ if ($carrier[strtolower(self::SAMPLED_NAME)] === "1" ||
+ strtolower($carrier[strtolower(self::SAMPLED_NAME)] === "true")
+ ) {
+ $flags = $flags | SAMPLED_FLAG;
+ }
+ }
+
+ if (isset($carrier[strtolower(self::TRACE_ID_NAME)])) {
+ $traceId = CodecUtility::hexToInt64($carrier[strtolower(self::TRACE_ID_NAME)], 16, 10);
+ }
+
+ if (isset($carrier[strtolower(self::PARENT_ID_NAME)])) {
+ $parentId = CodecUtility::hexToInt64($carrier[strtolower(self::PARENT_ID_NAME)], 16, 10);
+ }
+
+ if (isset($carrier[strtolower(self::SPAN_ID_NAME)])) {
+ $spanId = CodecUtility::hexToInt64($carrier[strtolower(self::SPAN_ID_NAME)], 16, 10);
+ }
+
+ if (isset($carrier[strtolower(self::FLAGS_NAME)])) {
+ if ($carrier[strtolower(self::FLAGS_NAME)] === "1") {
+ $flags = $flags | DEBUG_FLAG;
+ }
+ }
+
+ if ($traceId != "0" && $spanId != "0") {
+ return new SpanContext($traceId, $spanId, $parentId, $flags);
+ }
+
+ return null;
+ }
+}