Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add exception when generated thrift string exceeds maximum allowed package siz #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Jaeger/Thrift/AgentClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Jaeger\Thrift;

use Exception;
use Thrift\Transport\TMemoryBuffer;
use Thrift\Protocol\TCompactProtocol;
use Thrift\Type\TMessageType;
Expand All @@ -26,6 +27,9 @@ class AgentClient

public static $tptl = null;

/**
* @throws Exception
*/
public function buildThrift($batch)
{
$tran = new TMemoryBuffer();
Expand All @@ -42,6 +46,16 @@ public function buildThrift($batch)
$batchLen = $tran->available();
$batchThriftStr = $tran->read(Constants\UDP_PACKET_MAX_LENGTH);

if ($batchLen !== strlen($batchThriftStr)) {
throw new Exception(
sprintf(
'thrift string was longer than maximum allowed length: %d > %d',
$batchLen,
strlen($batchThriftStr)
)
);
}

return ['len' => $batchLen, 'thriftStr' => $batchThriftStr];
}

Expand Down
80 changes: 80 additions & 0 deletions tests/Thrift/AgentClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace tests\Thrift;

use Exception;
use Jaeger\Thrift\AgentClient;
use PHPUnit\Framework\TestCase;

class AgentClientTest extends TestCase
{
/**
* @param int $spanCount
* @dataProvider provideSpanCount
* @throws Exception
*/
public function testBuildThriftSuccess($spanCount)
{
$batch = $this->createBatchWithSpans($spanCount);

$subject = new AgentClient();
$result = $subject->buildThrift($batch);
self::assertSame($result['len'], strlen($result['thriftStr']));
}

public function provideSpanCount()
{
return [
[1],
[10],
[100],
[999]
];
}

public function testBuildThriftFailsTooLong()
{
$batch = $this->createBatchWithSpans(1000);

$this->expectException(Exception::class);
$this->expectExceptionMessageRegExp('/thrift string was longer than maximum allowed length/');

$subject = new AgentClient();
$subject->buildThrift($batch);
}

/**
* @param int $spanCount
* @return array
*/
private function createBatchWithSpans($spanCount)
{
$batch = [
'thriftProcess' => [
'serverName' => 'some server name',
'tags' => [
'peer.ipv4' => '0.0.0.0'
]
],
'thriftSpans' => []
];

for ($i = 0; $i < $spanCount; $i++) {
$batch['thriftSpans'][] = [
'spanId' => '123456789',
'operationName' => 'operation name 123456789012',
'tags' => [
'span.kind' => 'client'
],
'traceIdLow' => '999012393582',
'traceIdHigh' => '12338923423',
'parentSpanId' => null,
'flags' => null,
'startTime' => time(),
'duration' => 5,
'references' => []
];
}
return $batch;
}
}