Skip to content

Commit

Permalink
implement retrieve span and trace id as binary (#883)
Browse files Browse the repository at this point in the history
per spec, https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#retrieving-the-traceid-and-spanid allow retrieving span and trace id in binary form
remove hex2bin calls from otlp span converter and use new binary accessor
  • Loading branch information
brettmc authored Dec 8, 2022
1 parent 7bfc121 commit f5698c8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/API/Trace/SpanContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace OpenTelemetry\API\Trace;

use function hex2bin;

final class SpanContext implements SpanContextInterface
{
private static ?SpanContextInterface $invalidContext = null;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions src/API/Trace/SpanContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions src/Contrib/Otlp/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand All @@ -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());
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/API/Trace/SpanContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit f5698c8

Please sign in to comment.