Coder Social home page Coder Social logo

laravel-newrelic's Introduction

Laravel 5 NewRelic Service Provider

Laravel Version Package Tag Supported
5.x.x 2.2.x yes
5.2.x 2.1.x yes
5.1.x 2.0.x yes
5.0.x 2.0.x no

see below for Laravel 4.x support

Installation

See the table above for package version information, and change the version below accordingly.

Using composer, run:

composer require intouch/laravel-newrelic:"~2.0"

Or add intouch/laravel-newrelic to your composer requirements:

"require": {
    "intouch/laravel-newrelic": "~2.0"
}

... and then run composer install

Once the package is installed, open your config/app.php configuration file and locate the providers key. Add the following line to the end:

Intouch\LaravelNewrelic\NewrelicServiceProvider::class,

Optionally, locate the aliases key and add the following line:

'Newrelic' => Intouch\LaravelNewrelic\Facades\Newrelic::class,

Finally, publish the default configuration (it will end up in config/newrelic.php):

php artisan vendor:publish --provider="Intouch\LaravelNewrelic\NewrelicServiceProvider"

Configuration

Once the configuration from the package if published, see config/newrelic.php for configuration options and descriptions.

Transaction Names

Naming is done by replacing tokens in a name_provider string with formatted output collected from the application. The newrelic.name_provider config parameter holds this string - note that non-token string parts are left as-is.

Token Description Example
{controller} Controller / Action name App\Http\Controllers\MyController@action
{method} HTTP Verb GET, POST
{route} Route Name if named, otherwise {controller} auth.login
{path} Registered route path /users/{id?}
{uri} Request URI path /users/12345

The default newrelic.name_provider string is '{uri} {route}'.

Eloquent Model Observers

There are two observer classes for monitoring your Eloquent models, the NewrelicCountingObserver and the NewrelicTimingObserver. As their names suggest, one counts the number of times observable model events happen and the other gathers their timings (in milliseconds). These recorded metrics will show up in your NewRelic Custom Metrics.

The NewrelicCountingObserver can be used for any observable model events, including your custom events. The NewrelicTimingObserver currently only supports the built-in Eloquent observable events (see Model Events in the Laravel documentation).

Using the observers is simple - wherever you choose to register your model observers, simply add:

User::observe(new \Intouch\LaravelNewrelic\Observers\NewrelicTimingObserver() );
User::observe(new \Intouch\LaravelNewrelic\Observers\NewrelicCountingObserver() );

... assuming you want to observe the User model.

Both observers take two optional parameters to their constructors: $name and $care. $name is the name you want to give to your custom metric, and if unset will default to the class name of the model object it is observing. If you want to change the $care array without changing the naming, simply pass null as the first constructor argument.

