diff --git a/src/API/Trace/SpanContext.php b/src/API/Trace/SpanContext.php index 5b235d01c..ae8d16b46 100644 --- a/src/API/Trace/SpanContext.php +++ b/src/API/Trace/SpanContext.php @@ -4,6 +4,8 @@ namespace OpenTelemetry\API\Trace; +use function hex2bin; + final class SpanContext implements SpanContextInterface { private static ?SpanContextInterface $invalidContext = null; @@ -49,11 +51,21 @@ public function getTraceId(): string return $this->traceId; } + public function getTraceIdBinary(): string + { + return hex2bin($this->traceId); + } + public function getSpanId(): string { return $this->spanId; } + public function getSpanIdBinary(): string + { + return hex2bin($this->spanId); + } + public function getTraceState(): ?TraceStateInterface { return $this->traceState; diff --git a/src/API/Trace/SpanContextInterface.php b/src/API/Trace/SpanContextInterface.php index f049bb321..e8126bbfd 100644 --- a/src/API/Trace/SpanContextInterface.php +++ b/src/API/Trace/SpanContextInterface.php @@ -12,20 +12,17 @@ interface SpanContextInterface public const TRACE_FLAG_SAMPLED = 0x01; public const TRACE_FLAG_DEFAULT = 0x00; - /** @todo Implement this in the API layer */ public static function createFromRemoteParent(string $traceId, string $spanId, int $traceFlags = self::TRACE_FLAG_DEFAULT, ?TraceStateInterface $traceState = null): SpanContextInterface; - - /** @todo Implement this in the API layer */ public static function getInvalid(): SpanContextInterface; - - /** @todo Implement this in the API layer */ public static function create(string $traceId, string $spanId, int $traceFlags = self::TRACE_FLAG_DEFAULT, ?TraceStateInterface $traceState = null): SpanContextInterface; /** @psalm-mutation-free */ public function getTraceId(): string; + public function getTraceIdBinary(): string; /** @psalm-mutation-free */ public function getSpanId(): string; + public function getSpanIdBinary(): string; public function getTraceFlags(): int; public function getTraceState(): ?TraceStateInterface; public function isValid(): bool; diff --git a/src/Contrib/Otlp/SpanConverter.php b/src/Contrib/Otlp/SpanConverter.php index 0b8860c82..99fee4060 100644 --- a/src/Contrib/Otlp/SpanConverter.php +++ b/src/Contrib/Otlp/SpanConverter.php @@ -4,7 +4,6 @@ namespace OpenTelemetry\Contrib\Otlp; -use function hex2bin; use OpenTelemetry\API\Trace as API; use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceRequest; use Opentelemetry\Proto\Common\V1\InstrumentationScope; @@ -140,11 +139,11 @@ private function convertStatusCode(string $status): int private function convertSpan(SpanDataInterface $span): Span { $pSpan = new Span(); - $pSpan->setTraceId(hex2bin($span->getContext()->getTraceId())); - $pSpan->setSpanId(hex2bin($span->getContext()->getSpanId())); + $pSpan->setTraceId($span->getContext()->getTraceIdBinary()); + $pSpan->setSpanId($span->getContext()->getSpanIdBinary()); $pSpan->setTraceState((string) $span->getContext()->getTraceState()); if ($span->getParentContext()->isValid()) { - $pSpan->setParentSpanId(hex2bin($span->getParentContext()->getSpanId())); + $pSpan->setParentSpanId($span->getParentContext()->getSpanIdBinary()); } $pSpan->setName($span->getName()); $pSpan->setKind($this->convertSpanKind($span->getKind())); @@ -164,8 +163,8 @@ private function convertSpan(SpanDataInterface $span): Span foreach ($span->getLinks() as $link) { /** @psalm-suppress InvalidArgument */ $pSpan->getLinks()[] = $pLink = new Link(); - $pLink->setTraceId(hex2bin($link->getSpanContext()->getTraceId())); - $pLink->setSpanId(hex2bin($link->getSpanContext()->getSpanId())); + $pLink->setTraceId($link->getSpanContext()->getTraceIdBinary()); + $pLink->setSpanId($link->getSpanContext()->getSpanIdBinary()); $pLink->setTraceState((string) $link->getSpanContext()->getTraceState()); $this->setAttributes($pLink, $link->getAttributes()); } diff --git a/tests/Unit/API/Trace/SpanContextTest.php b/tests/Unit/API/Trace/SpanContextTest.php index ebc3741da..af64e540c 100644 --- a/tests/Unit/API/Trace/SpanContextTest.php +++ b/tests/Unit/API/Trace/SpanContextTest.php @@ -61,6 +61,16 @@ public function test_get_trace_id(): void $this->assertSame(self::SECOND_TRACE_ID, $this->second->getTraceId()); } + public function test_get_trace_and_span_id_binary(): void + { + $traceId = '7d9df3359179cfd468fdf360f3e29f1a'; + $spanId = '7d9df3359179cfd4'; + + $spanContext = SpanContext::create($traceId, $spanId); + $this->assertSame(hex2bin($traceId), $spanContext->getTraceIdBinary()); + $this->assertSame(hex2bin($spanId), $spanContext->getSpanIdBinary()); + } + public function test_get_span_id(): void { $this->assertSame(self::FIRST_SPAN_ID, $this->first->getSpanId());