Skip to content

Commit

Permalink
Prevent creation of invalid trace id / span id (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay authored Jun 6, 2022
1 parent 7dda601 commit 3ca87a8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/SDK/Trace/RandomIdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OpenTelemetry\SDK\Trace;

use OpenTelemetry\API\Trace\SpanContext;
use Throwable;

class RandomIdGenerator implements IdGeneratorInterface
Expand All @@ -13,12 +14,20 @@ class RandomIdGenerator implements IdGeneratorInterface

public function generateTraceId(): string
{
return $this->randomHex(self::TRACE_ID_HEX_LENGTH);
do {
$traceId = $this->randomHex(self::TRACE_ID_HEX_LENGTH);
} while (!SpanContext::isValidTraceId($traceId));

return $traceId;
}

public function generateSpanId(): string
{
return $this->randomHex(self::SPAN_ID_HEX_LENGTH);
do {
$spanId = $this->randomHex(self::SPAN_ID_HEX_LENGTH);
} while (!SpanContext::isValidSpanId($spanId));

return $spanId;
}

/**
Expand Down

0 comments on commit 3ca87a8

Please sign in to comment.