$care is an array of event names you want to care about. This differs slightly between the Counting and Timing observers. For the Counting observer, any event can be counted independently. For the Timing observer, it uses the difference in time between saving and saved to submit the metric, so only the after-operation events can be observed: created, saved, updated, deleted, restored. This is also why custom observable events are not supported for the Timing observer (yet ... working on it, we're happy to take PRs).

Per NewRelic's "best practice" suggestions, all metric names are prefaced with 'Custom/'. The Counting observer also adds 'Counts/' to the name, while the Timing observer adds 'Timing/' to the name. Both observers append the event name to the end of the metric name. Take as an example, using the Counting observer on the User model monitoring the created event - the name would be: Custom/Counts/App/User/created (where App/User is the namespaced class name of the observed model with slashes reversed for NewRelic metric paths, or will be whatever you set in $name if supplied).

It is safe to run these observers in integration tests or interactive test environments as long as newrelic.throw_if_not_installed is set to false. Then if the NewRelic PHP Agent is not installed in that environment, the custom metrics will simply not be recorded. If the NewRelic PHP Agent is installed in that environment, the metrics will be recorded.

The default events both observers care about are: created, saved, updated, deleted, restored.

NOTE: To use the observers, the Newrelic Facade must be loaded in your application configuration, not just the Service Provider.

NOTE: NewRelic restricts the total number of custom metrics you can have to 2000, and recommends less than 1000.

Example Custom Metrics Dashboard

dashboard

Basic Use

This package includes a Facade to the Intouch/Newrelic class.
Any of its methods may be accessed as any other Facade is accessed, for example:

App::after( function() {
    Newrelic::setAppName( 'MyApp' );
} );

... would set the NewRelic App Name to 'MyApp'

Laravel 4.x Support

Laravel Version Package Tag Supported
4.2.x 1.1.5 no
4.1.x 1.1.5 no
4.0.x 1.0.4 no
*we will review PRs for unsupported versions, but we don't use those versions in production ourselves so we aren't
testing / working on that*

Issues

Before opening an issues for data not reporting in the format you have configured, please check your NewRelic PHP Agent logs and please see: https://discuss.newrelic.com/t/php-agent-4-19-0-disabled-3rd-party-service-provider-incorrectly/1666

If that hasn't cleared things up, please open an issue here or send us a PR.

laravel-newrelic's People

Contributors

alies-dev avatar altrim avatar andrioli avatar apancutt avatar bryceray1121 avatar danhunsaker avatar danwall avatar daper avatar emmanuelgautier avatar hootlex avatar lukewaite avatar mosanger avatar paunin avatar philonl avatar pleckey avatar tomschlick avatar weotch avatar wins1908 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-newrelic's Issues

Argument 1 passed to NewrelicServiceProvider must be an instance of JobProcessed, JobProcessing given

Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Intouch\LaravelNewrelic\NewrelicServiceProvider::Intouch\LaravelNewrelic{closure}() must be an instance of Illuminate\Queue\Events\JobProcessed, instance of Illuminate\Queue\Events\JobProcessing given, called in /home/www/releases/1548843103/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php on line 360 in /home/forge/interaction-design.org/releases/1548843103/vendor/intouch/laravel-newrelic/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php:95

Laravel version: 5.7.23
Package version: 2.2.1

Problem caused by a new release, PR #48

I need that folk already quick-fixed this issue on their forks, but we need a robust fix

Problem with different router

Hello,

I'm using dingo/api as my default router. It only pushes POST index.php, GET index.php, PUT index.php to Newrelic. I've seen your library and wanted to use for fixing this issue. But it threw another exception. Here is the trace is below.

So, i know that you dont support for dingo/api or another custom provider. But any help would be appreciated.

Thanks.

Stack trace:
#0 /var/www/vendor/intouch/laravel-newrelic/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php(102): Intouch\LaravelNewrelic\NewrelicServiceProvider->getPath()
#1 /var/www/vendor/intouch/laravel-newrelic/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php(78): Intouch\LaravelNewrelic\NewrelicServiceProvider->getTransactionName()
#2 /var/www/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(348): Intouch\LaravelNewrelic\NewrelicServiceProvider->Intouch\LaravelNewrelic\{closure}(Object(Illuminate\Routing\Events\RouteMatched))
#3 /var/www/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(199): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('Illuminate\\Rout...', Array)
#4 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php(533): Illuminate\Events\Dispatcher->dispatch('Illuminate\\Rout...')
#5 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php(513): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\Request))
#6 /var/www/vendor/dingo/api/src/Routing/Adapter/Laravel.php(81): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))
#7 /var/www/vendor/dingo/api/src/Routing/Router.php(503): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\Request), 'v1')
#8 /var/www/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))
#9 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(114): Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware\{closure}(Object(Dingo\Api\Http\Request))
#10 /var/www/vendor/barryvdh/laravel-cors/src/HandleCors.php(36): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
#11 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Barryvdh\Cors\HandleCors->handle(Object(Dingo\Api\Http\Request), Object(Closure))
#12 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
#13 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))
#14 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
#15 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))
#16 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
#17 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Dingo\Api\Http\Request), Object(Closure))
#18 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
#19 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))
#20 /var/www/app/Http/Middleware/ContentManipulateMiddleware.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
#21 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): App\Http\Middleware\ContentManipulateMiddleware->handle(Object(Dingo\Api\Http\Request), Object(Closure))
#22 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
#23 /var/www/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#24 /var/www/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))
#25 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Dingo\Api\Http\Middleware\Request->handle(Object(Dingo\Api\Http\Request), Object(Closure))
#26 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#27 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#29 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#30 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(234): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#31 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(57): Illuminate\Foundation\Testing\TestCase->call('GET', '/api/v1/health-...', Array, Array, Array, Array)
#32 /var/www/tests/Feature/Api/ApiStatusCheckTest.php(14): Illuminate\Foundation\Testing\TestCase->get('/api/v1/health-...')
#33 [internal function]: Tests\Feature\Api\ApiStatusCheckTest->testExample()
#34 /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php(1054): ReflectionMethod->invokeArgs(Object(Tests\Feature\Api\ApiStatusCheckTest), Array)
#35 /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php(905): PHPUnit_Framework_TestCase->runTest()
#36 /var/www/vendor/phpunit/phpunit/src/Framework/TestResult.php(686): PHPUnit_Framework_TestCase->runBare()
#37 /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php(860): PHPUnit_Framework_TestResult->run(Object(Tests\Feature\Api\ApiStatusCheckTest))
#38 /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#39 /var/www/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(517): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#40 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(186): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array, true)
#41 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(116): PHPUnit_TextUI_Command->run(Array, true)
#42 /var/www/vendor/phpunit/phpunit/phpunit(52): PHPUnit_TextUI_Command::main()
#43 {main}  

Laravel 5.2 support

There is no more Illuminate\Routing\Router::before filter, it was deprecated in 5.1 and removed in 5.2. So NewrelicServiceProvider::registerNamedTransactions is broken.

Transactions not showing up

Setting the app name in an App::before() closure seems to work (I can see all my app names in the New Relic panel) but none of the apps have any transactions.

I already tried setting the $xmit parameter to true while setting the app name but that doesn't seem to help.

Any ideas?

Support Laravel 7

There is a "illuminate/support": "5.*" dependency that doesn't allow to install package for Laravel 7 projects

Config file should have warning about the default settings

Thanks for the awesome package.

I stumbled upon a problematic situation with Newrelic as I ended up using the default values for 'name_provider' (=> '{uri} {route}') in the config. It's nice way to use NR with Laravel, but in high-volume + lots of changing uris, we got contacted by Newrelic and they informed us that there's "unusually large number of unique transaction names".

So how about adding additional warning or suggestion to the config file which says something like "If you have lots of different uris, drop the {uri} from the config as Newrelic doesn't recommend over 1000 transaction names"? Probably saves some work from people using your package and from Newrelic as well :)

