Skip to content
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
28 changes: 28 additions & 0 deletions agent/fw_laravel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,21 @@ static nr_status_t nr_laravel_should_assign_generic_path(const nrtxn_t* txn,
return NR_FAILURE;
}

/*
* Ensure that the transaction is properly stopped when Laravel Horizon is being
* used by wrapping the handle method of the HorizonCommand and
* SupervisorCommand class.
*/
NR_PHP_WRAPPER(nr_laravel_horizon_end_txn) {
NR_UNUSED_SPECIALFN;
(void)wraprec;

nr_php_txn_end(1, 0 TSRMLS_CC);

nrl_verbosedebug(NRL_FRAMEWORK, "Ending Laravel Horizon Transaction");
}
NR_PHP_WRAPPER_END

void nr_laravel_enable(TSRMLS_D) {
/*
* We set the path to 'unknown' to prevent having to name routing errors.
Expand Down Expand Up @@ -1231,6 +1246,19 @@ void nr_laravel_enable(TSRMLS_D) {
nr_php_wrap_user_function_before_after_clean(
NR_PSTR("Symfony\\Component\\Console\\Application::doRun"),
nr_laravel_console_application_dorun, NULL, NULL);

/*
* The following functions have been added to ensure that the transaction
* is properly stopped when Laravel Horizon is being used. This is
* accomplished by wrapping the handle method of the HorizonCommand class and
* the SupervisorCommand class.
*/
nr_php_wrap_user_function_before_after_clean(
NR_PSTR("Laravel\\Horizon\\Console\\HorizonCommand::handle"),
nr_laravel_horizon_end_txn, NULL, NULL);
nr_php_wrap_user_function_before_after_clean(
NR_PSTR("Laravel\\Horizon\\Console\\SupervisorCommand::handle"),
nr_laravel_horizon_end_txn, NULL, NULL);
#else
nr_php_wrap_user_function(NR_PSTR("Symfony\\Component\\Console\\Application::doRun"),
nr_laravel_console_application_dorun TSRMLS_CC);
Expand Down
1 change: 0 additions & 1 deletion agent/fw_laravel_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,6 @@ void nr_laravel_queue_enable(TSRMLS_D) {
nr_php_wrap_user_function_before_after_clean(
NR_PSTR("Illuminate\\Queue\\SyncQueue::raiseAfterJobEvent"),
nr_laravel_queue_worker_raiseAfterJobEvent_before, NULL, NULL);

#else

/*
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/frameworks/laravel/mock_horizon_error_path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/* Mock enough bits of Laravel framework to test the HorizonCommand */

namespace Laravel\Horizon\Console {
class HorizonCommand {
public function handle() {
echo "Error handle function\n";
throw new \Error("Error occurred");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/* Mock enough bits of Laravel framework to test the HorizonCommand */

namespace Laravel\Horizon\Console {
class HorizonCommand {
public function handle() {
echo "Exception handle function\n";
throw new \Exception("Exception occurred");
}
}
}
16 changes: 16 additions & 0 deletions tests/integration/frameworks/laravel/mock_horizon_happy_path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/* Mock enough bits of Laravel framework to test the HorizonCommand */

namespace Laravel\Horizon\Console {
class HorizonCommand {
public function handle() {
echo "handle function\n";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/* Mock enough bits of Laravel framework to test the SupervisorCommand */

namespace Laravel\Horizon\Console {
class SupervisorCommand {
public function handle() {
echo "Error handle function\n";
throw new \Error("Error occurred");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/* Mock enough bits of Laravel framework to test the SupervisorCommand */

namespace Laravel\Horizon\Console {
class SupervisorCommand {
public function handle() {
echo "Exception handle function\n";
throw new \Exception("Exception occurred");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/* Mock enough bits of Laravel framework to test the SupervisorCommand */

namespace Laravel\Horizon\Console {
class SupervisorCommand {
public function handle() {
echo "handle function\n";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
This test ensures that when the newrelic.special = disable_laravel_queue INI setting
is set, the transaction is still properly stopped when HorizonCommand::handle is executed.
This test verifies this behavior by first making sure the transaction exists and the
transaction trace is not empty. Then it calls the HorizonCommand::handle method and
verifies that the transaction has been stopped by checking that there was no harvest received.
*/

/*SKIPIF
<?php
if (version_compare(PHP_VERSION, "8.0", "<")) {
die("skip: PHP < 8.0 not supported\n");
}
*/

/*INI
newrelic.framework = laravel
newrelic.special = disable_laravel_queue
*/

/*EXPECT_HARVEST
no
*/

/*EXPECT
foobar
Custom/foobar
handle function
foobar
*/

require_once(__DIR__ . '/mock_horizon_happy_path.php');
require_once(__DIR__.'/../../../include/integration.php');

use Laravel\Horizon\Console\HorizonCommand;
use NewRelic\Integration\Transaction;

function foobar() {
echo "foobar\n";
}
function get_txn() {
return new Transaction;
}

newrelic_add_custom_tracer('foobar');
foobar();

$txn = get_txn();
foreach ($txn->getTrace()->findSegmentsByName('Custom/foobar') as $segment) {
echo $segment->name;
echo "\n";
}

$horizon = new HorizonCommand();
$horizon->handle();
foobar();
70 changes: 70 additions & 0 deletions tests/integration/frameworks/laravel/test_horizon_error_path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
Ensure that when HorizonCommand::handle is executed, the transaction is properly stopped.
This test verifies this behavior by first making sure the transaction exists and the
transaction trace is not empty. Then it calls the HorizonCommand::handle method and
verifies that the transaction has been stopped by checking that there was no harvest received.
It also verifies that no traced errors and no error events were recorded.
*/

/*SKIPIF
<?php
if (version_compare(PHP_VERSION, "8.0", "<")) {
die("skip: PHP < 8.0 not supported\n");
}
*/

/*INI
newrelic.framework = laravel
*/

/*EXPECT_HARVEST
no
*/

/*EXPECT_TRACED_ERRORS
null
*/

/*EXPECT_ERROR_EVENTS
null
*/

/*EXPECT_REGEX
foobar
Custom/foobar
Error handle function

Fatal error: Uncaught Error: Error occurred in .*mock_horizon_error_path.php:.*
*/

require_once(__DIR__ . '/mock_horizon_error_path.php');
require_once(__DIR__.'/../../../include/integration.php');

use Laravel\Horizon\Console\HorizonCommand;
use NewRelic\Integration\Transaction;

function foobar() {
echo "foobar\n";
}
function get_txn() {
return new Transaction;
}

newrelic_add_custom_tracer('foobar');
foobar();

$txn = get_txn();
foreach ($txn->getTrace()->findSegmentsByName('Custom/foobar') as $segment) {
echo $segment->name;
echo "\n";
}

$horizon = new HorizonCommand();
$horizon->handle();
foobar();
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
Ensure that when HorizonCommand::handle is executed, the transaction is properly stopped.
This test verifies this behavior by first making sure the transaction exists and the
transaction trace is not empty. Then it calls the HorizonCommand::handle method and
verifies that the transaction has been stopped by checking that there was no harvest received.
It also verifies that no traced errors and no error events were recorded.
*/

/*SKIPIF
<?php
if (version_compare(PHP_VERSION, "8.0", "<")) {
die("skip: PHP < 8.0 not supported\n");
}
*/

/*INI
newrelic.framework = laravel
*/

/*EXPECT_HARVEST
no
*/

/*EXPECT_TRACED_ERRORS
null
*/

/*EXPECT_ERROR_EVENTS
null
*/

/*EXPECT_REGEX
foobar
Custom/foobar
Exception handle function

Fatal error: Uncaught Exception: Exception occurred in .*mock_horizon_exception_path.php:.*
*/

require_once(__DIR__ . '/mock_horizon_exception_path.php');
require_once(__DIR__.'/../../../include/integration.php');

use Laravel\Horizon\Console\HorizonCommand;
use NewRelic\Integration\Transaction;

function foobar() {
echo "foobar\n";
}
function get_txn() {
return new Transaction;
}

newrelic_add_custom_tracer('foobar');
foobar();

$txn = get_txn();
foreach ($txn->getTrace()->findSegmentsByName('Custom/foobar') as $segment) {
echo $segment->name;
echo "\n";
}

$horizon = new HorizonCommand();
$horizon->handle();
foobar();
Loading