|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017 OpenCensus Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace OpenTelemetry\Extension\Propagator\XCloudTrace; |
| 19 | + |
| 20 | +use OpenTelemetry\API\Trace\SpanContext; |
| 21 | +use OpenTelemetry\API\Trace\SpanContextInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * This format using a human readable string encoding to propagate SpanContext. |
| 25 | + * The current format of the header is `<trace-id>[/<span-id>][;o=<options>]`. |
| 26 | + * The options are a bitmask of options. Currently the only option is the |
| 27 | + * least significant bit which signals whether the request was traced or not |
| 28 | + * (1 = traced, 0 = not traced). |
| 29 | + */ |
| 30 | +final class XCloudTraceFormatter |
| 31 | +{ |
| 32 | + const CONTEXT_HEADER_FORMAT = '/([0-9a-fA-F]{32})(?:\/(\d+))?(?:;o=(\d+))?/'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Generate a SpanContext object from the Trace Context header |
| 36 | + * |
| 37 | + * @param string $header |
| 38 | + * @return SpanContextInterface |
| 39 | + */ |
| 40 | + public static function deserialize(string $header) : SpanContextInterface |
| 41 | + { |
| 42 | + if (preg_match(self::CONTEXT_HEADER_FORMAT, $header, $matches)) { |
| 43 | + return SpanContext::createFromRemoteParent( |
| 44 | + strtolower($matches[1]), |
| 45 | + array_key_exists(2, $matches) && !empty($matches[2]) |
| 46 | + ? self::decToHex($matches[2]) |
| 47 | + : null, |
| 48 | + array_key_exists(3, $matches) |
| 49 | + ? (int)($matches[3] == '1') |
| 50 | + : null |
| 51 | + ); |
| 52 | + } |
| 53 | + return SpanContext::getInvalid(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Convert a SpanContextInterface to header string |
| 58 | + * |
| 59 | + * @param SpanContextInterface $context |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + public static function serialize(SpanContextInterface $context) : string |
| 63 | + { |
| 64 | + $ret = $context->getTraceId(); |
| 65 | + if ($context->getSpanId()) { |
| 66 | + $ret .= '/' . self::hexToDec($context->getSpanId()); |
| 67 | + } |
| 68 | + $ret .= ';o=' . ($context->isSampled() ? '1' : '0'); |
| 69 | + return $ret; |
| 70 | + } |
| 71 | + |
| 72 | + private static function decToHex(string $num) : string |
| 73 | + { |
| 74 | + $int = (int) $num; |
| 75 | + if (self::isBigNum($int)) { |
| 76 | + $ret = self::baseConvert($num, 10, 16); |
| 77 | + } else { |
| 78 | + $ret = dechex($int); |
| 79 | + } |
| 80 | + return str_pad($ret, 16, '0', STR_PAD_LEFT); |
| 81 | + } |
| 82 | + |
| 83 | + private static function hexToDec(string $num) : string |
| 84 | + { |
| 85 | + $dec = hexdec($num); |
| 86 | + if (self::isBigNum($dec)) { |
| 87 | + return self::baseConvert($num, 16, 10); |
| 88 | + } |
| 89 | + return strval($dec); |
| 90 | + } |
| 91 | + |
| 92 | + private static function isBigNum(int|float $number) : bool |
| 93 | + { |
| 94 | + return $number >= PHP_INT_MAX; |
| 95 | + } |
| 96 | + |
| 97 | + private static function baseConvert(string $num, int $fromBase, int $toBase) : string |
| 98 | + { |
| 99 | + $chars = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 100 | + $newstring = substr($chars, 0, $toBase); |
| 101 | + |
| 102 | + $length = strlen($num); |
| 103 | + $result = ''; |
| 104 | + |
| 105 | + for ($i = 0; $i < $length; $i++) { |
| 106 | + $number[$i] = strpos($chars, $num[$i]); |
| 107 | + } |
| 108 | + |
| 109 | + do { |
| 110 | + $divide = 0; |
| 111 | + $newlen = 0; |
| 112 | + for ($i = 0; $i < $length; $i++) { |
| 113 | + $divide = $divide * $fromBase + $number[$i]; |
| 114 | + if ($divide >= $toBase) { |
| 115 | + $number[$newlen++] = (int)($divide / $toBase); |
| 116 | + $divide = $divide % $toBase; |
| 117 | + } elseif ($newlen > 0) { |
| 118 | + $number[$newlen++] = 0; |
| 119 | + } |
| 120 | + } |
| 121 | + $length = $newlen; |
| 122 | + $result = $newstring[$divide] . $result; |
| 123 | + } while ($newlen != 0); |
| 124 | + |
| 125 | + return $result; |
| 126 | + } |
| 127 | +} |
0 commit comments