Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #35 from philkra/development
Browse files Browse the repository at this point in the history
Use `REQUEST_TIME_FLOAT` as Transaction starting Time
  • Loading branch information
philkra authored Feb 22, 2019
2 parents 94851ac + ccb94bd commit 8879fbc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ The following environment variables are supported in the default configuration:
You may also publish the `elastic-apm.php` configuration file to change additional settings:

```bash
php artisan vendor:publish --tag=config --provider="PhilKra\ElasticApmLaravel\Providers\ElasticApmService
Provider"
php artisan vendor:publish --tag=config
```

Once published, open the `config/elastic-apm.php` file and review the various settings.

### Laravel Test Setup

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).

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.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"name":"philkra/elastic-apm-laravel",
"keywords":[
"laravel",
Expand All @@ -16,7 +16,7 @@
"illuminate/http":"5.5.x|5.6.x|5.7.x",
"illuminate/routing":"5.5.x|5.6.x|5.7.x",
"illuminate/support":"5.5.x|5.6.x|5.7.x",
"philkra/elastic-apm-php-agent":"6.*.*",
"philkra/elastic-apm-php-agent":">=6.5.3",
"ramsey/uuid":"^3.0"
},
"require-dev":{
Expand Down
25 changes: 8 additions & 17 deletions src/Middleware/RecordTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
use Closure;
use Illuminate\Support\Facades\Log;
use PhilKra\Agent;
use PhilKra\Helper\Timer;

class RecordTransaction
{
/**
* @var \PhilKra\Agent
*/
protected $agent;
/**
* @var Timer
*/
private $timer;

/**
* RecordTransaction constructor.
* @param Agent $agent
*/
public function __construct(Agent $agent)
public function __construct(Agent $agent, Timer $timer)
{
$this->agent = $agent;
$this->timer = $timer;
}

/**
Expand Down Expand Up @@ -60,9 +66,7 @@ public function handle($request, Closure $next)
$transaction->setTransactionName($this->getRouteUriTransactionName($request));
}

$transaction->stop(
$this->getDuration(LARAVEL_START)
);
$transaction->stop($this->timer->getElapsedInMilliseconds());

return $response;
}
Expand Down Expand Up @@ -118,19 +122,6 @@ protected function getRouteUriTransactionName(\Illuminate\Http\Request $request)
);
}

/**
* @param $start
*
* @return float
*/
protected function getDuration($start): float
{
$diff = microtime(true) - $start;
$corrected = $diff * 1000; // convert to miliseconds

return round($corrected, 3);
}

/**
* @param array $headers
*
Expand Down
13 changes: 12 additions & 1 deletion src/Providers/ElasticApmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
use Illuminate\Support\ServiceProvider;
use PhilKra\Agent;
use PhilKra\ElasticApmLaravel\Contracts\VersionResolver;
use PhilKra\Helper\Timer;

class ElasticApmServiceProvider extends ServiceProvider
{
/** @var float */
private $startTime;
/** @var Timer */
private $timer;

/**
* Bootstrap the application services.
*
Expand Down Expand Up @@ -56,6 +62,11 @@ public function register()
);
});

$this->startTime = $this->app['request']->server('REQUEST_TIME_FLOAT') ?? microtime(true);
$this->timer = new Timer($this->startTime);

$this->app->instance(Timer::class, $this->timer);

$this->app->alias(Agent::class, 'elastic-apm');
$this->app->instance('query-log', collect([]));
}
Expand Down Expand Up @@ -162,7 +173,7 @@ protected function listenForQueries()
$query = [
'name' => 'Eloquent Query',
'type' => 'db.mysql.query',
'start' => round((microtime(true) - $query->time / 1000 - LARAVEL_START) * 1000, 3),
'start' => round((microtime(true) - $query->time / 1000 - $this->startTime) * 1000, 3),
// calculate start time from duration
'duration' => round($query->time, 3),
'stacktrace' => $stackTrace,
Expand Down

0 comments on commit 8879fbc

Please sign in to comment.