You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 3, 2020. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+13-70Lines changed: 13 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,11 +18,15 @@ This package is a continuation of the excellent work done by [philkra](https://g
18
18
composer require nipwaayoni/elastic-apm-laravel
19
19
```
20
20
21
+
The [nipwaayoni/elastic-apm-php-agent](https://github.com/nipwaayoni/elastic-apm-php-agent) no longer includes and http client. You must ensure a [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible implementation is available. Please see the [agent install guide](https://github.com/nipwaayoni/elastic-apm-php-agent/blob/master/docs/install.md) for more information.
22
+
21
23
## Service Provider
22
24
23
25
### Laravel
24
26
25
-
No need to register service provider manually. It is registered automatically by [package discovery](https://laravel.com/docs/5.6/packages#package-discovery).
27
+
If using Laravel >=5.5, registration is done automatically by [package discovery](https://laravel.com/docs/7.x/packages#package-discovery).
28
+
29
+
This package is not tested or asserted to work with Laravel <5.5.
26
30
27
31
### Lumen
28
32
@@ -73,92 +77,31 @@ based process. We hope to address those issues in a future release.
73
77
74
78
Spans occur within a Transaction. Spans represent events within the Transaction. Queries made through Laravel's
75
79
database layer are automatically added to the Transaction. You can add your own Span events using the `EventTimer`
76
-
class from this package.
80
+
class from this package. See the docs for [creating spans](docs/creating_spans.md).
77
81
78
82
Nested Spans are not supported by this package yet.
79
83
80
-
#### Laravel
81
-
82
-
Acquire the EventTimer object from the container.
83
-
84
-
```php
85
-
class MyClass
86
-
{
87
-
/**
88
-
* @var EventTimer
89
-
*/
90
-
private $eventTimer;
91
-
92
-
public function __construct(EventTimer $eventTimer)
93
-
{
94
-
$this->eventTimer = $eventTimer;
95
-
}
96
-
97
-
public function runSomeRequest(int $number): AnObject
98
-
{
99
-
$event = $this->eventTimer->begin('Request the data');
100
-
$result = $this->someMethod($number);
101
-
$this->eventTimer->finish($event);
102
-
103
-
return new AnObject($result);
104
-
}
105
-
}
106
-
```
107
-
108
-
#### Lumen
109
-
110
-
pending
111
-
112
84
### Error Events
113
85
114
-
#### Laravel
115
-
116
-
In `app/Exceptions/Handler`, add the following to the `report` method:
117
-
118
-
```php
119
-
ElasticApm::captureThrowable($exception);
120
-
```
121
-
122
-
Make sure to import the facade at the top of your file:
123
-
124
-
```php
125
-
use ElasticApm;
126
-
```
127
-
128
-
#### Lumen
129
-
130
-
pending
86
+
The APM service defines exception events as a valid type. Exceptions in your application can be sent to APM in addition to any normal exception handling. See the docs for [exceptions](docs/exceptions.md).
131
87
132
88
## Agent Configuration
133
89
134
-
### Laravel
135
-
136
-
The following environment variables are supported in the default configuration:
90
+
You can use a number of environment settings to influence the behavior of this package. At a minimum, you must set the APM server URL and, if applicable, the secret toke:
137
91
138
92
| Variable | Description |
139
93
|-------------------|-------------|
140
-
|APM_ACTIVE |`true` or `false` defaults to `true`. If `false`, the agent will collect, but not send, transaction data. |
141
-
|APM_APPNAME | Name of the app as it will appear in APM. |
142
-
|APM_APPVERSION | Version of the app as it will appear in APM. |
143
94
|APM_SERVERURL | URL to the APM intake service. |
144
95
|APM_SECRETTOKEN | Secret token, if required. |
145
-
|APM_APIVERSION | APM API version, defaults to `v1` (only v1 is supported at this time). |
146
-
|APM_USEROUTEURI |`true` or `false` defaults to `false`. The default behavior is to record the URL as sent in the request. This can result in excessive unique entries in APM. Set to `true` to have the agent use the route URL instead. |
147
-
|APM_QUERYLOG |`true` or `false` defaults to 'true'. Set to `false` to completely disable query logging, or to `auto` if you would like to use the threshold feature. |
148
-
|APM_THRESHOLD | Query threshold in milliseconds, defaults to `200`. If a query takes longer then 200ms, we enable the query log. Make sure you set `APM_QUERYLOG=auto`. |
149
-
|APM_BACKTRACEDEPTH | Defaults to `25`. Depth of backtrace in query span. |
150
-
|APM_RENDERSOURCE | Defaults to `true`. Include source code in query span. |
151
96
152
-
You may also publish the `elastic-apm.php`configuration file to change additional settings:
97
+
Refer to the [configuration docs](docs/configuration.md) for more information.
153
98
154
-
```bash
155
-
php artisan vendor:publish --tag=config
156
-
```
99
+
### HTTP Client Customization
157
100
158
-
Once published, open the `config/elastic-apm.php` file and review the various settings.
101
+
It is no longer possible to provide HTTP client options through the APM PHP Agent configuration. If you need to customize the HTTP client, you must implement and configure a suitable client object and properly register it with the Laravel service container. See the "HTTP Client Configuation" section of the [configuration docs](docs/configuration.md).
159
102
160
-
###Laravel Test Setup
103
+
## Laravel Test Setup
161
104
162
105
Laravel provides classes to support running unit and feature tests with PHPUnit. In most cases, you will want to explicitly disable APM during testing since it is enabled by default. Refer to the Laravel documentation for more information (https://laravel.com/docs/5.7/testing).
163
106
164
-
Because the APM agent checks it's active status using a strict boolean type, you must ensure your `APM_ACTIVE` value is a boolean `false` rather than simply a falsy value. The best way to accomplish this is to create an `.env.testing` file and include `APM_ACTIVE=false`, along with any other environment settings required for your tests. This file should be safe to include in your SCM.
107
+
Because the APM agent checks its active status using a strict boolean type, you must ensure your `APM_ACTIVE` value is a boolean `false` rather than simply a falsy value. The best way to accomplish this is to create an `.env.testing` file and include `APM_ACTIVE=false`, along with any other environment settings required for your tests. This file should be safe to include in your SCM.
1. The [Elastic APM PHP Agent](https://github.com/nipwaayoni/elastic-apm-php-agent) minimum version has been updated.
6
+
2. It is no longer recommended to call `Agent::send()` in `Exceptions\Handler::report()`. This will likely result in duplicate reporting of exceptions.
7
+
3. The `elastic-apm.env` settings have been redefined. This key now contains the allowed list of environment variables to include the APM context. The application environment is now set in `elastic-apm.environment`. If you previously published the `config/elastic-apm.php` file, you must make certain it is updated.
8
+
4. The [Elastic APM PHP Agent](https://github.com/nipwaayoni/elastic-apm-php-agent) no longer provides an HTTP client implementation. Please see the [agent install guide](https://github.com/nipwaayoni/elastic-apm-php-agent/blob/master/docs/install.md) for more information. The configuration passed to the `Agent` can no longer contain the `httpClient` key. If you previously published the `config/elastic-apm.php` file, you must remove that key.
9
+
5. The method of creating span events has changed. See the [creating spans](creating_spans.md) documentation.
The following environment variables are supported in the default configuration:
4
+
5
+
| Variable | Description |
6
+
|-------------------|-------------|
7
+
|APM_ACTIVE |`true` or `false` defaults to `true`. If `false`, the agent will collect, but not send, transaction data. |
8
+
|APM_APPNAME | Name of the app as it will appear in APM. |
9
+
|APM_APPVERSION | Version of the app as it will appear in APM. |
10
+
|APM_SERVERURL | URL to the APM intake service. |
11
+
|APM_SECRETTOKEN | Secret token, if required. |
12
+
|APM_APIVERSION | APM API version, defaults to `v2` (only v2 is supported at this time). |
13
+
|APM_USEROUTEURI |`true` or `false` defaults to `false`. The default behavior is to record the URL as sent in the request. This can result in excessive unique entries in APM. Set to `true` to have the agent use the route URL instead. |
14
+
|APM_QUERYLOG |`true` or `false` defaults to 'true'. Set to `false` to completely disable query logging, or to `auto` if you would like to use the threshold feature. |
15
+
|APM_THRESHOLD | Query threshold in milliseconds, defaults to `200`. If a query takes longer then 200ms, we enable the query log. Make sure you set `APM_QUERYLOG=auto`. |
16
+
|APM_BACKTRACEDEPTH | Defaults to `25`. Depth of backtrace in query span. |
17
+
|APM_RENDERSOURCE | Defaults to `true`. Include source code in query span. |
18
+
19
+
You may also publish the `elastic-apm.php` configuration file to change additional settings:
20
+
21
+
```bash
22
+
php artisan vendor:publish --tag=config
23
+
```
24
+
25
+
Once published, open the `config/elastic-apm.php` file and review the various settings.
26
+
27
+
## HTTP Client Configuration
28
+
29
+
If you need to customize the HTTP client, you must create a [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible implementation and bind it in the Laravel service container. For now, we will use a GuzzleHttp adapter from the PHP-HTTP project.
The following example demonstrates how to create a GuzzleHttp client that will not verify server certificates. Once you create the client, bind it in the service container under the `ElasticApmHttpClient` abstract.
36
+
37
+
```php
38
+
$this->app->bind('ElasticApmHttpClient', function () {
39
+
// Create the configured client
40
+
$client = new \GuzzleHttp\Client([
41
+
'verify' => false,
42
+
// other client options
43
+
]);
44
+
45
+
// Wrap the client object in the adapter and return it
46
+
return new \Http\Adapter\Guzzle6\Client($client);
47
+
});
48
+
49
+
```
50
+
51
+
If the service container has a binding for `ElasticApmHttpClient`, the concrete implementation will be retrieved and passed into the `Agent`.
The current event implementation creates a single APM transaction to represent the Laravel Request/Response. You can create APM span events to represent discrete actions within the transaction. The `\Nipwaayoni\ElasticApmLaravel\Apm\EventTimer` class facilitates making spans.
Laravel provides a convenient method for working with application exceptions which we can leverage to send Exceptions to APM. In `app/Exceptions/Handler`, add the following to the `report` method:
4
+
5
+
```php
6
+
ElasticApm::captureThrowable($exception);
7
+
```
8
+
9
+
Make sure to import the facade at the top of your file:
10
+
11
+
```php
12
+
use ElasticApm;
13
+
```
14
+
15
+
The collected Exceptions will be sent when `Agent::send()` is called in the middleware.
16
+
17
+
Note that previous versions of this package suggested calling `Agent::send()` explicitly in `Handler::report()`. The was related to APM < 7 intake and is no longer suggested as it may lead to duplicate events.
18
+
19
+
Note that the Laravel exception handler is only aware of exceptions generated after the Laravel framework is minimally bootstrapped. An error preventing the proper executation of the bootstrap process will not be captured in `Handler::report()`.
0 commit comments