Skip to content

Commit

Permalink
Support of 128bit trace id's. Support of Jaeger thrift compact over U…
Browse files Browse the repository at this point in the history
…DP protocol. And other minor fixes.

Fix followed issues:
#129
#120
#42
  • Loading branch information
Ilya Burtsev committed Aug 25, 2023
1 parent 3173d9c commit 782f67a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/Jaeger/Codec/TextCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public function extract($carrier)
$baggage = null;
$debugId = null;

foreach ($carrier as $key => $value) {
foreach ((array)$carrier as $key => $value) {
$ucKey = strtolower($key);

if ($ucKey === $this->traceIdHeader) {
if ($this->urlEncoding) {
$value = urldecode($value);
}
list($traceId, $spanId, $parentId, $flags) =
[$traceId, $spanId, $parentId, $flags] =
$this->spanContextFromString($value);
} elseif ($this->startsWith($ucKey, $this->baggagePrefix)) {
if ($this->urlEncoding) {
Expand Down Expand Up @@ -143,7 +143,10 @@ public function extract($carrier)
private function spanContextToString($traceId, $spanId, $parentId, $flags)
{
$parentId = $parentId ?? 0;
return sprintf('%x:%x:%x:%x', $traceId, $spanId, $parentId, $flags);
if (is_int($traceId)) {
$traceId = sprintf('%016x', $traceId);
}
return sprintf('%s:%x:%x:%x', $traceId, $spanId, $parentId, $flags);
}

/**
Expand All @@ -163,7 +166,7 @@ private function spanContextFromString($value): array
}

return [
CodecUtility::hexToInt64($parts[0]),
$parts[0],
CodecUtility::hexToInt64($parts[1]),
CodecUtility::hexToInt64($parts[2]),
$parts[3],
Expand Down
20 changes: 19 additions & 1 deletion src/Jaeger/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config

const ZIPKIN_OVER_COMPACT_UDP = "zipkin_over_compact_udp";
const JAEGER_OVER_BINARY_UDP = "jaeger_over_binary_udp";
const JAEGER_OVER_COMPACT_UDP = "jaeger_over_compact_udp";
const JAEGER_OVER_BINARY_HTTP = "jaeger_over_binary_http";

const IPV6 = "IPv6";
Expand All @@ -35,7 +36,12 @@ class Config
*/
public static function getAvailableDispatchModes()
{
return [self::ZIPKIN_OVER_COMPACT_UDP, self::JAEGER_OVER_BINARY_UDP, self::JAEGER_OVER_BINARY_HTTP];
return [
self::ZIPKIN_OVER_COMPACT_UDP,
self::JAEGER_OVER_BINARY_UDP,
self::JAEGER_OVER_BINARY_HTTP,
self::JAEGER_OVER_COMPACT_UDP,
];
}

/**
Expand Down Expand Up @@ -175,13 +181,22 @@ private function getLogging(): bool
return (bool)($this->config['logging'] ?? false);
}

/**
* @return string
*/
public function getDispatchMode(): string
{
return $this->config['dispatch_mode'];
}

/**
* @return ReporterInterface
*/
private function getReporter(): ReporterInterface
{
switch ($this->config["dispatch_mode"]) {
case self::JAEGER_OVER_BINARY_UDP:
case self::JAEGER_OVER_COMPACT_UDP:
$reporter = (new JaegerReporterFactory($this))->createReporter();
break;
case self::ZIPKIN_OVER_COMPACT_UDP:
Expand Down Expand Up @@ -271,6 +286,9 @@ public function getLocalAgentReportingPort(): int
case self::JAEGER_OVER_BINARY_UDP:
$port = DEFAULT_JAEGER_UDP_BINARY_REPORTING_PORT;
break;
case self::JAEGER_OVER_COMPACT_UDP:
$port = DEFAULT_JAEGER_UDP_COMPACT_REPORTING_PORT;
break;
case self::JAEGER_OVER_BINARY_HTTP:
$port = DEFAULT_JAEGER_HTTP_BINARY_REPORTING_PORT;
break;
Expand Down
1 change: 1 addition & 0 deletions src/Jaeger/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

const DEFAULT_ZIPKIN_UDP_COMPACT_REPORTING_PORT = 5775;
const DEFAULT_JAEGER_UDP_BINARY_REPORTING_PORT = 6832;
const DEFAULT_JAEGER_UDP_COMPACT_REPORTING_PORT = 6831;
const DEFAULT_JAEGER_HTTP_BINARY_REPORTING_PORT = 14268;

const DEFAULT_SAMPLING_PORT = 5778;
Expand Down
24 changes: 22 additions & 2 deletions src/Jaeger/Mapper/SpanToJaegerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jaeger\Mapper;

use Jaeger\Codec\CodecUtility;
use Jaeger\Span;
use Jaeger\Thrift\Agent\Zipkin\AnnotationType;
use Jaeger\Thrift\Agent\Zipkin\BinaryAnnotation;
Expand Down Expand Up @@ -155,9 +156,11 @@ public function mapSpanToJaeger(Span $span) : JaegerThriftSpan
]);
}

[$low, $high] = $this->extractTraceIdFromString($span->getContext()->getTraceId());

return new JaegerThriftSpan([
"traceIdLow" => (int)$span->getContext()->getTraceId(),
"traceIdHigh" => 0,
"traceIdLow" => $low,
"traceIdHigh" => $high,
"spanId" => (int)$span->getContext()->getSpanId(),
"parentSpanId" => (int)$span->getContext()->getParentId(),
"operationName" => $span->getOperationName(),
Expand All @@ -168,4 +171,21 @@ public function mapSpanToJaeger(Span $span) : JaegerThriftSpan
"logs" => $logs
]);
}

private function extractTraceIdFromString(?string $id): array
{
if ($id === null) {
return [0, 0];
}

if (strlen($id) > 16) {
$traceIdLow = CodecUtility::hexToInt64(substr($id, -16, 16));
$traceIdHigh = CodecUtility::hexToInt64(substr($id, 0, 16));
} else {
$traceIdLow = (int) CodecUtility::hexToInt64($id);
$traceIdHigh = 0;
}

return [$traceIdLow, $traceIdHigh];
}
}
8 changes: 5 additions & 3 deletions src/Jaeger/ReporterFactory/JaegerReporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace Jaeger\ReporterFactory;

use Jaeger\AgentClient\HttpAgentClient;
use Jaeger\Config;
use Jaeger\Reporter\JaegerReporter;
use Jaeger\Reporter\ReporterInterface;
use Jaeger\Sender\JaegerSender;
use Jaeger\Thrift\Agent\AgentClient;
use Jaeger\ThriftUdpTransport;
use Thrift\Exception\TTransportException;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TCompactProtocol;
use Thrift\Transport\TBufferedTransport;

class JaegerReporterFactory extends AbstractReporterFactory implements ReporterFactoryInterface
{
public function createReporter() : ReporterInterface
public function createReporter(): ReporterInterface
{
$udp = new ThriftUdpTransport(
$this->config->getLocalAgentReportingHost(),
Expand All @@ -34,7 +35,8 @@ public function createReporter() : ReporterInterface
} catch (TTransportException $e) {
$this->config->getLogger()->warning($e->getMessage());
}
$protocol = new TBinaryProtocol($transport);
$protocol = $this->config->getDispatchMode() === Config::JAEGER_OVER_COMPACT_UDP ?
new TCompactProtocol($transport) : new TBinaryProtocol($transport);
$client = new AgentClient($protocol);
$this->config->getLogger()->debug('Initializing UDP Jaeger Tracer with Jaeger.Thrift over Binary protocol');
$sender = new JaegerSender($client, $this->config->getLogger());
Expand Down
16 changes: 13 additions & 3 deletions tests/Jaeger/Codec/TextCodecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function testCanInjectSimpleContextInCarrier(): void

$this->assertCount(1 , $carrier);
$this->assertArrayHasKey(TRACE_ID_HEADER, $carrier);
$this->assertSame('trace-id:0:0:0', $carrier[TRACE_ID_HEADER]);
}

public function testTraceIdConvertToHex(): void
{
$context = new SpanContext(3003082921159205154, 'span-id', null, null);
$carrier = [];

$this->textCodec->inject($context, $carrier);
$this->assertSame('29ad18017abca122:0:0:0', $carrier[TRACE_ID_HEADER]);
}

/**
Expand Down Expand Up @@ -95,7 +105,7 @@ public function carrierDataProvider(): array
[
TRACE_ID_HEADER => '32834e4115071776:f7802330248418d:f123456789012345:1'
],
"3639838965278119798",
"32834e4115071776",
"1114643325879075213",
"-1070935975401544891",
1,
Expand All @@ -107,7 +117,7 @@ public function carrierDataProvider(): array
TRACE_ID_HEADER => '32834e4115071776:f7802330248418d:f123456789012345:1',
BAGGAGE_HEADER_PREFIX . 'baggage-1' => 'https://testdomain.sk',
],
"3639838965278119798",
"32834e4115071776",
"1114643325879075213",
"-1070935975401544891",
1,
Expand All @@ -119,7 +129,7 @@ public function carrierDataProvider(): array
TRACE_ID_HEADER => '32834e4115071776:f7802330248418d:f123456789012345:1',
BAGGAGE_HEADER_PREFIX . 'baggage-1' => 'https%3A%2F%2Ftestdomain.sk',
],
"3639838965278119798",
"32834e4115071776",
"1114643325879075213",
"-1070935975401544891",
1,
Expand Down
5 changes: 4 additions & 1 deletion tests/Jaeger/Mapper/SpanToJaegerMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SpanToJaegerMapperTest extends \PHPUnit\Framework\TestCase
public function setUp(): void
{
$this->tracer = new Tracer($this->serviceName, new NullReporter, new ConstSampler);
$this->context = new SpanContext(0, 0, 0, SAMPLED_FLAG);
$this->context = new SpanContext('5f2c2ea76d359a165f2c2ea76d35b26b', 0, 0, SAMPLED_FLAG);
}

/**
Expand All @@ -61,6 +61,9 @@ public function shouldProperlyInitializeAtConstructTime(): void
$mapper = new SpanToJaegerMapper();
$thriftSpan = $mapper->mapSpanToJaeger($span);

$this->assertSame(6857907629205068310, $thriftSpan->traceIdHigh);
$this->assertSame(6857907629205074539, $thriftSpan->traceIdLow);

$index = 0;
$this->assertEquals($thriftSpan->tags[$index]->key, "component");
$this->assertEquals($thriftSpan->tags[$index]->vType, TagType::STRING);
Expand Down
2 changes: 1 addition & 1 deletion tests/Jaeger/TracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function shouldInjectSpanContextToCarrier(): void
$this->tracer->inject($spanContext, TEXT_MAP, $carrier);

$this->assertCount(1, $carrier);
$this->assertEquals('0:0:0:0', $carrier[TRACE_ID_HEADER]);
$this->assertEquals('0000000000000000:0:0:0', $carrier[TRACE_ID_HEADER]);
}

/** @test */
Expand Down

0 comments on commit 782f67a

Please sign in to comment.