Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
/ hyperf-tracer Public archive
forked from hyperf/tracer

Commit

Permalink
[CORE-936] Usando Correlation ID como Trace ID dos Spans (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
leocavalcante authored Dec 12, 2022
1 parent 76d8c1a commit 002090b
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 6 deletions.
106 changes: 106 additions & 0 deletions class_map/SpanContext.php
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);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"source": "https://github.com/PicPay/hyperf-tracer"
},
"require": {
"php": ">=7.4",
"php": ">=8.0",
"ext-json": "*",
"ext-sockets": "*",
"hyperf/contract": "^2.2|^3.0",
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use GuzzleHttp\Client;
use Hyperf\Tracer\Listener\DbQueryExecutedListener;
use Jaeger\SpanContext;
use Jaeger\ThriftUdpTransport;
use OpenTracing\Tracer;

Expand All @@ -37,6 +38,7 @@ public function __invoke(): array
],
'class_map' => [
ThriftUdpTransport::class => __DIR__ . '/../class_map/ThriftUdpTransport.php',
SpanContext::class => __DIR__ . '/../class_map/SpanContext.php',
],
],
],
Expand Down
6 changes: 6 additions & 0 deletions src/SpanStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Hyperf\Context\Context;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Engine\Exception\CoroutineDestroyedException;
use Hyperf\Tracer\Support\Uuid;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use OpenTracing\Span;
Expand Down Expand Up @@ -56,6 +57,11 @@ protected function startSpan(
}
$root = $this->tracer->startSpan($name, $option);
$root->setTag(SPAN_KIND, $kind);

if ($spanContext === null && ! empty($correlationId = $request->getHeaderLine('X-Request-ID'))) {
$root->getContext()->setTraceId((string) Uuid::asInt($correlationId));
}

Context::set('tracer.root', $root);
return $root;
}
Expand Down
30 changes: 30 additions & 0 deletions src/Support/Uuid.php
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]);
}
}
30 changes: 30 additions & 0 deletions tests/Support/UuidTest.php
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'));
}
}

0 comments on commit 002090b

Please sign in to comment.