From fb6067a7678ca1a3f43c8f783a95d6529a4b8381 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 29 Mar 2018 15:26:14 -0700 Subject: [PATCH 1/3] SpanData now implements stackTraceHashId() --- src/Trace/SpanData.php | 21 +++++++++++++++++++++ tests/unit/Trace/SpanDataTest.php | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/Trace/SpanData.php b/src/Trace/SpanData.php index a0889cac1..db0f3ea4d 100644 --- a/src/Trace/SpanData.php +++ b/src/Trace/SpanData.php @@ -136,6 +136,13 @@ class SpanData */ private $kind; + /** + * An optional value to de-dup the stackTrace array. + * + * @var int + */ + private $stackTraceHashId; + /** * Instantiate a new SpanData instance. * @@ -303,6 +310,20 @@ public function stackTrace() return $this->stackTrace; } + /** + * Return a hash id for this span's stackTrace. + * + * @return id + */ + public function stackTraceHashId() + { + if (!isset($this->stackTraceHashId)) { + // take the lower 16 digits of the md5 and convert to an int + $this->stackTraceHashId = hexdec(substr(md5(serialize($this->stackTrace)), -16)); + } + return $this->stackTraceHashId; + } + /** * Whether or not this span is in the same process as its parent. * diff --git a/tests/unit/Trace/SpanDataTest.php b/tests/unit/Trace/SpanDataTest.php index 711cb6b05..e35b66acf 100644 --- a/tests/unit/Trace/SpanDataTest.php +++ b/tests/unit/Trace/SpanDataTest.php @@ -70,4 +70,23 @@ public function spanDataOptions() ['kind', Span::KIND_SERVER] ]; } + + public function testStackTraceHashIdIsRepeatable() + { + $stackTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $startTime = new \DateTime(); + $endTime = new \DateTime(); + $spanData = new SpanData('span-name', 'aaa', 'bbb', $startTime, $endTime, [ + 'stackTrace' => $stackTrace + ]); + + $spanData2 = new SpanData('span-name2', 'aaa', 'bbb', $startTime, $endTime, [ + 'stackTrace' => $stackTrace + ]); + + $hashId = $spanData->stackTraceHashId(); + $this->assertInternalType('int', $hashId); + $this->assertNotEquals(0, $hashId); + $this->assertEquals($hashId, $spanData2->stackTraceHashId()); + } } From cc56d058035861ba6882bfc046a88a2d4662343d Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 29 Mar 2018 15:45:49 -0700 Subject: [PATCH 2/3] Workaround for intsize on 32-bit PHP --- src/Trace/SpanData.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Trace/SpanData.php b/src/Trace/SpanData.php index db0f3ea4d..2c4b19624 100644 --- a/src/Trace/SpanData.php +++ b/src/Trace/SpanData.php @@ -318,8 +318,11 @@ public function stackTrace() public function stackTraceHashId() { if (!isset($this->stackTraceHashId)) { - // take the lower 16 digits of the md5 and convert to an int - $this->stackTraceHashId = hexdec(substr(md5(serialize($this->stackTrace)), -16)); + // take the lower 16 (8 for 32-bit PHP) digits of the md5 and + // convert to an int + $digits = PHP_INT_SIZE * -2; + $md5 = md5(serialize($this->stackTrace)); + $this->stackTraceHashId = hexdec(substr($md5, $digits)); } return $this->stackTraceHashId; } From 102831b384430c757ed897dfdf5a825be32f0537 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 2 Apr 2018 10:09:54 -0700 Subject: [PATCH 3/3] Store and return the hashId as a hex string to preserve uint64 semantics --- src/Trace/SpanData.php | 15 +++++++-------- tests/unit/Trace/SpanDataTest.php | 3 +-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Trace/SpanData.php b/src/Trace/SpanData.php index 2c4b19624..2f9e9f6d9 100644 --- a/src/Trace/SpanData.php +++ b/src/Trace/SpanData.php @@ -137,9 +137,10 @@ class SpanData private $kind; /** - * An optional value to de-dup the stackTrace array. + * An optional value to de-dup the stackTrace array. Represented as a + * hexadecimal string. * - * @var int + * @var string */ private $stackTraceHashId; @@ -311,18 +312,16 @@ public function stackTrace() } /** - * Return a hash id for this span's stackTrace. + * Return a hash id for this span's stackTrace in hexadecimal * - * @return id + * @return string */ public function stackTraceHashId() { if (!isset($this->stackTraceHashId)) { - // take the lower 16 (8 for 32-bit PHP) digits of the md5 and - // convert to an int - $digits = PHP_INT_SIZE * -2; + // take the lower 16 digits of the md5 $md5 = md5(serialize($this->stackTrace)); - $this->stackTraceHashId = hexdec(substr($md5, $digits)); + $this->stackTraceHashId = substr($md5, 16); } return $this->stackTraceHashId; } diff --git a/tests/unit/Trace/SpanDataTest.php b/tests/unit/Trace/SpanDataTest.php index e35b66acf..2ef4f330c 100644 --- a/tests/unit/Trace/SpanDataTest.php +++ b/tests/unit/Trace/SpanDataTest.php @@ -85,8 +85,7 @@ public function testStackTraceHashIdIsRepeatable() ]); $hashId = $spanData->stackTraceHashId(); - $this->assertInternalType('int', $hashId); - $this->assertNotEquals(0, $hashId); + $this->assertInternalType('string', $hashId); $this->assertEquals($hashId, $spanData2->stackTraceHashId()); } }