Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 8d8ae0e

Browse files
authored
SpanData now implements stackTraceHashId() (#154)
* SpanData now implements stackTraceHashId() * Workaround for intsize on 32-bit PHP * Store and return the hashId as a hex string to preserve uint64 semantics
1 parent eb528d3 commit 8d8ae0e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Trace/SpanData.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ class SpanData
136136
*/
137137
private $kind;
138138

139+
/**
140+
* An optional value to de-dup the stackTrace array. Represented as a
141+
* hexadecimal string.
142+
*
143+
* @var string
144+
*/
145+
private $stackTraceHashId;
146+
139147
/**
140148
* Instantiate a new SpanData instance.
141149
*
@@ -303,6 +311,21 @@ public function stackTrace()
303311
return $this->stackTrace;
304312
}
305313

314+
/**
315+
* Return a hash id for this span's stackTrace in hexadecimal
316+
*
317+
* @return string
318+
*/
319+
public function stackTraceHashId()
320+
{
321+
if (!isset($this->stackTraceHashId)) {
322+
// take the lower 16 digits of the md5
323+
$md5 = md5(serialize($this->stackTrace));
324+
$this->stackTraceHashId = substr($md5, 16);
325+
}
326+
return $this->stackTraceHashId;
327+
}
328+
306329
/**
307330
* Whether or not this span is in the same process as its parent.
308331
*

tests/unit/Trace/SpanDataTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,22 @@ public function spanDataOptions()
7070
['kind', Span::KIND_SERVER]
7171
];
7272
}
73+
74+
public function testStackTraceHashIdIsRepeatable()
75+
{
76+
$stackTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
77+
$startTime = new \DateTime();
78+
$endTime = new \DateTime();
79+
$spanData = new SpanData('span-name', 'aaa', 'bbb', $startTime, $endTime, [
80+
'stackTrace' => $stackTrace
81+
]);
82+
83+
$spanData2 = new SpanData('span-name2', 'aaa', 'bbb', $startTime, $endTime, [
84+
'stackTrace' => $stackTrace
85+
]);
86+
87+
$hashId = $spanData->stackTraceHashId();
88+
$this->assertInternalType('string', $hashId);
89+
$this->assertEquals($hashId, $spanData2->stackTraceHashId());
90+
}
7391
}

0 commit comments

Comments
 (0)