This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
forked from hyperf/tracer
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CORE-936] Usando Correlation ID como Trace ID dos Spans (#16)
- Loading branch information
1 parent
76d8c1a
commit 002090b
Showing
7 changed files
with
180 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf + PicPay. | ||
* | ||
* @link https://github.com/PicPay/hyperf-tracer | ||
* @document https://github.com/PicPay/hyperf-tracer/wiki | ||
* @contact @PicPay | ||
* @license https://github.com/PicPay/hyperf-tracer/blob/main/LICENSE | ||
*/ | ||
namespace Jaeger; | ||
|
||
use ArrayIterator; | ||
use OpenTracing\SpanContext as OTSpanContext; | ||
use ReturnTypeWillChange; | ||
|
||
class SpanContext implements OTSpanContext | ||
{ | ||
private array $baggage; | ||
|
||
public function __construct( | ||
private string $traceId, | ||
private string $spanId, | ||
private ?string $parentId = null, | ||
private ?int $flags = null, | ||
?array $baggage = null, | ||
private ?int $debugId = null, | ||
) { | ||
$this->baggage = $baggage ?? []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
#[ReturnTypeWillChange] | ||
public function getIterator(): ArrayIterator|iterable | ||
{ | ||
return new ArrayIterator($this->baggage); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getBaggageItem(string $key): ?string | ||
{ | ||
return $this->baggage[$key] ?? null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @return SpanContext | ||
*/ | ||
public function withBaggageItem(string $key, string $value): OTSpanContext | ||
{ | ||
return new self( | ||
$this->traceId, | ||
$this->spanId, | ||
$this->parentId, | ||
$this->flags, | ||
[$key => $value] + $this->baggage | ||
); | ||
} | ||
|
||
public function getTraceId(): string | ||
{ | ||
return $this->traceId; | ||
} | ||
|
||
public function setTraceId(string $traceId): self | ||
{ | ||
$this->traceId = $traceId; | ||
return $this; | ||
} | ||
|
||
public function getParentId(): ?string | ||
{ | ||
return $this->parentId; | ||
} | ||
|
||
public function getSpanId(): string | ||
{ | ||
return $this->spanId; | ||
} | ||
|
||
public function getFlags(): ?int | ||
{ | ||
return $this->flags; | ||
} | ||
|
||
public function getBaggage(): array | ||
{ | ||
return $this->baggage; | ||
} | ||
|
||
public function getDebugId(): ?int | ||
{ | ||
return $this->debugId; | ||
} | ||
|
||
public function isDebugIdContainerOnly(): bool | ||
{ | ||
return ($this->traceId === null) && ($this->debugId !== null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf + PicPay. | ||
* | ||
* @link https://github.com/PicPay/hyperf-tracer | ||
* @document https://github.com/PicPay/hyperf-tracer/wiki | ||
* @contact @PicPay | ||
* @license https://github.com/PicPay/hyperf-tracer/blob/main/LICENSE | ||
*/ | ||
namespace Hyperf\Tracer\Support; | ||
|
||
use RuntimeException; | ||
|
||
final class Uuid | ||
{ | ||
public static function asInt(string $uuid): int | ||
{ | ||
$bin = hex2bin(str_replace(['{', '-', '}'], '', $uuid)); | ||
[$left, $right] = str_split($bin, 8); | ||
$results = unpack('q', $left ^ $right); | ||
|
||
if (empty($results)) { | ||
throw new RuntimeException("Error unpacking given UUID {$uuid}"); | ||
} | ||
|
||
return abs($results[1] ?? $results[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf + PicPay. | ||
* | ||
* @link https://github.com/PicPay/hyperf-tracer | ||
* @document https://github.com/PicPay/hyperf-tracer/wiki | ||
* @contact @PicPay | ||
* @license https://github.com/PicPay/hyperf-tracer/blob/main/LICENSE | ||
*/ | ||
namespace HyperfTest\Tracer\Support; | ||
|
||
use Hyperf\Tracer\Support\Uuid; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class UuidTest extends TestCase | ||
{ | ||
public function testAsInt(): void | ||
{ | ||
self::assertSame(0, Uuid::asInt('00000000-0000-0000-0000-000000000000')); | ||
self::assertSame(9189094052915056584, Uuid::asInt('96b2bd47-a66e-434f-ae42-ea5703a93acf')); | ||
self::assertSame(2817854019230525549, Uuid::asInt('29adb00e-dc41-495a-ba3a-6bf46eb9ad82')); | ||
self::assertSame(7856230658856071025, Uuid::asInt('d88187ee-37dd-41ba-a9ae-21101f2d47d7')); | ||
self::assertSame(0, Uuid::asInt('ffffffff-ffff-ffff-ffff-ffffffffffff')); | ||
} | ||
} |