From 6145db13a5065e60f09519dbcd6f5f39c9cf6fd6 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Wed, 3 Oct 2018 19:09:57 +0300 Subject: [PATCH 01/27] fixing hexdec on a 16 char hex number, which could actually exceed INT64 --- src/Jaeger/SpanConverter.php | 41 +++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index cbca29a..e257e18 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -125,15 +125,28 @@ private static function convertTimestamp(\DateTimeInterface $dateTime) return (int)((float) $dateTime->format('U.u') * 1000 * 1000); } + + /** * Split the provided hexId into 2 64-bit integers (16 hex chars each). * Returns array of 2 int values. */ private static function convertTraceId($hexId) { + $method = ''; + switch (true) { + case function_exists('bcadd'): + $method = sprintf('\%s::bc_half_uuid_to_int64s', self::class); + break; + case function_exists('gmp_add'): + $method = sprintf('\%s::gmp_half_uuid_to_int64s', self::class); + break; + default: + throw new \Exception('Please install `php-bc` or `php-gmp` extensions for this to work.'); + } return array_slice( array_map( - 'hexdec', + $method, str_split( substr( str_pad($hexId, 32, "0", STR_PAD_LEFT), @@ -146,4 +159,30 @@ private static function convertTraceId($hexId) 2 ); } + + const MAX_INT_64s = '9223372036854775807'; + + private static function gmp_half_uuid_to_int64s($hex) { + $dec = 0; + $len = strlen($hex); + for ($i = 1; $i <= $len; $i++) { + $dec = gmp_add($dec, gmp_mul(strval(hexdec($hex[$i - 1])), gmp_pow('16', strval($len - $i)))); + } + if (gmp_cmp($dec, self::MAX_INT_64s) > 0) { + $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64s), gmp_add(self::MAX_INT_64s, '1')); + } + return intval($dec); + } + + private static function bc_half_uuid_to_int64s($hex) { + $dec = 0; + $len = strlen($hex); + for ($i = 1; $i <= $len; $i++) { + $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); + } + if (bccomp($dec, self::MAX_INT_64s) > 0) { + $dec = bcsub(bcsub($dec, self::MAX_INT_64s), bcadd(self::MAX_INT_64s, '2')); + } + return intval($dec); + } } From 214572a6e164b848ad8545a983b09c6396daa8f2 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Wed, 3 Oct 2018 19:29:53 +0300 Subject: [PATCH 02/27] Updated method names to camel case --- src/Jaeger/SpanConverter.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index e257e18..aa21b1b 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -136,10 +136,10 @@ private static function convertTraceId($hexId) $method = ''; switch (true) { case function_exists('bcadd'): - $method = sprintf('\%s::bc_half_uuid_to_int64s', self::class); + $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); break; case function_exists('gmp_add'): - $method = sprintf('\%s::gmp_half_uuid_to_int64s', self::class); + $method = sprintf('\%s::gmpHalfUuidToInt64s', self::class); break; default: throw new \Exception('Please install `php-bc` or `php-gmp` extensions for this to work.'); @@ -162,7 +162,7 @@ private static function convertTraceId($hexId) const MAX_INT_64s = '9223372036854775807'; - private static function gmp_half_uuid_to_int64s($hex) { + private static function gmpHalfUuidToInt64s($hex) { $dec = 0; $len = strlen($hex); for ($i = 1; $i <= $len; $i++) { @@ -174,7 +174,7 @@ private static function gmp_half_uuid_to_int64s($hex) { return intval($dec); } - private static function bc_half_uuid_to_int64s($hex) { + private static function bcHalfUuidToInt64s($hex) { $dec = 0; $len = strlen($hex); for ($i = 1; $i <= $len; $i++) { From 9f2fd8ea9f1f78021e9a46a63df308cb597fbe56 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Wed, 3 Oct 2018 19:41:43 +0300 Subject: [PATCH 03/27] Update SpanConverter.php --- src/Jaeger/SpanConverter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index aa21b1b..483f41f 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -160,7 +160,7 @@ private static function convertTraceId($hexId) ); } - const MAX_INT_64s = '9223372036854775807'; + const MAX_INT_64S = '9223372036854775807'; private static function gmpHalfUuidToInt64s($hex) { $dec = 0; @@ -168,8 +168,8 @@ private static function gmpHalfUuidToInt64s($hex) { for ($i = 1; $i <= $len; $i++) { $dec = gmp_add($dec, gmp_mul(strval(hexdec($hex[$i - 1])), gmp_pow('16', strval($len - $i)))); } - if (gmp_cmp($dec, self::MAX_INT_64s) > 0) { - $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64s), gmp_add(self::MAX_INT_64s, '1')); + if (gmp_cmp($dec, self::MAX_INT_64S) > 0) { + $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64S), gmp_add(self::MAX_INT_64S, '1')); } return intval($dec); } @@ -180,8 +180,8 @@ private static function bcHalfUuidToInt64s($hex) { for ($i = 1; $i <= $len; $i++) { $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); } - if (bccomp($dec, self::MAX_INT_64s) > 0) { - $dec = bcsub(bcsub($dec, self::MAX_INT_64s), bcadd(self::MAX_INT_64s, '2')); + if (bccomp($dec, self::MAX_INT_64S) > 0) { + $dec = bcsub(bcsub($dec, self::MAX_INT_64S), bcadd(self::MAX_INT_64S, '2')); } return intval($dec); } From 1c195a49a9bd0a2e18be5b5bab555d454ef7ca77 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Wed, 3 Oct 2018 19:42:58 +0300 Subject: [PATCH 04/27] Update SpanConverter.php --- src/Jaeger/SpanConverter.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 483f41f..7d71197 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -162,7 +162,8 @@ private static function convertTraceId($hexId) const MAX_INT_64S = '9223372036854775807'; - private static function gmpHalfUuidToInt64s($hex) { + private static function gmpHalfUuidToInt64s($hex) + { $dec = 0; $len = strlen($hex); for ($i = 1; $i <= $len; $i++) { @@ -174,7 +175,8 @@ private static function gmpHalfUuidToInt64s($hex) { return intval($dec); } - private static function bcHalfUuidToInt64s($hex) { + private static function bcHalfUuidToInt64s($hex) + { $dec = 0; $len = strlen($hex); for ($i = 1; $i <= $len; $i++) { From dc38d671908c5334f0377f9aae532d971bd7bbf6 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 11:45:02 +0300 Subject: [PATCH 05/27] added pre-install-* script limiter + unit tests for bc and gmp math --- .circleci/config.yml | 3 ++- composer.json | 4 +++ src/Installer.php | 11 ++++++++ src/Jaeger/SpanConverter.php | 2 +- tests/unit/Jaeger/SpanConverterTest.php | 35 ++++++++++++++++++++++++- 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/Installer.php diff --git a/.circleci/config.yml b/.circleci/config.yml index 47b518c..5716d01 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,7 +1,8 @@ unit-config: &unit-config steps: # library requires sockets extension - - run: sudo docker-php-ext-install sockets + - run: sudo apt-get install -y libgmp-dev re2c libmhash-dev libmcrypt-dev file && sudo ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/local/include/ + - run: sudo docker-php-ext-install bcmath gmm sockets - checkout diff --git a/composer.json b/composer.json index 361dbc0..43d6e39 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,9 @@ "psr-4": { "OpenCensus\\Trace\\Exporter\\": "src/" } + }, + "scripts": { + "pre-package-install": "OpenCensus\\Trace\\Exporter\\Installer::checkPhpExtDependency", + "pre-package-update": "OpenCensus\\Trace\\Exporter\\Installer::checkPhpExtDependency" } } diff --git a/src/Installer.php b/src/Installer.php new file mode 100644 index 0000000..e002c00 --- /dev/null +++ b/src/Installer.php @@ -0,0 +1,11 @@ +assertEquals($expectedLow, $span->traceIdLow); } + /** + * @dataProvider traceIdValues + */ + public function testHexdecBigNumbers($traceId, $expectedHigh, $expectedLow) + { + $ids = array_map( + '\OpenCensus\Trace\Exporter\Jaeger\SpanConverter::bcHalfUuidToInt64s', + str_split( + substr( + str_pad($traceId, 32, "0", STR_PAD_LEFT), + -32 + ), + 16 + ) + ); + $this->assertEquals($expectedHigh, $ids[0]); + $this->assertEquals($expectedLow, $ids[1]); + $ids = array_map( + '\OpenCensus\Trace\Exporter\Jaeger\SpanConverter::gmpHalfUuidToInt64s', + str_split( + substr( + str_pad($traceId, 32, "0", STR_PAD_LEFT), + -32 + ), + 16 + ) + ); + $this->assertEquals($expectedHigh, $ids[0]); + $this->assertEquals($expectedLow, $ids[1]); + } + public function traceIdValues() { return [ ['aaa', 0, 2730], ['aaa0000000000000bbb', 2730, 3003], - ['10000000000000aaa0000000000000bbb', 2730, 3003] + ['10000000000000aaa0000000000000bbb', 2730, 3003], + ['fd7a7112906349cc80bb3f6c6a385a85', -181708510409307700, -9170666481338787195], + ['5d37220beb8d4310b3e906a94776b893', 6716874803838272272, -5482843747228665709], ]; } } From 69565fc0a80f0d9e1f9f262ba3ba96c8c6d6a0e4 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 11:46:39 +0300 Subject: [PATCH 06/27] fixed typo --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5716d01..79c0610 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ unit-config: &unit-config steps: # library requires sockets extension - run: sudo apt-get install -y libgmp-dev re2c libmhash-dev libmcrypt-dev file && sudo ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/local/include/ - - run: sudo docker-php-ext-install bcmath gmm sockets + - run: sudo docker-php-ext-install bcmath gmp sockets - checkout From 456014ba5a8a4ac9054310b10c7a565242235c1c Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 11:59:01 +0300 Subject: [PATCH 07/27] lint fixes --- src/Installer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Installer.php b/src/Installer.php index e002c00..116147b 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -3,7 +3,8 @@ namespace OpenCensus\Trace\Exporter; class Installer { - public static function checkPhpExtDependency() { + public static function checkPhpExtDependency() + { if (!function_exists('bcadd') && !function_exists('gmp_mul')) { throw new \Exception('`opencensus-php-exporter-jaeger` requires one of the two extensions to be installed: `php-bcmath` or `php-gmp`'); } From aed10eb20a88f38c3b9613f96fc185cc7cdabe9a Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 12:01:41 +0300 Subject: [PATCH 08/27] fixed integration test --- Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index b67fea2..f2a110b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,8 +19,9 @@ WORKDIR /workspace # current user is circleci RUN sudo chown -R $(whoami) /workspace -RUN sudo docker-php-ext-install sockets +RUN sudo apt-get install -y libgmp-dev re2c libmhash-dev libmcrypt-dev file && sudo ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/local/include/ +RUN sudo docker-php-ext-install bcmath gmp sockets RUN composer install -n --prefer-dist -ENTRYPOINT [] \ No newline at end of file +ENTRYPOINT [] From ff90764002f1e669864ea050a332ece57c829afd Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 12:10:32 +0300 Subject: [PATCH 09/27] linter fix --- src/Installer.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Installer.php b/src/Installer.php index 116147b..b38cbcd 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -5,8 +5,9 @@ class Installer { public static function checkPhpExtDependency() { - if (!function_exists('bcadd') && !function_exists('gmp_mul')) { - throw new \Exception('`opencensus-php-exporter-jaeger` requires one of the two extensions to be installed: `php-bcmath` or `php-gmp`'); + if (!function_exists('bcadd') && !function_exists('gmp_add')) { + throw new \Exception('`opencensus-php-exporter-jaeger` requires one of the two extensions to be ' + . 'installed: `php-bcmath` or `php-gmp`'); } } } From a0e17cf64783f5201c78965e3b563d617b17df66 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 12:11:44 +0300 Subject: [PATCH 10/27] linter fix --- src/Installer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Installer.php b/src/Installer.php index b38cbcd..3493ee7 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -2,7 +2,8 @@ namespace OpenCensus\Trace\Exporter; -class Installer { +class Installer +{ public static function checkPhpExtDependency() { if (!function_exists('bcadd') && !function_exists('gmp_add')) { From bad181b0d510d40fcc68c7a524a6c214467b7669 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 12:34:43 +0300 Subject: [PATCH 11/27] better way of handling big math --- composer.json | 3 +-- src/Jaeger/SpanConverter.php | 24 ++++++++++------- tests/unit/Jaeger/SpanConverterTest.php | 36 ++++--------------------- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/composer.json b/composer.json index 43d6e39..be04ea2 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,6 @@ } }, "scripts": { - "pre-package-install": "OpenCensus\\Trace\\Exporter\\Installer::checkPhpExtDependency", - "pre-package-update": "OpenCensus\\Trace\\Exporter\\Installer::checkPhpExtDependency" + "post-dependencies-solving": "OpenCensus\\Trace\\Exporter\\Installer::checkPhpExtDependency" } } diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 6932be1..6d8b1c1 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -125,7 +125,15 @@ private static function convertTimestamp(\DateTimeInterface $dateTime) return (int)((float) $dateTime->format('U.u') * 1000 * 1000); } + const BIG_MATH_BC = 'bc'; + const BIG_MATH_GMP = 'gmp'; + private static $preferredBigMathExt = 'bc'; + + public static function setPreferredBigMathExt($ext = self::BIG_MATH_BC) + { + self::$preferredBigMathExt = $ext; + } /** * Split the provided hexId into 2 64-bit integers (16 hex chars each). @@ -134,15 +142,13 @@ private static function convertTimestamp(\DateTimeInterface $dateTime) private static function convertTraceId($hexId) { $method = ''; - switch (true) { - case function_exists('bcadd'): - $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); - break; - case function_exists('gmp_add'): - $method = sprintf('\%s::gmpHalfUuidToInt64s', self::class); - break; - default: - throw new \Exception('Please install `php-bcmath` or `php-gmp` extensions for this to work.'); + if (self::$preferredBigMathExt === self::BIG_MATH_BC && !function_exists('bcadd')) { + throw new \Exception('For this functionality to work, please install PhP `bcmath` extension, or ' . + 'swtich to `gmp`.'); + } + if (self::$preferredBigMathExt === self::BIG_MATH_GMP && !function_exists('gmp_add')) { + throw new \Exception('For this functionality to work, please install PhP `gmp` extension, or ' . + 'swtich to `bcmath`.'); } return array_slice( array_map( diff --git a/tests/unit/Jaeger/SpanConverterTest.php b/tests/unit/Jaeger/SpanConverterTest.php index ffb928a..cdf29ec 100644 --- a/tests/unit/Jaeger/SpanConverterTest.php +++ b/tests/unit/Jaeger/SpanConverterTest.php @@ -117,40 +117,14 @@ public function testTraceId($traceId, $expectedHigh, $expectedLow) 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); + SpanConverter::setPreferredBigMathExt(SpanConverter::BIG_MATH_BC); + $span = SpanConverter::convertSpan($span->spanData()); + $this->assertEquals($expectedHigh, $span->traceIdHigh); + $this->assertEquals($expectedLow, $span->traceIdLow); + SpanConverter::setPreferredBigMathExt(SpanConverter::BIG_MATH_GMP); $span = SpanConverter::convertSpan($span->spanData()); $this->assertEquals($expectedHigh, $span->traceIdHigh); $this->assertEquals($expectedLow, $span->traceIdLow); - } - - /** - * @dataProvider traceIdValues - */ - public function testHexdecBigNumbers($traceId, $expectedHigh, $expectedLow) - { - $ids = array_map( - '\OpenCensus\Trace\Exporter\Jaeger\SpanConverter::bcHalfUuidToInt64s', - str_split( - substr( - str_pad($traceId, 32, "0", STR_PAD_LEFT), - -32 - ), - 16 - ) - ); - $this->assertEquals($expectedHigh, $ids[0]); - $this->assertEquals($expectedLow, $ids[1]); - $ids = array_map( - '\OpenCensus\Trace\Exporter\Jaeger\SpanConverter::gmpHalfUuidToInt64s', - str_split( - substr( - str_pad($traceId, 32, "0", STR_PAD_LEFT), - -32 - ), - 16 - ) - ); - $this->assertEquals($expectedHigh, $ids[0]); - $this->assertEquals($expectedLow, $ids[1]); } public function traceIdValues() From a998864d8dec4c24402aba3d3d612438ce196b0d Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 12:39:12 +0300 Subject: [PATCH 12/27] brought back missing big math code --- src/Jaeger/SpanConverter.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 6d8b1c1..3b9b07f 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -142,14 +142,21 @@ public static function setPreferredBigMathExt($ext = self::BIG_MATH_BC) private static function convertTraceId($hexId) { $method = ''; - if (self::$preferredBigMathExt === self::BIG_MATH_BC && !function_exists('bcadd')) { + if (self::$preferredBigMathExt === self::BIG_MATH_BC && function_exists('bcadd')) { + $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); + } else { throw new \Exception('For this functionality to work, please install PhP `bcmath` extension, or ' . 'swtich to `gmp`.'); } if (self::$preferredBigMathExt === self::BIG_MATH_GMP && !function_exists('gmp_add')) { + $method = sprintf('\%s::gmpHalfUuidToInt64s', self::class); + } else { throw new \Exception('For this functionality to work, please install PhP `gmp` extension, or ' . 'swtich to `bcmath`.'); } + if (!$method) { + throw new \Exception('No big math `hexdec` method selected. Have you set the proper big math library?'); + } return array_slice( array_map( $method, From 3f991fe7b67be0d0a4c6c84a95f78f59a4efbc40 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 12:40:38 +0300 Subject: [PATCH 13/27] fixed bcmath prezence test --- src/Jaeger/SpanConverter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 3b9b07f..f3dc46a 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -142,7 +142,7 @@ public static function setPreferredBigMathExt($ext = self::BIG_MATH_BC) private static function convertTraceId($hexId) { $method = ''; - if (self::$preferredBigMathExt === self::BIG_MATH_BC && function_exists('bcadd')) { + if (self::$preferredBigMathExt === self::BIG_MATH_BC && !function_exists('bcadd')) { $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); } else { throw new \Exception('For this functionality to work, please install PhP `bcmath` extension, or ' . From 16760e97bc3d4cdce0492db47560b9a4b227bc0d Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 13:09:14 +0300 Subject: [PATCH 14/27] removed un-necessary checks since they were made @ install anyways --- Dockerfile | 2 +- src/Jaeger/SpanConverter.php | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index f2a110b..6eef9e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ WORKDIR /workspace # current user is circleci RUN sudo chown -R $(whoami) /workspace -RUN sudo apt-get install -y libgmp-dev re2c libmhash-dev libmcrypt-dev file && sudo ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/local/include/ +RUN sudo apt-get update && sudo apt-get install -y libgmp-dev re2c libmhash-dev libmcrypt-dev file && sudo ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/local/include/ RUN sudo docker-php-ext-install bcmath gmp sockets RUN composer install -n --prefer-dist diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index f3dc46a..9bb46f4 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -142,20 +142,14 @@ public static function setPreferredBigMathExt($ext = self::BIG_MATH_BC) private static function convertTraceId($hexId) { $method = ''; - if (self::$preferredBigMathExt === self::BIG_MATH_BC && !function_exists('bcadd')) { + if (self::$preferredBigMathExt === self::BIG_MATH_BC) { $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); - } else { - throw new \Exception('For this functionality to work, please install PhP `bcmath` extension, or ' . - 'swtich to `gmp`.'); } - if (self::$preferredBigMathExt === self::BIG_MATH_GMP && !function_exists('gmp_add')) { + if (self::$preferredBigMathExt === self::BIG_MATH_GMP) { $method = sprintf('\%s::gmpHalfUuidToInt64s', self::class); - } else { - throw new \Exception('For this functionality to work, please install PhP `gmp` extension, or ' . - 'swtich to `bcmath`.'); } if (!$method) { - throw new \Exception('No big math `hexdec` method selected. Have you set the proper big math library?'); + throw new \Exception('No big math `hexdec` method selected. Have you set the proper big math extension?'); } return array_slice( array_map( From 36c796da4eb75dc55651ef886a2eb2ba00b1c0c4 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 13:15:19 +0300 Subject: [PATCH 15/27] no cache --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 79c0610..82868b1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -9,9 +9,9 @@ unit-config: &unit-config # Download and cache dependencies - restore_cache: keys: - - v1-dependencies-{{ checksum "composer.json" }} + - v1-dependencies-{{ checksum "composer.json" }}-v1 # fallback to using the latest cache if no exact match is found - - v1-dependencies- + - v1-dependencies-v1- - run: composer install -n --prefer-dist From 61876e2a88419725559b0f7d8c0055cec723e47c Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Fri, 5 Oct 2018 13:25:52 +0300 Subject: [PATCH 16/27] fixed tests --- tests/unit/Jaeger/SpanConverterTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit/Jaeger/SpanConverterTest.php b/tests/unit/Jaeger/SpanConverterTest.php index cdf29ec..e537deb 100644 --- a/tests/unit/Jaeger/SpanConverterTest.php +++ b/tests/unit/Jaeger/SpanConverterTest.php @@ -118,13 +118,13 @@ public function testTraceId($traceId, $expectedHigh, $expectedLow) 'endTime' => new \DateTime() ]); SpanConverter::setPreferredBigMathExt(SpanConverter::BIG_MATH_BC); - $span = SpanConverter::convertSpan($span->spanData()); - $this->assertEquals($expectedHigh, $span->traceIdHigh); - $this->assertEquals($expectedLow, $span->traceIdLow); + $spanData = SpanConverter::convertSpan($span->spanData()); + $this->assertEquals($expectedHigh, $spanData->traceIdHigh); + $this->assertEquals($expectedLow, $spanData->traceIdLow); SpanConverter::setPreferredBigMathExt(SpanConverter::BIG_MATH_GMP); - $span = SpanConverter::convertSpan($span->spanData()); - $this->assertEquals($expectedHigh, $span->traceIdHigh); - $this->assertEquals($expectedLow, $span->traceIdLow); + $spanData = SpanConverter::convertSpan($span->spanData()); + $this->assertEquals($expectedHigh, $spanData->traceIdHigh); + $this->assertEquals($expectedLow, $spanData->traceIdLow); } public function traceIdValues() From abd0193c4f758ce5297737d2ebd9255118327ede Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 12:59:56 +0300 Subject: [PATCH 17/27] converting SpanConverter to interface & implementations --- src/Installer.php | 2 +- src/Jaeger/SpanConverter.php | 88 ++++------- src/Jaeger/SpanConverterBcmath.php | 39 +++++ src/Jaeger/SpanConverterInterface.php | 51 ++++++ src/Jaeger/SpanConverterOld.php | 197 ++++++++++++++++++++++++ src/JaegerExporter.php | 5 +- tests/unit/Jaeger/SpanConverterTest.php | 18 +-- 7 files changed, 327 insertions(+), 73 deletions(-) create mode 100644 src/Jaeger/SpanConverterBcmath.php create mode 100644 src/Jaeger/SpanConverterInterface.php create mode 100644 src/Jaeger/SpanConverterOld.php diff --git a/src/Installer.php b/src/Installer.php index 3493ee7..814b8b2 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -6,7 +6,7 @@ class Installer { public static function checkPhpExtDependency() { - if (!function_exists('bcadd') && !function_exists('gmp_add')) { + if (!extension_loaded('bcmath') && !extension_loaded('gmp')) { throw new \Exception('`opencensus-php-exporter-jaeger` requires one of the two extensions to be ' . 'installed: `php-bcmath` or `php-gmp`'); } diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 9bb46f4..68db6e4 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -29,10 +29,6 @@ use Jaeger\Thrift\Tag; use Jaeger\Thrift\TagType; -/** - * This class handles converting from the OpenCensus data model into its - * Jaeger Thrift representation. - */ class SpanConverter { /** @@ -43,13 +39,13 @@ class SpanConverter * @param SpanData $span The span to convert. * @return Span The Jaeger Thrift Span representation. */ - public static function convertSpan(SpanData $span) + public function convertSpan(SpanData $span) { - $startTime = self::convertTimestamp($span->startTime()); - $endTime = self::convertTimestamp($span->endTime()); - $spanId = hexdec($span->spanId()); - $parentSpanId = hexdec($span->parentSpanId()); - list($highTraceId, $lowTraceId) = self::convertTraceId($span->traceId()); + $startTime = $this->convertTimestamp($span->startTime()); + $endTime = $this->convertTimestamp($span->endTime()); + $spanId = $this->hexdec($span->spanId()); + $parentSpanId = $this->hexdec($span->parentSpanId()); + list($highTraceId, $lowTraceId) = $this->convertTraceId($span->traceId()); return new Span([ 'traceIdLow' => $lowTraceId, @@ -61,15 +57,15 @@ public static function convertSpan(SpanData $span) 'flags' => 0, 'startTime' => $startTime, 'duration' => $endTime - $startTime, - 'tags' => self::convertTags($span->attributes()), - 'logs' => self::convertLogs($span->timeEvents()) + 'tags' => $this->convertTags($span->attributes()), + 'logs' => $this->convertLogs($span->timeEvents()) ]); } /** * Convert an associative array of $key => $value to Jaeger Tags. */ - public static function convertTags(array $attributes) + public function convertTags(array $attributes) { $tags = []; foreach ($attributes as $key => $value) { @@ -82,33 +78,33 @@ public static function convertTags(array $attributes) return $tags; } - private static function convertLogs(array $timeEvents) + protected function convertLogs(array $timeEvents) { return array_map(function (TimeEvent $timeEvent) { if ($timeEvent instanceof Annotation) { - return self::convertAnnotation($timeEvent); + return $this->convertAnnotation($timeEvent); } elseif ($timeEvent instanceof MessageEvent) { - return self::convertMessageEvent($timeEvent); + return $this->convertMessageEvent($timeEvent); } else { } }, $timeEvents); } - private static function convertAnnotation(Annotation $annotation) + protected function convertAnnotation(Annotation $annotation) { return new Log([ - 'timestamp' => self::convertTimestamp($annotation->time()), - 'fields' => self::convertTags($annotation->attributes() + [ + 'timestamp' => $this->convertTimestamp($annotation->time()), + 'fields' => $this->convertTags($annotation->attributes() + [ 'description' => $annotation->description() ]) ]); } - private static function convertMessageEvent(MessageEvent $messageEvent) + protected function convertMessageEvent(MessageEvent $messageEvent) { return new Log([ - 'timestamp' => self::convertTimestamp($messageEvent->time()), - 'fields' => self::convertTags([ + 'timestamp' => $this->convertTimestamp($messageEvent->time()), + 'fields' => $this->convertTags([ 'type' => $messageEvent->type(), 'id' => $messageEvent->id(), 'uncompressedSize' => $messageEvent->uncompressedSize(), @@ -120,40 +116,20 @@ private static function convertMessageEvent(MessageEvent $messageEvent) /** * Return the given timestamp as an int in milliseconds. */ - private static function convertTimestamp(\DateTimeInterface $dateTime) + protected function convertTimestamp(\DateTimeInterface $dateTime) { return (int)((float) $dateTime->format('U.u') * 1000 * 1000); } - const BIG_MATH_BC = 'bc'; - const BIG_MATH_GMP = 'gmp'; - - private static $preferredBigMathExt = 'bc'; - - public static function setPreferredBigMathExt($ext = self::BIG_MATH_BC) - { - self::$preferredBigMathExt = $ext; - } - /** * Split the provided hexId into 2 64-bit integers (16 hex chars each). * Returns array of 2 int values. */ - private static function convertTraceId($hexId) + protected function convertTraceId($hexId) { - $method = ''; - if (self::$preferredBigMathExt === self::BIG_MATH_BC) { - $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); - } - if (self::$preferredBigMathExt === self::BIG_MATH_GMP) { - $method = sprintf('\%s::gmpHalfUuidToInt64s', self::class); - } - if (!$method) { - throw new \Exception('No big math `hexdec` method selected. Have you set the proper big math extension?'); - } return array_slice( array_map( - $method, + [$this, 'hexdec'], str_split( substr( str_pad($hexId, 32, "0", STR_PAD_LEFT), @@ -169,28 +145,18 @@ private static function convertTraceId($hexId) const MAX_INT_64S = '9223372036854775807'; - private static function gmpHalfUuidToInt64s($hex) + /** + * Hexdec convertion method for big data with limitation to PhP's signed INT64, using gmp + */ + protected function hexdec($hex) { $dec = 0; $len = strlen($hex); for ($i = 1; $i <= $len; $i++) { $dec = gmp_add($dec, gmp_mul(strval(hexdec($hex[$i - 1])), gmp_pow('16', strval($len - $i)))); } - if (gmp_cmp($dec, self::MAX_INT_64S) > 0) { - $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64S), gmp_add(self::MAX_INT_64S, '1')); - } - return intval($dec); - } - - private static function bcHalfUuidToInt64s($hex) - { - $dec = 0; - $len = strlen($hex); - for ($i = 1; $i <= $len; $i++) { - $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); - } - if (bccomp($dec, self::MAX_INT_64S) > 0) { - $dec = bcsub(bcsub($dec, self::MAX_INT_64S), bcadd(self::MAX_INT_64S, '2')); + if (gmp_cmp($dec, $this->MAX_INT_64S) > 0) { + $dec = gmp_sub(gmp_and($dec, $this->MAX_INT_64S), gmp_add($this->MAX_INT_64S, '1')); } return intval($dec); } diff --git a/src/Jaeger/SpanConverterBcmath.php b/src/Jaeger/SpanConverterBcmath.php new file mode 100644 index 0000000..4052294 --- /dev/null +++ b/src/Jaeger/SpanConverterBcmath.php @@ -0,0 +1,39 @@ +MAX_INT_64S) > 0) { + $dec = bcsub(bcsub($dec, $this->MAX_INT_64S), bcadd($this->MAX_INT_64S, '2')); + } + return intval($dec); + } +} diff --git a/src/Jaeger/SpanConverterInterface.php b/src/Jaeger/SpanConverterInterface.php new file mode 100644 index 0000000..d31a8db --- /dev/null +++ b/src/Jaeger/SpanConverterInterface.php @@ -0,0 +1,51 @@ + $value to Jaeger Tags. + */ + public function convertTags(array $attributes); +} diff --git a/src/Jaeger/SpanConverterOld.php b/src/Jaeger/SpanConverterOld.php new file mode 100644 index 0000000..9bb46f4 --- /dev/null +++ b/src/Jaeger/SpanConverterOld.php @@ -0,0 +1,197 @@ +startTime()); + $endTime = self::convertTimestamp($span->endTime()); + $spanId = hexdec($span->spanId()); + $parentSpanId = hexdec($span->parentSpanId()); + list($highTraceId, $lowTraceId) = self::convertTraceId($span->traceId()); + + return new Span([ + 'traceIdLow' => $lowTraceId, + 'traceIdHigh' => $highTraceId, + 'spanId' => $spanId, + 'parentSpanId' => $parentSpanId, + 'operationName' => $span->name(), + 'references' => [], // for now, links cannot describe references + 'flags' => 0, + 'startTime' => $startTime, + 'duration' => $endTime - $startTime, + 'tags' => self::convertTags($span->attributes()), + 'logs' => self::convertLogs($span->timeEvents()) + ]); + } + + /** + * Convert an associative array of $key => $value to Jaeger Tags. + */ + public static function convertTags(array $attributes) + { + $tags = []; + foreach ($attributes as $key => $value) { + $tags[] = new Tag([ + 'key' => (string) $key, + 'vType' => TagType::STRING, + 'vStr' => (string) $value + ]); + } + return $tags; + } + + private static function convertLogs(array $timeEvents) + { + return array_map(function (TimeEvent $timeEvent) { + if ($timeEvent instanceof Annotation) { + return self::convertAnnotation($timeEvent); + } elseif ($timeEvent instanceof MessageEvent) { + return self::convertMessageEvent($timeEvent); + } else { + } + }, $timeEvents); + } + + private static function convertAnnotation(Annotation $annotation) + { + return new Log([ + 'timestamp' => self::convertTimestamp($annotation->time()), + 'fields' => self::convertTags($annotation->attributes() + [ + 'description' => $annotation->description() + ]) + ]); + } + + private static function convertMessageEvent(MessageEvent $messageEvent) + { + return new Log([ + 'timestamp' => self::convertTimestamp($messageEvent->time()), + 'fields' => self::convertTags([ + 'type' => $messageEvent->type(), + 'id' => $messageEvent->id(), + 'uncompressedSize' => $messageEvent->uncompressedSize(), + 'compressedSize' => $messageEvent->compressedSize() + ]) + ]); + } + + /** + * Return the given timestamp as an int in milliseconds. + */ + private static function convertTimestamp(\DateTimeInterface $dateTime) + { + return (int)((float) $dateTime->format('U.u') * 1000 * 1000); + } + + const BIG_MATH_BC = 'bc'; + const BIG_MATH_GMP = 'gmp'; + + private static $preferredBigMathExt = 'bc'; + + public static function setPreferredBigMathExt($ext = self::BIG_MATH_BC) + { + self::$preferredBigMathExt = $ext; + } + + /** + * Split the provided hexId into 2 64-bit integers (16 hex chars each). + * Returns array of 2 int values. + */ + private static function convertTraceId($hexId) + { + $method = ''; + if (self::$preferredBigMathExt === self::BIG_MATH_BC) { + $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); + } + if (self::$preferredBigMathExt === self::BIG_MATH_GMP) { + $method = sprintf('\%s::gmpHalfUuidToInt64s', self::class); + } + if (!$method) { + throw new \Exception('No big math `hexdec` method selected. Have you set the proper big math extension?'); + } + return array_slice( + array_map( + $method, + str_split( + substr( + str_pad($hexId, 32, "0", STR_PAD_LEFT), + -32 + ), + 16 + ) + ), + 0, + 2 + ); + } + + const MAX_INT_64S = '9223372036854775807'; + + private static function gmpHalfUuidToInt64s($hex) + { + $dec = 0; + $len = strlen($hex); + for ($i = 1; $i <= $len; $i++) { + $dec = gmp_add($dec, gmp_mul(strval(hexdec($hex[$i - 1])), gmp_pow('16', strval($len - $i)))); + } + if (gmp_cmp($dec, self::MAX_INT_64S) > 0) { + $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64S), gmp_add(self::MAX_INT_64S, '1')); + } + return intval($dec); + } + + private static function bcHalfUuidToInt64s($hex) + { + $dec = 0; + $len = strlen($hex); + for ($i = 1; $i <= $len; $i++) { + $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); + } + if (bccomp($dec, self::MAX_INT_64S) > 0) { + $dec = bcsub(bcsub($dec, self::MAX_INT_64S), bcadd(self::MAX_INT_64S, '2')); + } + return intval($dec); + } +} diff --git a/src/JaegerExporter.php b/src/JaegerExporter.php index a7d45ff..fe530a2 100644 --- a/src/JaegerExporter.php +++ b/src/JaegerExporter.php @@ -76,9 +76,10 @@ public function __construct($serviceName, array $options = []) ]; $this->host = $options['host']; $this->port = (int) $options['port']; + $this->spanConverter = empty($options['spanConverter']) ? new SpanConverter() : $options['spanConverter']; $this->process = new Process([ 'serviceName' => $serviceName, - 'tags' => SpanConverter::convertTags($options['tags']) + 'tags' => $this->spanConverter->convertTags($options['tags']) ]); $this->client = $options['client']; } @@ -98,7 +99,7 @@ public function export(array $spans) $client = $this->client ?: new UDPClient($this->host, $this->port); $batch = new Batch([ 'process' => $this->process, - 'spans' => array_map([SpanConverter::class, 'convertSpan'], $spans) + 'spans' => array_map([$this->spanConverter, 'convertSpan'], $spans) ]); $client->emitBatch($batch); diff --git a/tests/unit/Jaeger/SpanConverterTest.php b/tests/unit/Jaeger/SpanConverterTest.php index e537deb..e729b56 100644 --- a/tests/unit/Jaeger/SpanConverterTest.php +++ b/tests/unit/Jaeger/SpanConverterTest.php @@ -28,6 +28,11 @@ class SpanConverterTest extends TestCase { + public function setUp() + { + $this->spanConverter = new SpanConverter(); + } + public function testFormatsTrace() { $span = new OCSpan([ @@ -37,7 +42,7 @@ public function testFormatsTrace() 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - $span = SpanConverter::convertSpan($span->spanData()); + $span = $this->spanConverter->convertSpan($span->spanData()); $this->assertInstanceOf(Span::class, $span); $this->assertInternalType('string', $span->operationName); $this->assertInternalType('int', $span->traceIdHigh); @@ -66,7 +71,7 @@ public function testTimeEvents() 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - $span = SpanConverter::convertSpan($span->spanData()); + $span = $this->spanConverter->convertSpan($span->spanData()); $this->assertCount(2, $span->logs); $log1 = $span->logs[0]; $this->assertInternalType('int', $log1->timestamp); @@ -99,7 +104,7 @@ public function testAttributes() 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - $span = SpanConverter::convertSpan($span->spanData()); + $span = $this->spanConverter->convertSpan($span->spanData()); $this->assertCount(2, $span->tags); $this->assertEquals('foo', $span->tags[0]->key); $this->assertEquals('bar', $span->tags[0]->vStr); @@ -117,12 +122,7 @@ public function testTraceId($traceId, $expectedHigh, $expectedLow) 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - SpanConverter::setPreferredBigMathExt(SpanConverter::BIG_MATH_BC); - $spanData = SpanConverter::convertSpan($span->spanData()); - $this->assertEquals($expectedHigh, $spanData->traceIdHigh); - $this->assertEquals($expectedLow, $spanData->traceIdLow); - SpanConverter::setPreferredBigMathExt(SpanConverter::BIG_MATH_GMP); - $spanData = SpanConverter::convertSpan($span->spanData()); + $spanData = $this->spanConverter->convertSpan($span->spanData()); $this->assertEquals($expectedHigh, $spanData->traceIdHigh); $this->assertEquals($expectedLow, $spanData->traceIdLow); } From e493b09c304949b06d6cc0b9d0354f376f8ea62f Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 13:05:20 +0300 Subject: [PATCH 18/27] overzealous replace - const --- src/Jaeger/SpanConverter.php | 4 ++-- tests/unit/Jaeger/SpanConverterTest.php | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 68db6e4..c152342 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -155,8 +155,8 @@ protected function hexdec($hex) for ($i = 1; $i <= $len; $i++) { $dec = gmp_add($dec, gmp_mul(strval(hexdec($hex[$i - 1])), gmp_pow('16', strval($len - $i)))); } - if (gmp_cmp($dec, $this->MAX_INT_64S) > 0) { - $dec = gmp_sub(gmp_and($dec, $this->MAX_INT_64S), gmp_add($this->MAX_INT_64S, '1')); + if (gmp_cmp($dec, self::MAX_INT_64S) > 0) { + $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64S), gmp_add(self::MAX_INT_64S, '1')); } return intval($dec); } diff --git a/tests/unit/Jaeger/SpanConverterTest.php b/tests/unit/Jaeger/SpanConverterTest.php index e729b56..7ce968b 100644 --- a/tests/unit/Jaeger/SpanConverterTest.php +++ b/tests/unit/Jaeger/SpanConverterTest.php @@ -30,7 +30,8 @@ class SpanConverterTest extends TestCase { public function setUp() { - $this->spanConverter = new SpanConverter(); + parent::setUp(); + $this->converter = new SpanConverter(); } public function testFormatsTrace() @@ -42,7 +43,7 @@ public function testFormatsTrace() 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - $span = $this->spanConverter->convertSpan($span->spanData()); + $span = $this->converter->convertSpan($span->spanData()); $this->assertInstanceOf(Span::class, $span); $this->assertInternalType('string', $span->operationName); $this->assertInternalType('int', $span->traceIdHigh); @@ -71,7 +72,7 @@ public function testTimeEvents() 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - $span = $this->spanConverter->convertSpan($span->spanData()); + $span = $this->converter->convertSpan($span->spanData()); $this->assertCount(2, $span->logs); $log1 = $span->logs[0]; $this->assertInternalType('int', $log1->timestamp); @@ -104,7 +105,7 @@ public function testAttributes() 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - $span = $this->spanConverter->convertSpan($span->spanData()); + $span = $this->converter->convertSpan($span->spanData()); $this->assertCount(2, $span->tags); $this->assertEquals('foo', $span->tags[0]->key); $this->assertEquals('bar', $span->tags[0]->vStr); @@ -122,7 +123,7 @@ public function testTraceId($traceId, $expectedHigh, $expectedLow) 'startTime' => new \DateTime(), 'endTime' => new \DateTime() ]); - $spanData = $this->spanConverter->convertSpan($span->spanData()); + $spanData = $this->converter->convertSpan($span->spanData()); $this->assertEquals($expectedHigh, $spanData->traceIdHigh); $this->assertEquals($expectedLow, $spanData->traceIdLow); } From dc8cd100654ef5a01e3a2eb0d1ae69b6067b11ce Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 13:07:21 +0300 Subject: [PATCH 19/27] removed old code --- src/Jaeger/SpanConverterOld.php | 197 -------------------------------- 1 file changed, 197 deletions(-) delete mode 100644 src/Jaeger/SpanConverterOld.php diff --git a/src/Jaeger/SpanConverterOld.php b/src/Jaeger/SpanConverterOld.php deleted file mode 100644 index 9bb46f4..0000000 --- a/src/Jaeger/SpanConverterOld.php +++ /dev/null @@ -1,197 +0,0 @@ -startTime()); - $endTime = self::convertTimestamp($span->endTime()); - $spanId = hexdec($span->spanId()); - $parentSpanId = hexdec($span->parentSpanId()); - list($highTraceId, $lowTraceId) = self::convertTraceId($span->traceId()); - - return new Span([ - 'traceIdLow' => $lowTraceId, - 'traceIdHigh' => $highTraceId, - 'spanId' => $spanId, - 'parentSpanId' => $parentSpanId, - 'operationName' => $span->name(), - 'references' => [], // for now, links cannot describe references - 'flags' => 0, - 'startTime' => $startTime, - 'duration' => $endTime - $startTime, - 'tags' => self::convertTags($span->attributes()), - 'logs' => self::convertLogs($span->timeEvents()) - ]); - } - - /** - * Convert an associative array of $key => $value to Jaeger Tags. - */ - public static function convertTags(array $attributes) - { - $tags = []; - foreach ($attributes as $key => $value) { - $tags[] = new Tag([ - 'key' => (string) $key, - 'vType' => TagType::STRING, - 'vStr' => (string) $value - ]); - } - return $tags; - } - - private static function convertLogs(array $timeEvents) - { - return array_map(function (TimeEvent $timeEvent) { - if ($timeEvent instanceof Annotation) { - return self::convertAnnotation($timeEvent); - } elseif ($timeEvent instanceof MessageEvent) { - return self::convertMessageEvent($timeEvent); - } else { - } - }, $timeEvents); - } - - private static function convertAnnotation(Annotation $annotation) - { - return new Log([ - 'timestamp' => self::convertTimestamp($annotation->time()), - 'fields' => self::convertTags($annotation->attributes() + [ - 'description' => $annotation->description() - ]) - ]); - } - - private static function convertMessageEvent(MessageEvent $messageEvent) - { - return new Log([ - 'timestamp' => self::convertTimestamp($messageEvent->time()), - 'fields' => self::convertTags([ - 'type' => $messageEvent->type(), - 'id' => $messageEvent->id(), - 'uncompressedSize' => $messageEvent->uncompressedSize(), - 'compressedSize' => $messageEvent->compressedSize() - ]) - ]); - } - - /** - * Return the given timestamp as an int in milliseconds. - */ - private static function convertTimestamp(\DateTimeInterface $dateTime) - { - return (int)((float) $dateTime->format('U.u') * 1000 * 1000); - } - - const BIG_MATH_BC = 'bc'; - const BIG_MATH_GMP = 'gmp'; - - private static $preferredBigMathExt = 'bc'; - - public static function setPreferredBigMathExt($ext = self::BIG_MATH_BC) - { - self::$preferredBigMathExt = $ext; - } - - /** - * Split the provided hexId into 2 64-bit integers (16 hex chars each). - * Returns array of 2 int values. - */ - private static function convertTraceId($hexId) - { - $method = ''; - if (self::$preferredBigMathExt === self::BIG_MATH_BC) { - $method = sprintf('\%s::bcHalfUuidToInt64s', self::class); - } - if (self::$preferredBigMathExt === self::BIG_MATH_GMP) { - $method = sprintf('\%s::gmpHalfUuidToInt64s', self::class); - } - if (!$method) { - throw new \Exception('No big math `hexdec` method selected. Have you set the proper big math extension?'); - } - return array_slice( - array_map( - $method, - str_split( - substr( - str_pad($hexId, 32, "0", STR_PAD_LEFT), - -32 - ), - 16 - ) - ), - 0, - 2 - ); - } - - const MAX_INT_64S = '9223372036854775807'; - - private static function gmpHalfUuidToInt64s($hex) - { - $dec = 0; - $len = strlen($hex); - for ($i = 1; $i <= $len; $i++) { - $dec = gmp_add($dec, gmp_mul(strval(hexdec($hex[$i - 1])), gmp_pow('16', strval($len - $i)))); - } - if (gmp_cmp($dec, self::MAX_INT_64S) > 0) { - $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64S), gmp_add(self::MAX_INT_64S, '1')); - } - return intval($dec); - } - - private static function bcHalfUuidToInt64s($hex) - { - $dec = 0; - $len = strlen($hex); - for ($i = 1; $i <= $len; $i++) { - $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); - } - if (bccomp($dec, self::MAX_INT_64S) > 0) { - $dec = bcsub(bcsub($dec, self::MAX_INT_64S), bcadd(self::MAX_INT_64S, '2')); - } - return intval($dec); - } -} From 96ab6b064a4f5f306032908cb242347d0c4bf33a Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 13:19:44 +0300 Subject: [PATCH 20/27] additional tests for bcmath --- src/Jaeger/SpanConverter.php | 30 ++++++++- src/Jaeger/SpanConverterBcmath.php | 11 +++- src/Jaeger/SpanConverterInterface.php | 3 +- tests/unit/Jaeger/SpanConverterBcmathTest.php | 29 ++++++++ tests/unit/JaegerExporterBcmathTest.php | 66 +++++++++++++++++++ 5 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 tests/unit/Jaeger/SpanConverterBcmathTest.php create mode 100644 tests/unit/JaegerExporterBcmathTest.php diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index c152342..a4fdea5 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -29,13 +29,15 @@ use Jaeger\Thrift\Tag; use Jaeger\Thrift\TagType; +/** + * This class handles converting from the OpenCensus data model into its + * Jaeger Thrift representation. + */ class SpanConverter { /** * Convert an OpenCensus Span to its Jaeger Thrift representation. * - * @access private - * * @param SpanData $span The span to convert. * @return Span The Jaeger Thrift Span representation. */ @@ -78,6 +80,11 @@ public function convertTags(array $attributes) return $tags; } + /** + * + * @param array $timeEvents + * @return array + */ protected function convertLogs(array $timeEvents) { return array_map(function (TimeEvent $timeEvent) { @@ -90,6 +97,11 @@ protected function convertLogs(array $timeEvents) }, $timeEvents); } + /** + * + * @param Annotation $annotation + * @return Log + */ protected function convertAnnotation(Annotation $annotation) { return new Log([ @@ -100,6 +112,11 @@ protected function convertAnnotation(Annotation $annotation) ]); } + /** + * + * @param MessageEvent $messageEvent + * @return Log + */ protected function convertMessageEvent(MessageEvent $messageEvent) { return new Log([ @@ -124,6 +141,9 @@ protected function convertTimestamp(\DateTimeInterface $dateTime) /** * Split the provided hexId into 2 64-bit integers (16 hex chars each). * Returns array of 2 int values. + * + * @param str $hexId + * @return array */ protected function convertTraceId($hexId) { @@ -146,7 +166,11 @@ protected function convertTraceId($hexId) const MAX_INT_64S = '9223372036854775807'; /** - * Hexdec convertion method for big data with limitation to PhP's signed INT64, using gmp + * Hexdec convertion method for big data with limitation to PhP's signed INT64, using gmp. + * Warning: Method may not work with hex numbers larger than 8 'digits'. + * + * @param str $hex + * @return number */ protected function hexdec($hex) { diff --git a/src/Jaeger/SpanConverterBcmath.php b/src/Jaeger/SpanConverterBcmath.php index 4052294..21242a2 100644 --- a/src/Jaeger/SpanConverterBcmath.php +++ b/src/Jaeger/SpanConverterBcmath.php @@ -18,11 +18,18 @@ namespace OpenCensus\Trace\Exporter\Jaeger; use OpenCensus\Trace\Exporter\Jaeger\SpanConverter; - +/** + * This class handles converting from the OpenCensus data model into its + * Jaeger Thrift representation. + */ class SpanConverterBcmath extends SpanConverter { /** - * Hexdec convertion method for big data with limitation to PhP's signed INT64, using bcmath + * Hexdec convertion method for big data with limitation to PhP's signed INT64, using bcmath. + * Warning: Method may not work with hex numbers larger than 8 'digits'. + * + * @param str $hex + * @return number */ protected function hexdec($hex) { diff --git a/src/Jaeger/SpanConverterInterface.php b/src/Jaeger/SpanConverterInterface.php index d31a8db..63ef2be 100644 --- a/src/Jaeger/SpanConverterInterface.php +++ b/src/Jaeger/SpanConverterInterface.php @@ -36,8 +36,7 @@ interface SpanConverterInterface { /** * Convert an OpenCensus Span to its Jaeger Thrift representation. - * - * @access private + * Warning: Method may not work with hex numbers larger than 8 'digits'. * * @param SpanData $span The span to convert. * @return Span The Jaeger Thrift Span representation. diff --git a/tests/unit/Jaeger/SpanConverterBcmathTest.php b/tests/unit/Jaeger/SpanConverterBcmathTest.php new file mode 100644 index 0000000..4eb5510 --- /dev/null +++ b/tests/unit/Jaeger/SpanConverterBcmathTest.php @@ -0,0 +1,29 @@ +converter = new SpanConverterBcmath(); + } +} diff --git a/tests/unit/JaegerExporterBcmathTest.php b/tests/unit/JaegerExporterBcmathTest.php new file mode 100644 index 0000000..76deee0 --- /dev/null +++ b/tests/unit/JaegerExporterBcmathTest.php @@ -0,0 +1,66 @@ +client = $this->prophesize(AgentIf::class); + } + + public function testFormatsTrace() + { + $this->client->emitBatch( + Argument::any() + )->willReturn(null)->shouldBeCalled(); + $converter = new SpanConverterBcmath(); + $exporter = new JaegerExporter('test-agent', [ + 'client' => $this->client->reveal(), + 'spanConverter' => $converter + ]); + $span = new OCSpan([ + 'name' => 'span-name', + 'traceId' => 'aaa', + 'spanId' => 'bbb', + 'startTime' => new \DateTime(), + 'endTime' => new \DateTime() + ]); + $this->assertTrue($exporter->export([$span->spanData()])); + } + + public function testEmptyTrace() + { + $converter = new SpanConverterBcmath(); + $exporter = new JaegerExporter('test-agent', [ + 'client' => $this->client->reveal(), + 'spanConverter' => $converter + ]); + $this->assertFalse($exporter->export([])); + } +} From 9dddc910c468ce9579bd21d0fe09d8bba6835a64 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 13:23:27 +0300 Subject: [PATCH 21/27] code clean & lint fix --- src/Jaeger/SpanConverterBcmath.php | 1 + src/Jaeger/SpanConverterInterface.php | 7 ------- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Jaeger/SpanConverterBcmath.php b/src/Jaeger/SpanConverterBcmath.php index 21242a2..29d2463 100644 --- a/src/Jaeger/SpanConverterBcmath.php +++ b/src/Jaeger/SpanConverterBcmath.php @@ -18,6 +18,7 @@ namespace OpenCensus\Trace\Exporter\Jaeger; use OpenCensus\Trace\Exporter\Jaeger\SpanConverter; + /** * This class handles converting from the OpenCensus data model into its * Jaeger Thrift representation. diff --git a/src/Jaeger/SpanConverterInterface.php b/src/Jaeger/SpanConverterInterface.php index 63ef2be..5dbf600 100644 --- a/src/Jaeger/SpanConverterInterface.php +++ b/src/Jaeger/SpanConverterInterface.php @@ -18,15 +18,9 @@ namespace OpenCensus\Trace\Exporter\Jaeger; -use OpenCensus\Trace\Annotation; -use OpenCensus\Trace\MessageEvent; use OpenCensus\Trace\SpanData; -use OpenCensus\Trace\TimeEvent; -use Jaeger\Thrift\Log; use Jaeger\Thrift\Span; -use Jaeger\Thrift\Tag; -use Jaeger\Thrift\TagType; /** * This class handles converting from the OpenCensus data model into its @@ -36,7 +30,6 @@ interface SpanConverterInterface { /** * Convert an OpenCensus Span to its Jaeger Thrift representation. - * Warning: Method may not work with hex numbers larger than 8 'digits'. * * @param SpanData $span The span to convert. * @return Span The Jaeger Thrift Span representation. From 0e8fcbf8573000d4599574f02a36ff0956ad82be Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 13:30:35 +0300 Subject: [PATCH 22/27] test fix --- tests/unit/Jaeger/SpanConverterBcmathTest.php | 4 +++- tests/unit/JaegerExporterBcmathTest.php | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/Jaeger/SpanConverterBcmathTest.php b/tests/unit/Jaeger/SpanConverterBcmathTest.php index 4eb5510..9df7c37 100644 --- a/tests/unit/Jaeger/SpanConverterBcmathTest.php +++ b/tests/unit/Jaeger/SpanConverterBcmathTest.php @@ -19,7 +19,9 @@ use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; -class SpanConverterTest extends TestCase +use OpenCensus\Tests\Unit\Trace\Exporter\Jaeger\SpanConverterTest; + +class SpanConverterBcmathTest extends SpanConverterTest { public function setUp() { diff --git a/tests/unit/JaegerExporterBcmathTest.php b/tests/unit/JaegerExporterBcmathTest.php index 76deee0..0355d30 100644 --- a/tests/unit/JaegerExporterBcmathTest.php +++ b/tests/unit/JaegerExporterBcmathTest.php @@ -18,7 +18,6 @@ namespace OpenCensus\Tests\Unit\Trace\Exporter; use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; - use OpenCensus\Tests\Unit\Trace\Exporter\JaegerExporterTest; /** From d3a68548e37f3746996caecdb7f1b80bf3e3aa6c Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 14:04:54 +0300 Subject: [PATCH 23/27] test fixes --- src/Jaeger/SpanConverterBcmath.php | 4 ++-- tests/unit/Jaeger/SpanConverterBcmathTest.php | 4 +++- tests/unit/JaegerExporterBcmathTest.php | 14 +++++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Jaeger/SpanConverterBcmath.php b/src/Jaeger/SpanConverterBcmath.php index 29d2463..536765a 100644 --- a/src/Jaeger/SpanConverterBcmath.php +++ b/src/Jaeger/SpanConverterBcmath.php @@ -39,8 +39,8 @@ protected function hexdec($hex) for ($i = 1; $i <= $len; $i++) { $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); } - if (bccomp($dec, $this->MAX_INT_64S) > 0) { - $dec = bcsub(bcsub($dec, $this->MAX_INT_64S), bcadd($this->MAX_INT_64S, '2')); + if (bccomp($dec, self::MAX_INT_64S) > 0) { + $dec = bcsub(bcsub($dec, self::MAX_INT_64S), bcadd(self::MAX_INT_64S, '2')); } return intval($dec); } diff --git a/tests/unit/Jaeger/SpanConverterBcmathTest.php b/tests/unit/Jaeger/SpanConverterBcmathTest.php index 9df7c37..f2d5c2b 100644 --- a/tests/unit/Jaeger/SpanConverterBcmathTest.php +++ b/tests/unit/Jaeger/SpanConverterBcmathTest.php @@ -17,10 +17,12 @@ namespace OpenCensus\Tests\Unit\Trace\Exporter\Jaeger; -use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; +require_once __DIR__ . '/SpanConverterTest.php'; use OpenCensus\Tests\Unit\Trace\Exporter\Jaeger\SpanConverterTest; +use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; + class SpanConverterBcmathTest extends SpanConverterTest { public function setUp() diff --git a/tests/unit/JaegerExporterBcmathTest.php b/tests/unit/JaegerExporterBcmathTest.php index 0355d30..daec51d 100644 --- a/tests/unit/JaegerExporterBcmathTest.php +++ b/tests/unit/JaegerExporterBcmathTest.php @@ -17,9 +17,21 @@ namespace OpenCensus\Tests\Unit\Trace\Exporter; -use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; +require_once __DIR__ . '/JaegerExporterTest.php'; + use OpenCensus\Tests\Unit\Trace\Exporter\JaegerExporterTest; +use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; + +use OpenCensus\Trace\Exporter\JaegerExporter; +use OpenCensus\Trace\Annotation; +use OpenCensus\Trace\MessageEvent; +use OpenCensus\Trace\Span as OCSpan; +use Prophecy\Argument; +use Jaeger\Thrift\Span; +use Jaeger\Thrift\Agent\AgentIf; +use PHPUnit\Framework\TestCase; + /** * @group trace */ From 49a462bed6393915c199180ce33aeaff31c86761 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 14:51:08 +0300 Subject: [PATCH 24/27] moved hexdec converter methods into their own classes having their own interface --- src/Jaeger/HexdecConverter.php | 46 +++++++++++++++ ...erBcmath.php => HexdecConverterBcMath.php} | 17 +++--- src/Jaeger/HexdecConverterInterface.php | 32 ++++++++++ src/Jaeger/SpanConverter.php | 58 +++++++++---------- tests/unit/Jaeger/SpanConverterBcmathTest.php | 9 ++- tests/unit/JaegerExporterBcmathTest.php | 14 +++-- 6 files changed, 129 insertions(+), 47 deletions(-) create mode 100644 src/Jaeger/HexdecConverter.php rename src/Jaeger/{SpanConverterBcmath.php => HexdecConverterBcMath.php} (78%) create mode 100644 src/Jaeger/HexdecConverterInterface.php diff --git a/src/Jaeger/HexdecConverter.php b/src/Jaeger/HexdecConverter.php new file mode 100644 index 0000000..c9ca22a --- /dev/null +++ b/src/Jaeger/HexdecConverter.php @@ -0,0 +1,46 @@ + 0) { + $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64S), gmp_add(self::MAX_INT_64S, '1')); + } + return intval($dec); + } +} diff --git a/src/Jaeger/SpanConverterBcmath.php b/src/Jaeger/HexdecConverterBcMath.php similarity index 78% rename from src/Jaeger/SpanConverterBcmath.php rename to src/Jaeger/HexdecConverterBcMath.php index 536765a..defa56a 100644 --- a/src/Jaeger/SpanConverterBcmath.php +++ b/src/Jaeger/HexdecConverterBcMath.php @@ -6,7 +6,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -17,22 +17,21 @@ namespace OpenCensus\Trace\Exporter\Jaeger; -use OpenCensus\Trace\Exporter\Jaeger\SpanConverter; +use OpenCensus\Trace\Exporter\Jaeger\HexdecConverterInterface; -/** - * This class handles converting from the OpenCensus data model into its - * Jaeger Thrift representation. - */ -class SpanConverterBcmath extends SpanConverter +class HexdecConverterBcMath implements HexdecConverterInterface { + + const MAX_INT_64S = '9223372036854775807'; + /** - * Hexdec convertion method for big data with limitation to PhP's signed INT64, using bcmath. + * Hexdec convertion method for big data with limitation to PhP's signed INT64, using gmp. * Warning: Method may not work with hex numbers larger than 8 'digits'. * * @param str $hex * @return number */ - protected function hexdec($hex) + public function convert($hex) { $dec = 0; $len = strlen($hex); diff --git a/src/Jaeger/HexdecConverterInterface.php b/src/Jaeger/HexdecConverterInterface.php new file mode 100644 index 0000000..15a0de0 --- /dev/null +++ b/src/Jaeger/HexdecConverterInterface.php @@ -0,0 +1,32 @@ +hexdec = empty($options['hexdecConverter']) ? new HexdecConverter() : $options['hexdecConverter']; + } + /** * Convert an OpenCensus Span to its Jaeger Thrift representation. * @@ -45,8 +64,8 @@ public function convertSpan(SpanData $span) { $startTime = $this->convertTimestamp($span->startTime()); $endTime = $this->convertTimestamp($span->endTime()); - $spanId = $this->hexdec($span->spanId()); - $parentSpanId = $this->hexdec($span->parentSpanId()); + $spanId = $this->hexdec->convert($span->spanId()); + $parentSpanId = $this->hexdec->convert($span->parentSpanId()); list($highTraceId, $lowTraceId) = $this->convertTraceId($span->traceId()); return new Span([ @@ -85,7 +104,7 @@ public function convertTags(array $attributes) * @param array $timeEvents * @return array */ - protected function convertLogs(array $timeEvents) + private function convertLogs(array $timeEvents) { return array_map(function (TimeEvent $timeEvent) { if ($timeEvent instanceof Annotation) { @@ -102,7 +121,7 @@ protected function convertLogs(array $timeEvents) * @param Annotation $annotation * @return Log */ - protected function convertAnnotation(Annotation $annotation) + private function convertAnnotation(Annotation $annotation) { return new Log([ 'timestamp' => $this->convertTimestamp($annotation->time()), @@ -117,7 +136,7 @@ protected function convertAnnotation(Annotation $annotation) * @param MessageEvent $messageEvent * @return Log */ - protected function convertMessageEvent(MessageEvent $messageEvent) + private function convertMessageEvent(MessageEvent $messageEvent) { return new Log([ 'timestamp' => $this->convertTimestamp($messageEvent->time()), @@ -133,7 +152,7 @@ protected function convertMessageEvent(MessageEvent $messageEvent) /** * Return the given timestamp as an int in milliseconds. */ - protected function convertTimestamp(\DateTimeInterface $dateTime) + private function convertTimestamp(\DateTimeInterface $dateTime) { return (int)((float) $dateTime->format('U.u') * 1000 * 1000); } @@ -145,11 +164,11 @@ protected function convertTimestamp(\DateTimeInterface $dateTime) * @param str $hexId * @return array */ - protected function convertTraceId($hexId) + private function convertTraceId($hexId) { return array_slice( array_map( - [$this, 'hexdec'], + [$this->hexdec, 'convert'], str_split( substr( str_pad($hexId, 32, "0", STR_PAD_LEFT), @@ -163,25 +182,4 @@ protected function convertTraceId($hexId) ); } - const MAX_INT_64S = '9223372036854775807'; - - /** - * Hexdec convertion method for big data with limitation to PhP's signed INT64, using gmp. - * Warning: Method may not work with hex numbers larger than 8 'digits'. - * - * @param str $hex - * @return number - */ - protected function hexdec($hex) - { - $dec = 0; - $len = strlen($hex); - for ($i = 1; $i <= $len; $i++) { - $dec = gmp_add($dec, gmp_mul(strval(hexdec($hex[$i - 1])), gmp_pow('16', strval($len - $i)))); - } - if (gmp_cmp($dec, self::MAX_INT_64S) > 0) { - $dec = gmp_sub(gmp_and($dec, self::MAX_INT_64S), gmp_add(self::MAX_INT_64S, '1')); - } - return intval($dec); - } } diff --git a/tests/unit/Jaeger/SpanConverterBcmathTest.php b/tests/unit/Jaeger/SpanConverterBcmathTest.php index f2d5c2b..75e234c 100644 --- a/tests/unit/Jaeger/SpanConverterBcmathTest.php +++ b/tests/unit/Jaeger/SpanConverterBcmathTest.php @@ -21,13 +21,16 @@ use OpenCensus\Tests\Unit\Trace\Exporter\Jaeger\SpanConverterTest; -use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; +use OpenCensus\Trace\Exporter\Jaeger\HexdecConverterBcMath; +use OpenCensus\Trace\Exporter\Jaeger\SpanConverter; -class SpanConverterBcmathTest extends SpanConverterTest +class SpanConverterBcMathTest extends SpanConverterTest { public function setUp() { parent::setUp(); - $this->converter = new SpanConverterBcmath(); + $this->converter = new SpanConverter([ + 'hexdecConverter' => new HexdecConverterBcMath() + ]); } } diff --git a/tests/unit/JaegerExporterBcmathTest.php b/tests/unit/JaegerExporterBcmathTest.php index daec51d..ea4451d 100644 --- a/tests/unit/JaegerExporterBcmathTest.php +++ b/tests/unit/JaegerExporterBcmathTest.php @@ -21,9 +21,9 @@ use OpenCensus\Tests\Unit\Trace\Exporter\JaegerExporterTest; -use OpenCensus\Trace\Exporter\Jaeger\SpanConverterBcmath; - use OpenCensus\Trace\Exporter\JaegerExporter; +use OpenCensus\Trace\Exporter\Jaeger\SpanConverter; +use OpenCensus\Trace\Exporter\Jaeger\HexdecConverterBcMath; use OpenCensus\Trace\Annotation; use OpenCensus\Trace\MessageEvent; use OpenCensus\Trace\Span as OCSpan; @@ -35,7 +35,7 @@ /** * @group trace */ -class JaegerExporterBcmathTest extends JaegerExporterTest +class JaegerExporterBcMathTest extends JaegerExporterTest { private $client; @@ -50,7 +50,9 @@ public function testFormatsTrace() $this->client->emitBatch( Argument::any() )->willReturn(null)->shouldBeCalled(); - $converter = new SpanConverterBcmath(); + $converter = new SpanConverter([ + 'hexdecConverter' => new HexdecConverterBcMath() + ]); $exporter = new JaegerExporter('test-agent', [ 'client' => $this->client->reveal(), 'spanConverter' => $converter @@ -67,7 +69,9 @@ public function testFormatsTrace() public function testEmptyTrace() { - $converter = new SpanConverterBcmath(); + $converter = new SpanConverter([ + 'hexdecConverter' => new HexdecConverterBcMath() + ]); $exporter = new JaegerExporter('test-agent', [ 'client' => $this->client->reveal(), 'spanConverter' => $converter From ae081aa130c2e57a67d11352b14ad13258be0cb0 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Mon, 8 Oct 2018 14:54:01 +0300 Subject: [PATCH 25/27] lint fix --- src/Jaeger/SpanConverter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 12b9413..1de553f 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -181,5 +181,4 @@ private function convertTraceId($hexId) 2 ); } - } From bd8005772b5b943a1a8a464a585f893b16cdb962 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Tue, 9 Oct 2018 10:50:34 +0300 Subject: [PATCH 26/27] comments and interface removal --- src/Jaeger/HexdecConverter.php | 6 +++- src/Jaeger/HexdecConverterBcMath.php | 6 +++- src/Jaeger/HexdecConverterInterface.php | 5 +-- src/Jaeger/SpanConverter.php | 3 +- src/Jaeger/SpanConverterInterface.php | 43 ------------------------- 5 files changed, 14 insertions(+), 49 deletions(-) delete mode 100644 src/Jaeger/SpanConverterInterface.php diff --git a/src/Jaeger/HexdecConverter.php b/src/Jaeger/HexdecConverter.php index c9ca22a..3e2131e 100644 --- a/src/Jaeger/HexdecConverter.php +++ b/src/Jaeger/HexdecConverter.php @@ -19,13 +19,17 @@ use OpenCensus\Trace\Exporter\Jaeger\HexdecConverterInterface; + /** + * Hexdec converter class. Used for converting hex string values into numbers by using + * `gmp` as large numbers library. + */ class HexdecConverter implements HexdecConverterInterface { const MAX_INT_64S = '9223372036854775807'; /** - * Hexdec convertion method for big data with limitation to PhP's signed INT64, using gmp. + * Hexdec convertion method for large numbers with limitation to PhP's signed INT64, using gmp. * Warning: Method may not work with hex numbers larger than 8 'digits'. * * @param str $hex diff --git a/src/Jaeger/HexdecConverterBcMath.php b/src/Jaeger/HexdecConverterBcMath.php index defa56a..a9ab454 100644 --- a/src/Jaeger/HexdecConverterBcMath.php +++ b/src/Jaeger/HexdecConverterBcMath.php @@ -19,13 +19,17 @@ use OpenCensus\Trace\Exporter\Jaeger\HexdecConverterInterface; + /** + * Hexdec converter class. Used for converting hex string values into numbers by using + * `bcmath` as large numbers library. + */ class HexdecConverterBcMath implements HexdecConverterInterface { const MAX_INT_64S = '9223372036854775807'; /** - * Hexdec convertion method for big data with limitation to PhP's signed INT64, using gmp. + * Hexdec convertion method for big data with limitation to PhP's signed INT64, using bcmath. * Warning: Method may not work with hex numbers larger than 8 'digits'. * * @param str $hex diff --git a/src/Jaeger/HexdecConverterInterface.php b/src/Jaeger/HexdecConverterInterface.php index 15a0de0..decccca 100644 --- a/src/Jaeger/HexdecConverterInterface.php +++ b/src/Jaeger/HexdecConverterInterface.php @@ -18,12 +18,13 @@ namespace OpenCensus\Trace\Exporter\Jaeger; /** - * + * Hexdec converter interface. Used for converting hex string values into numbers by using + * large numbers math libraries like Gmp or BCMath. */ interface HexdecConverterInterface { /** - * Hexdec convertion method for big data with limitation to PhP's signed INT64. + * Hexdec convertion method for large numbers with limitation to PhP's signed INT64. * * @param str $hex * @return number diff --git a/src/Jaeger/SpanConverter.php b/src/Jaeger/SpanConverter.php index 1de553f..e3c0e3f 100644 --- a/src/Jaeger/SpanConverter.php +++ b/src/Jaeger/SpanConverter.php @@ -20,7 +20,6 @@ require_once __DIR__ . '/../Thrift/Types.php'; use OpenCensus\Trace\Annotation; -use OpenCensus\Trace\Exporter\Jaeger\SpanConverterInterface; use OpenCensus\Trace\MessageEvent; use OpenCensus\Trace\SpanData; use OpenCensus\Trace\TimeEvent; @@ -34,7 +33,7 @@ * This class handles converting from the OpenCensus data model into its * Jaeger Thrift representation. */ -class SpanConverter implements SpanConverterInterface +class SpanConverter { /** diff --git a/src/Jaeger/SpanConverterInterface.php b/src/Jaeger/SpanConverterInterface.php deleted file mode 100644 index 5dbf600..0000000 --- a/src/Jaeger/SpanConverterInterface.php +++ /dev/null @@ -1,43 +0,0 @@ - $value to Jaeger Tags. - */ - public function convertTags(array $attributes); -} From d38bbc53b93fde514b2387892d5517a6d1502471 Mon Sep 17 00:00:00 2001 From: Dragos Cirjan Date: Tue, 9 Oct 2018 10:56:37 +0300 Subject: [PATCH 27/27] added variable declaration for `spanConverter` --- src/JaegerExporter.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/JaegerExporter.php b/src/JaegerExporter.php index fe530a2..bb995ed 100644 --- a/src/JaegerExporter.php +++ b/src/JaegerExporter.php @@ -36,22 +36,27 @@ class JaegerExporter implements ExporterInterface /** * @var string */ - protected $host; + private $host; /** * @var int */ - protected $port; + private $port; /** * @var Process */ - protected $process; + private $process; /** * @var AgentIf */ - protected $client; + private $client; + + /** + * @var SpanConverter + */ + private $spanConverter; /** * Create a new Jaeger Exporter.