Skip to content

Commit

Permalink
Merge pull request #4 from bethinkpl/IT-2038-sampling
Browse files Browse the repository at this point in the history
IT-2038 | introduce APM transactions sampling
  • Loading branch information
Maciej Brencz authored Jan 9, 2020
2 parents 0e27b79 + 78e6f95 commit 06e390f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ composer require bethinkpl/elastic-apm-laravel
## Additional features

* `X-Requested-By` HTTP request header value is set as `labels.requested_by` APM transaction field (`end-user-ajax` value is used when `X-Requested-With: XMLHttpRequest` is set)
* sampling of transactions reported to APM
* tracking of HTTP requests performed using GuzzleHttp library. Simply add the following middleware to your Guzzle client:

```php
Expand Down Expand Up @@ -115,6 +116,7 @@ The following environment variables are supported in the default configuration:
|APM_BACKTRACEDEPTH | Defaults to `25`. Depth of backtrace in query span. |
|APM_RENDERSOURCE | Defaults to `true`. Include source code in query span. |
|APM_HTTPLOG | Defaults to `true`. Will record HTTP requests performed via GuzzleHttp. |
|APM_SAMPLING | Defaults to `100`. Sets the percentage of transactions that will be reported to APM (ranges from 0 to 100).

You may also publish the `elastic-apm.php` configuration file to change additional settings:

Expand Down
3 changes: 3 additions & 0 deletions config/elastic-apm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// Sets whether the apm reporting should be active or not
'active' => env('APM_ACTIVE', true),

// Applies sampling of transactions to be reported to APM, defaults to 100%
'sampling' => env('APM_SAMPLING', 100),

'app' => [
// The app name that will identify your app in Kibana / Elastic APM
'appName' => env('APM_APPNAME', 'Laravel'),
Expand Down
16 changes: 11 additions & 5 deletions src/Providers/ElasticApmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ElasticApmServiceProvider extends ServiceProvider
/** @var float */
private static $lastHttpRequestStart;

/** @var bool */
private static $isSampled = true;

/**
* Bootstrap the application services.
*
Expand All @@ -41,7 +44,7 @@ public function boot()
], 'config');
}

if (config('elastic-apm.active') === true && config('elastic-apm.spans.querylog.enabled') !== false) {
if (config('elastic-apm.active') === true && config('elastic-apm.spans.querylog.enabled') !== false && self::$isSampled) {
$this->listenForQueries();
}
}
Expand All @@ -59,6 +62,10 @@ public function register()
'elastic-apm'
);

// apply transactions reporting sampling
$samplingRate = intval(config('elastic-apm.sampling')) ?: 100;
self::$isSampled = $samplingRate > mt_rand(0, 100);

$this->app->singleton(Agent::class, function ($app) {
return new Agent(
array_merge(
Expand All @@ -67,7 +74,7 @@ public function register()
'frameworkVersion' => app()->version(),
],
[
'active' => config('elastic-apm.active'),
'active' => config('elastic-apm.active') && self::$isSampled,
'httpClient' => config('elastic-apm.httpClient'),
],
$this->getAppConfig(),
Expand All @@ -88,7 +95,6 @@ public function register()

$this->app->alias(Agent::class, 'elastic-apm');
$this->app->instance('apm-spans-log', $collection);

}

/**
Expand Down Expand Up @@ -227,8 +233,8 @@ function(RequestInterface $request, array $options) {
self::$lastHttpRequestStart = microtime(true);
},
function (RequestInterface $request, array $options, PromiseInterface $promise) {
// leave early if monitoring is disabled
if (config('elastic-apm.active') !== true || config('elastic-apm.spans.httplog.enabled') !== true) {
// leave early if monitoring is disabled or when this transaction is not sampled
if (config('elastic-apm.active') !== true || config('elastic-apm.spans.httplog.enabled') !== true || !self::$isSampled) {
return;
}

Expand Down

0 comments on commit 06e390f

Please sign in to comment.