Skip to content

Commit 662dfe3

Browse files
committed
Identifiers from string
1 parent 55a09ae commit 662dfe3

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

src/Identifier/Identifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ public function __toString()
3939
* @return string
4040
*/
4141
abstract protected function generate();
42-
}
42+
}

src/Identifier/SpanIdentifier.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
*/
1010
class SpanIdentifier extends Identifier
1111
{
12+
/**
13+
* @inheritdoc
14+
*
15+
* @param $fromString string Optional, creates identifier from string
16+
*/
17+
public function __construct($fromString = null)
18+
{
19+
if ($fromString && is_zipkin_span_identifier($fromString)) {
20+
$this->value = $fromString;
21+
} else {
22+
parent::__construct();
23+
}
24+
}
25+
1226
/**
1327
* Generates 128-bit hex-encoded identifier
1428
* http://zipkin.io/pages/instrumenting.html#trace-identifiers

src/Identifier/TraceIdentifier.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
*/
1010
class TraceIdentifier extends Identifier
1111
{
12+
/**
13+
* @inheritdoc
14+
*
15+
* @param $fromString string Optional, creates identifier from string
16+
*/
17+
public function __construct($fromString = null)
18+
{
19+
if ($fromString && is_zipkin_trace_identifier($fromString)) {
20+
$this->value = $fromString;
21+
} else {
22+
parent::__construct();
23+
}
24+
}
25+
1226
/**
1327
* Generates 128-bit hex-encoded identifier
1428
* http://zipkin.io/pages/instrumenting.html#trace-identifiers

src/functions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,29 @@ function is_zipkin_timestamp($timestamp)
2525
return ctype_digit((string) $timestamp) && strlen($timestamp) === 16;
2626
}
2727
}
28+
29+
if (!function_exists('is_zipkin_trace_identifier')) {
30+
/**
31+
* Is zipkin trace identifier
32+
*
33+
* @param $identifier string|\whitemerry\phpkin\Identifier\Identifier
34+
*
35+
* @return bool
36+
*/
37+
function is_zipkin_trace_identifier($identifier) {
38+
return ctype_xdigit((string) $identifier) && strlen((string) $identifier) === 32;
39+
}
40+
}
41+
42+
if (!function_exists('is_zipkin_span_identifier')) {
43+
/**
44+
* Is zipkin span identifier
45+
*
46+
* @param $identifier string|\whitemerry\phpkin\Identifier\Identifier
47+
*
48+
* @return bool
49+
*/
50+
function is_zipkin_span_identifier($identifier) {
51+
return ctype_xdigit((string) $identifier) && strlen((string) $identifier) === 16;
52+
}
53+
}

0 commit comments

Comments
 (0)