Not working in Lumen 5.4

I've published the config file, added the Middleware, registered the ServiceProvider and added the Newrelic facade. However none of my "Transactions" are being recorded in APM. There are no errors in my application when I check my log files.

Not working with Laravel 5.5 & PHP 7.1 (fpm)

PHP execution trace follows...
#0 Illuminate\Routing\Router->prepareResponse() 
#1 Illuminate\Routing\Router->php-fpm: pool default (+0xe15b5)[0x7f9cd85e15b5]
PHP execution trace follows...
##2 0Illuminate\Routing\Pipeline ->Illuminate\Routing\RouterIlluminate\Routing\{closure}()->prepareResponse

Laravel not showing events

Hi, I followed all the instructions and set the configuration as
'auto_name_transactions' => true

but NewRelic only tracks background processes and errors. And nothing else.
I only have an API (to communicate with my app) and the routes are defined as this:

Route::controller('/api', 'ApiController');

Any thoughts?

Error while installing the package

I just created a Laravel 5 project and added this library, now when I run php artisan I get this error:

[BadMethodCallException]
Method after does not exist.

Wrong Publish Config Artisan Command

The readme says:

php artisan publish:config intouch/laravel-newrelic

For me (using Laravel 4.2.17), I had to run:

php artisan config:publish intouch/laravel-newrelic

[Request] Enable/Disable based upon environment config.

It'd be great to conditionally load the service provider based on an environment variable, that way avoiding the

 [BadMethodCallException]
  Method after does not exist.

error that shows up when this is installed without the New Relic PHP agent being installed.

Automatic transaction names

Since Laravel 4.1 and the getCurrentRouteName() confusion in other issues I have also noticed another problem and would like to start a discussion on improving this functionality.

In 4.0, currentRouteName() used to return either the name of the route (e.g. "home") or the HTTP verb followed by the request path (e.g. "get /"). This would work on both controller methods as well as route closures.

In 4.1, this functionality has been changed so that currentRouteName() returns either the route name (e.g. "home") or null. If a route name is not defined, it returns null - this is the case for both controllers and route closures.

Request::path() returns the current requests path, without the HTTP verb.

Route::currentRouteAction() returns the controller method in the format "HomeController@getIndex" which is my preferred method as it does not rely on the route being named. One caveat with this is that it also returns null on route closures.

Perhaps we could have some sort of fallback if currentRouteAction() returns null, we then use Request::path()?

These are just my thoughts, would love to hear feedback from others.

Lumen support

Do you have plan to support Lumen ?

Currently it does not work because of NewrelicServiceProvider and $app['router'] usage.

tag queue support

Hello,
the support of queue ais nice, could you tag it, to make it usable?

thanks

Adds a new option to log errors in newrelic when laravel debug is set to false.

In order to make the errors appear in the Newrelic dashboard when the debug is set to false I need to add the follow snippet to the app/start/global.php file:

App::error(function(Exception $exception, $code)
{
    Log::error($exception);
    if (App::environment('production'))
    {
        $newrelic = new Intouch\Newrelic\Newrelic;
        $newrelic->noticeError($exception);
    }

});

What you think about this be done behind the scenes and bring this as a new option in the package config file?

Not working in Laravel 5.4

Call to undefined method Illuminate\Foundation\Application::share()
in Intouch/LaravelNewrelic/NewrelicServiceProvider.php line 54

Fatal error on PHP 5.3

PHP Fatal error: Using $this when not in object context in F:\projects\Altitude
\Altitude-Core\vendor\intouch\laravel-newrelic\src\Intouch\LaravelNewrelic\Larav
elNewrelicServiceProvider.php on line 84

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.