Skip to content

Commit

Permalink
Merge 2a40bed into 93d6565
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldducky committed Feb 20, 2019
2 parents 93d6565 + 2a40bed commit 7cc7a8d
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Handler/DefaultHandler.php
Expand Up @@ -4,8 +4,20 @@

class DefaultHandler implements Handler
{
private $hasDistributedTracing;

public function __construct($hasDistributedTracing)
{
$this->hasDistributedTracing = $hasDistributedTracing;
}

public function handle($functionName, array $arguments = array())
{
return call_user_func_array($functionName, $arguments);
}

public function isDistributedTracingEnabled()
{
return $this->hasDistributedTracing;
}
}
5 changes: 5 additions & 0 deletions src/Handler/Handler.php
Expand Up @@ -11,4 +11,9 @@ interface Handler
* @return mixed
*/
public function handle($functionName, array $arguments = array());

/**
* @return bool
*/
public function isDistributedTracingEnabled();
}
5 changes: 5 additions & 0 deletions src/Handler/NullHandler.php
Expand Up @@ -8,4 +8,9 @@ public function handle($functionName, array $arguments = array())
{
return false;
}

public function isDistributedTracingEnabled()
{
return false;
}
}
57 changes: 56 additions & 1 deletion src/Newrelic.php
Expand Up @@ -57,7 +57,7 @@ public function __construct($throw = false, Handler $handler = null)
}

if ($handler === null) {
$handler = $this->installed ? new DefaultHandler() : new NullHandler();
$handler = $this->installed ? new DefaultHandler(function_exists('newrelic_create_distributed_trace_payload')) : new NullHandler();
}

$this->handler = $handler;
Expand Down Expand Up @@ -377,6 +377,61 @@ public function recordCustomEvent($name, array $attributes)
return $this->call('newrelic_record_custom_event', array($name, $attributes));
}

/**
* Create a newrelic distributed trace payload.
* {@link https://docs.newrelic.com/docs/agents/php-agent/features/distributed-tracing-php#manual}
*
* @return newrelic\DistributedTracePayload|false
*/
public function createDistributedTracePayload()
{
if ($this->handler->isDistributedTracingEnabled()) {
return $this->call('newrelic_create_distributed_trace_payload');
}

return false;
}

/**
* Accept a distributed trace payload.
*
* Generate a payload using:
* $payload = newrelic_create_distributed_trace_payload();
* $textPayload = $payload->Text();
*
* @param string $textPayload
*
* @return bool
*/
public function acceptDistributedTracePayload($textPayload)
{
if ($this->handler->isDistributedTracingEnabled()) {
return $this->call('newrelic_accept_distributed_trace_payload', array($textPayload));
}

return false;
}

/**
* Accept a distributed trace http safe payload.
*
* Generate an http safe payload using:
* $payload = newrelic_create_distributed_trace_payload();
* $httpSafePayload = $payload->httpSafe();
*
* @param string $httpSafePayload
*
* @return bool
*/
public function acceptDistributedTracePayloadHttpSafe($httpSafePayload)
{
if ($this->handler->isDistributedTracingEnabled()) {
return $this->call('newrelic_accept_distributed_trace_payload_httpsafe', array($httpSafePayload));
}

return false;
}

/**
* Call the named method with the given params. Return false if the NewRelic PHP agent is not available.
*
Expand Down
19 changes: 17 additions & 2 deletions tests/Handler/DefaultHandlerTest.php
Expand Up @@ -9,10 +9,11 @@ class DefaultHandlerTest extends TestCase
{
public function testImplementsInterface()
{
$handler = new DefaultHandler();
$handler = new DefaultHandler(false);

$this->assertInstanceOf('Intouch\Newrelic\Handler\Handler', $handler);
}

public function testHandleCallsFunctionWithArguments()
{
$functionName = 'strpos';
Expand All @@ -22,10 +23,24 @@ public function testHandleCallsFunctionWithArguments()
0
);

$handler = new DefaultHandler();
$handler = new DefaultHandler(false);

$expected = call_user_func_array($functionName, $arguments);

$this->assertSame($expected, $handler->handle($functionName, $arguments));
}

public function testIsDistributedTracingEnabled()
{
$handler = new DefaultHandler(true);

$this->assertTrue($handler->isDistributedTracingEnabled());
}

public function testIsDistributedTracingEnabledFalse()
{
$handler = new DefaultHandler(false);

$this->assertFalse($handler->isDistributedTracingEnabled());
}
}
7 changes: 7 additions & 0 deletions tests/Handler/NullHandlerTest.php
Expand Up @@ -27,4 +27,11 @@ public function testHandleReturnsFalse()

$this->assertFalse($handler->handle($functionName, $arguments));
}

public function testIsDistributedTracingEnabledReturnsFalse()
{
$handler = new NullHandler();

$this->assertFalse($handler->isDistributedTracingEnabled());
}
}
100 changes: 100 additions & 0 deletions tests/NewrelicTest.php
Expand Up @@ -400,6 +400,106 @@ public function testStartTransaction()
$this->assertSame($result, $agent->startTransaction($name, $licence));
}

public function testCreateDistributedTracePayload()
{
$result = true;

$handler = $this->getHandlerSpy(
'newrelic_create_distributed_trace_payload',
array(),
$result
);
$handler->expects($this->once())
->method('isDistributedTracingEnabled')
->willReturn(true);

$agent = new Newrelic(false, $handler);

$this->assertSame($result, $agent->createDistributedTracePayload());
}

public function testCreateDistributedTracePayloadNotEnabled()
{
$handler = $this->getHandlerMock();
$handler->expects($this->once())
->method('isDistributedTracingEnabled')
->willReturn(false);

$agent = new Newrelic(false, $handler);

$this->assertFalse($agent->createDistributedTracePayload());
}

public function testAcceptDistributedTracePayload()
{
$payload = 'payload';
$result = true;

$handler = $this->getHandlerSpy(
'newrelic_accept_distributed_trace_payload',
array(
$payload,
),
$result
);
$handler->expects($this->once())
->method('isDistributedTracingEnabled')
->willReturn(true);

$agent = new Newrelic(false, $handler);

$this->assertSame($result, $agent->acceptDistributedTracePayload($payload));
}

public function testAcceptDistributedTracePayloadNotEnabled()
{
$payload = 'payload';

$handler = $this->getHandlerMock();
$handler->expects($this->once())
->method('isDistributedTracingEnabled')
->willReturn(false);

$agent = new Newrelic(false, $handler);

$this->assertFalse($agent->acceptDistributedTracePayload($payload));
}

public function testAcceptDistributedTracePayloadHttpSafe()
{
$payload = 'payload';
$result = true;

$handler = $this->getHandlerSpy(
'newrelic_accept_distributed_trace_payload_httpsafe',
array(
$payload,
),
$result
);
$handler->expects($this->once())
->method('isDistributedTracingEnabled')
->willReturn(true);

$agent = new Newrelic(false, $handler);

$this->assertSame($result, $agent->acceptDistributedTracePayloadHttpSafe($payload));
}

public function testAcceptDistributedTracePayloadHttpSafeNotEnabled()
{
$payload = 'payload';

$handler = $this->getHandlerMock();
$handler->expects($this->once())
->method('isDistributedTracingEnabled')
->willReturn(false);

$agent = new Newrelic(false, $handler);

$this->assertFalse($agent->acceptDistributedTracePayloadHttpSafe($payload));
}

/**
* @return bool
*/
Expand Down

0 comments on commit 7cc7a8d

Please sign in to comment.