Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,21 @@ NR_PHP_WRAPPER(nr_laravel_queue_queue_createpayload) {
}
NR_PHP_WRAPPER_END

/*
* 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_TXN, "Ending Laravel Horizon Transaction");
}
NR_PHP_WRAPPER_END

void nr_laravel_queue_enable(TSRMLS_D) {
/*
* Hook the command class that implements Laravel's queue:work command so
Expand Down Expand Up @@ -871,6 +886,19 @@ 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);

/*
* 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

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";
}
}
}
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();
60 changes: 60 additions & 0 deletions tests/integration/frameworks/laravel/test_horizon_happy_path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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.
*/

/*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
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();
Loading