Skip to content

Commit

Permalink
fix: remove log (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
evan361425 authored Sep 9, 2021
1 parent c7680d4 commit 99bfeca
Showing 1 changed file with 3 additions and 116 deletions.
119 changes: 3 additions & 116 deletions src/Submission/DaemonSegmentSubmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Napp\Xray\Submission;

use Illuminate\Support\Facades\Log;
use Pkerrigan\Xray\Segment;
use Pkerrigan\Xray\Submission\DaemonSegmentSubmitter as SubmissionDaemonSegmentSubmitter;
use Pkerrigan\Xray\Submission\SegmentSubmitter;

class DaemonSegmentSubmitter implements SegmentSubmitter
Expand Down Expand Up @@ -36,10 +36,10 @@ public function __construct()
*
* @return DaemonSegmentSubmitterWithLog
*/
protected function submitter(): DaemonSegmentSubmitterWithLog
protected function submitter(): SubmissionDaemonSegmentSubmitter
{
if (is_null($this->submitter)) {
$this->submitter = new DaemonSegmentSubmitterWithLog(
$this->submitter = new SubmissionDaemonSegmentSubmitter(
$this->host,
$this->port
);
Expand All @@ -54,119 +54,6 @@ protected function submitter(): DaemonSegmentSubmitterWithLog
*/
public function submitSegment(Segment $segment)
{
Log::channel('trace-log')->debug('xray send start');
$this->submitter()->submitSegment($segment);
Log::channel('trace-log')->debug('xray send end');
}
}

class DaemonSegmentSubmitterWithLog
{
const MAX_SEGMENT_SIZE = 64000;

const HEADER = [
'format' => 'json',
'version' => 1
];

/**
* @var string
*/
private $host;

/**
* @var int
*/
private $port;

/**
* @var \Socket
*/
private $socket;

public function __construct(string $host = '127.0.0.1', int $port = 2000)
{
$this->host = $host;
$this->port = $port;
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
}

public function __destruct()
{
Log::channel('trace-log')->debug('xray destruct');
socket_close($this->socket);
}

/**
* @param Segment $segment
* @return void
*/
public function submitSegment(Segment $segment)
{
$packet = $this->buildPacket($segment);
$packetLength = strlen($packet);

if ($packetLength > self::MAX_SEGMENT_SIZE) {
$this->submitFragmented($segment);
return;
}

$result = $this->sendPacket($packet);
Log::channel('trace-log')->debug('xray send by submitSegment', ['result' => $result, 'packet' => $packet]);
}

/**
* @param Segment|array $segment
* @return string
*/
private function buildPacket($segment): string
{
return implode("\n", array_map('json_encode', [self::HEADER, $segment]));
}

/**
* @param string $packet
* @return void
*/
private function sendPacket(string $packet)
{
return socket_sendto($this->socket, $packet, strlen($packet), 0, $this->host, $this->port);
}

/**
* @param Segment $segment
* @return void
*/
private function submitFragmented(Segment $segment)
{
$rawSegment = $segment->jsonSerialize();
/** @var Segment[] $subsegments */
$subsegments = $rawSegment['subsegments'] ?? [];
unset($rawSegment['subsegments']);
$this->submitOpenSegment($rawSegment);

foreach ($subsegments as $subsegment) {
$subsegment = clone $subsegment;
$subsegment->setParentId($segment->getId())
->setTraceId($segment->getTraceId())
->setIndependent(true);
$this->submitSegment($subsegment);
}

$completePacket = $this->buildPacket($rawSegment);
$result = $this->sendPacket($completePacket);
Log::channel('trace-log')->debug('xray send by submitFragmented', ['result' => $result, 'packet' => $completePacket]);
}

/**
* @param $rawSegment
* @return void
*/
private function submitOpenSegment(array $openSegment)
{
unset($openSegment['end_time']);
$openSegment['in_progress'] = true;
$initialPacket = $this->buildPacket($openSegment);
$this->sendPacket($initialPacket);
}
}

0 comments on commit 99bfeca

Please sign in to comment.