Skip to content

Commit

Permalink
fix: no faulty host resolving (#4)
Browse files Browse the repository at this point in the history
contributors:
@mmross @cHeeSaW
  • Loading branch information
cawolf authored Feb 6, 2020
1 parent 5920561 commit 6cb95c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
10 changes: 4 additions & 6 deletions Factory/ZipkinTracerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
final class ZipkinTracerFactory implements TracerFactory
{
private $logger;
private $agentHostResolver;

public function __construct(LoggerInterface $logger)
public function __construct(AgentHostResolver $agentHostResolver, LoggerInterface $logger)
{
$this->agentHostResolver = $agentHostResolver;
$this->logger = $logger;
}

Expand All @@ -30,12 +32,8 @@ public function create(string $projectName, string $agentHost, string $agentPort
{
$tracer = new NoopTracer();

if (!dns_get_record($agentHost) && !filter_var($agentHost, FILTER_VALIDATE_IP)) {
$this->logger->warning(self::class . ': could not resolve agent host "' . $agentHost . '"');
return $tracer;
}

try {
$this->agentHostResolver->ensureAgentHostIsResolvable($agentHost);
$endpoint = Endpoint::create($projectName, gethostbyname($agentHost), null, (int) $agentPort);
$reporter = new Http();
$sampler = BinarySampler::createAsAlwaysSample();
Expand Down
14 changes: 10 additions & 4 deletions Tests/Factory/ZipkinTracerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

namespace Auxmoney\OpentracingBundle\Tests\Factory;

use Auxmoney\OpentracingBundle\Factory\AgentHostResolver;
use Auxmoney\OpentracingBundle\Factory\ZipkinTracerFactory;
use OpenTracing\NoopTracer;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
use RuntimeException;
use ZipkinOpenTracing\Tracer;

class ZipkinTracerFactoryTest extends TestCase
{
private $agentHostResolver;
private $logger;
private $projectName;
private $agentHost;
Expand All @@ -23,27 +26,30 @@ public function setUp()
{
parent::setUp();
$this->logger = $this->prophesize(LoggerInterface::class);
$this->agentHostResolver = $this->prophesize(AgentHostResolver::class);
$this->projectName = 'project name';
$this->agentHost = 'localhost';
$this->agentPort = '9411';

$this->subject = new ZipkinTracerFactory($this->logger->reveal());
$this->subject = new ZipkinTracerFactory($this->agentHostResolver->reveal(), $this->logger->reveal());
}

public function testCreateSuccess(): void
{
$this->agentHostResolver->ensureAgentHostIsResolvable('localhost')->shouldBeCalled();
$this->logger->warning(Argument::type('string'))->shouldNotBeCalled();

self::assertInstanceOf(Tracer::class, $this->subject->create($this->projectName, $this->agentHost, $this->agentPort));
}

public function testCreateNoDnsOrIp(): void
public function testCreateResolvingFailed(): void
{
$this->logger->warning(Argument::containingString('could not resolve agent host'))->shouldBeCalledOnce();
$this->agentHostResolver->ensureAgentHostIsResolvable('localhost')->shouldBeCalled()->willThrow(new RuntimeException('resolving failer'));
$this->logger->warning(Argument::containingString('resolving failer'))->shouldBeCalledOnce();

self::assertInstanceOf(
NoopTracer::class,
$this->subject->create($this->projectName, 'älsakfdkaofkeäkvaäsooäaegölsgälkfdvpaoskvä.cöm', $this->agentPort)
$this->subject->create($this->projectName, $this->agentHost, $this->agentPort)
);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"require": {
"php": "^7.1.33",
"auxmoney/opentracing-bundle-core": "^0.3.3",
"auxmoney/opentracing-bundle-core": "^0.3.5",
"opentracing/opentracing": "1.0.0-beta5@beta",
"jcchavezs/zipkin-opentracing": "^0.1"
},
Expand Down

0 comments on commit 6cb95c9

Please sign in to comment.