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

Commit 76965e1

Browse files
committed
Merge remote-tracking branch 'origin' into fix-active-config
2 parents 45a5ccb + c17c699 commit 76965e1

File tree

4 files changed

+115
-36
lines changed

4 files changed

+115
-36
lines changed

config/elastic-apm.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
return [
44
// Sets whether the apm reporting should be active or not
55
'active' => true,
6-
6+
77
'app' => [
88
// The app name that will identify your app in Kibana / Elastic APM
9-
'appName' => 'Some App Name',
9+
'appName' => env('APM_APPNAME', 'Laravel'),
1010

1111
// The version of your app
12-
'appVersion' => '',
12+
'appVersion' => env('APM_APPVERSION', ''),
1313
],
1414

1515
'env' => [
@@ -20,18 +20,25 @@
2020

2121
'server' => [
2222
// The apm-server to connect to
23-
'serverUrl' => 'http://127.0.0.1:8200',
23+
'serverUrl' => env('APM_SERVERURL', 'http://127.0.0.1:8200'),
2424

2525
// Token for x
26-
'secretToken' => null,
27-
26+
'secretToken' => env('APM_SECRETTOKEN', null),
27+
2828
// API version of the apm agent you connect to
29-
'apmVersion' => 'v1',
29+
'apmVersion' => env('APM_APIVERSION', 'v1'),
3030

3131
// Hostname of the system the agent is running on.
3232
'hostname' => gethostname(),
3333
],
3434

35+
'transactions' => [
36+
37+
//This option will bundle transaction on the route name without variables
38+
'use_route_uri' => false,
39+
40+
],
41+
3542
'spans' => [
3643
// Depth of backtraces
3744
'backtraceDepth'=> 25,
@@ -41,10 +48,10 @@
4148

4249
'querylog' => [
4350
// Set to false to completely disable query logging, or to 'auto' if you would like to use the threshold feature.
44-
'enabled' => true,
51+
'enabled' => env('APM_QUERYLOG', true),
4552

4653
// If a query takes longer then 200ms, we enable the query log. Make sure you set enabled = 'auto'
47-
'threshold' => 200,
54+
'threshold' => env('APM_THRESHOLD', 200),
4855
],
4956
],
5057
];

src/Contracts/VersionResolver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace PhilKra\ElasticApmLaravel\Contracts;
5+
6+
7+
interface VersionResolver
8+
{
9+
public function getVersion(): string;
10+
}

src/Middleware/RecordTransaction.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace PhilKra\ElasticApmLaravel\Middleware;
44

55
use Closure;
6-
use Illuminate\Routing\Route;
7-
use Illuminate\Support\Facades\Auth;
86
use PhilKra\Agent;
97

108
class RecordTransaction
@@ -14,15 +12,19 @@ class RecordTransaction
1412
*/
1513
protected $agent;
1614

15+
/**
16+
* RecordTransaction constructor.
17+
* @param Agent $agent
18+
*/
1719
public function __construct(Agent $agent)
1820
{
1921
$this->agent = $agent;
2022
}
2123

2224
/**
2325
* [handle description]
24-
* @param Request $request [description]
25-
* @param Closure $next [description]
26+
* @param Request $request [description]
27+
* @param Closure $next [description]
2628
* @return [type] [description]
2729
*/
2830
public function handle($request, Closure $next)
@@ -35,24 +37,28 @@ public function handle($request, Closure $next)
3537
$response = $next($request);
3638

3739
$transaction->setResponse([
38-
'finished' => true,
40+
'finished' => true,
3941
'headers_sent' => true,
40-
'status_code' => $response->getStatusCode(),
41-
'headers' => $this->formatHeaders($response->headers->all()),
42+
'status_code' => $response->getStatusCode(),
43+
'headers' => $this->formatHeaders($response->headers->all()),
4244
]);
4345

4446
$transaction->setUserContext([
45-
'id' => optional($request->user())->id,
47+
'id' => optional($request->user())->id,
4648
'email' => optional($request->user())->email,
4749
]);
4850

4951
$transaction->setMeta([
5052
'result' => $response->getStatusCode(),
51-
'type' => 'HTTP'
53+
'type' => 'HTTP'
5254
]);
5355

5456
$transaction->setSpans(app('query-log')->toArray());
5557

58+
if (config('elastic-apm.transactions.use_route_uri')) {
59+
$transaction->setTransactionName($this->getRouteUriTransactionName($request));
60+
}
61+
5662
$transaction->stop(
5763
$this->getDuration(LARAVEL_START)
5864
);
@@ -63,8 +69,8 @@ public function handle($request, Closure $next)
6369
/**
6470
* Perform any final actions for the request lifecycle.
6571
*
66-
* @param \Illuminate\Http\Request $request
67-
* @param \Symfony\Component\HttpFoundation\Response $response
72+
* @param \Illuminate\Http\Request $request
73+
* @param \Symfony\Component\HttpFoundation\Response $response
6874
*
6975
* @return void
7076
*/
@@ -74,12 +80,14 @@ public function terminate($request, $response)
7480
}
7581

7682
/**
77-
* @param \Illuminate\Http\Request $request
83+
* @param \Illuminate\Http\Request $request
84+
*
85+
* @return string
7886
*/
79-
protected function getTransactionName(\Illuminate\Http\Request $request)
87+
protected function getTransactionName(\Illuminate\Http\Request $request): string
8088
{
8189
// fix leading /
82-
$path = ($request->server->get('PATH_INFO') == '') ? '/' : $request->server->get('PATH_INFO');
90+
$path = ($request->server->get('REQUEST_URI') == '') ? '/' : $request->server->get('REQUEST_URI');
8391

8492
return sprintf(
8593
"%s %s",
@@ -88,6 +96,27 @@ protected function getTransactionName(\Illuminate\Http\Request $request)
8896
);
8997
}
9098

99+
/**
100+
* @param \Illuminate\Http\Request $request
101+
*
102+
* @return string
103+
*/
104+
protected function getRouteUriTransactionName(\Illuminate\Http\Request $request): string
105+
{
106+
$path = ($request->route()->uri === '/') ? '' : $request->route()->uri;
107+
108+
return sprintf(
109+
"%s /%s",
110+
$request->server->get('REQUEST_METHOD'),
111+
$path
112+
);
113+
}
114+
115+
/**
116+
* @param $start
117+
*
118+
* @return float
119+
*/
91120
protected function getDuration($start): float
92121
{
93122
$diff = microtime(true) - $start;
@@ -96,6 +125,11 @@ protected function getDuration($start): float
96125
return round($corrected, 3);
97126
}
98127

128+
/**
129+
* @param array $headers
130+
*
131+
* @return array
132+
*/
99133
protected function formatHeaders(array $headers): array
100134
{
101135
return collect($headers)->map(function ($values, $header) {

src/Providers/ElasticApmServiceProvider.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace PhilKra\ElasticApmLaravel\Providers;
44

5-
use Illuminate\Support\ServiceProvider;
65
use Illuminate\Database\Events\QueryExecuted;
76
use Illuminate\Support\Collection;
7+
use Illuminate\Support\ServiceProvider;
88
use PhilKra\Agent;
9+
use PhilKra\ElasticApmLaravel\Contracts\VersionResolver;
910

1011
class ElasticApmServiceProvider extends ServiceProvider
1112
{
@@ -17,7 +18,7 @@ class ElasticApmServiceProvider extends ServiceProvider
1718
public function boot()
1819
{
1920
$this->publishes([
20-
__DIR__.'/../../config/elastic-apm.php' => config_path('elastic-apm.php'),
21+
__DIR__ . '/../../config/elastic-apm.php' => config_path('elastic-apm.php'),
2122
], 'config');
2223

2324
if (config('elastic-apm.active') === true && config('elastic-apm.spans.querylog.enabled') !== false) {
@@ -33,7 +34,7 @@ public function boot()
3334
public function register()
3435
{
3536
$this->mergeConfigFrom(
36-
__DIR__.'/../../config/elastic-apm.php',
37+
__DIR__ . '/../../config/elastic-apm.php',
3738
'elastic-apm'
3839
);
3940

@@ -45,26 +46,48 @@ public function register()
4546
'frameworkVersion' => app()->version(),
4647
],
4748
['active' => config('elastic-apm.active')],
48-
config('elastic-apm.app'),
49+
$this->getAppConfig(),
4950
config('elastic-apm.env'),
5051
config('elastic-apm.server')
5152
)
5253
);
5354
});
54-
55+
5556
$this->app->alias(Agent::class, 'elastic-apm');
5657
$this->app->instance('query-log', collect([]));
5758
}
5859

60+
/**
61+
* @return array
62+
*/
63+
protected function getAppConfig(): array
64+
{
65+
$config = config('elastic-apm.app');
66+
67+
if ($this->app->bound(VersionResolver::class)) {
68+
$config['appVersion'] = $this->app->make(VersionResolver::class)->getVersion();
69+
}
70+
71+
return $config;
72+
}
73+
74+
/**
75+
* @param Collection $stackTrace
76+
* @return Collection
77+
*/
5978
protected function stripVendorTraces(Collection $stackTrace): Collection
6079
{
6180
return collect($stackTrace)->filter(function ($trace) {
6281
return !starts_with(array_get($trace, 'file'), [
63-
base_path().'/vendor',
82+
base_path() . '/vendor',
6483
]);
6584
});
6685
}
6786

87+
/**
88+
* @param array $stackTrace
89+
* @return Collection
90+
*/
6891
protected function getSourceCode(array $stackTrace): Collection
6992
{
7093
if (config('elastic-apm.spans.renderSource', false) === false) {
@@ -77,9 +100,12 @@ protected function getSourceCode(array $stackTrace): Collection
77100

78101
$fileLines = file(array_get($stackTrace, 'file'));
79102
return collect($fileLines)->filter(function ($code, $line) use ($stackTrace) {
80-
$lineStart = array_get($stackTrace, 'line') - 5;
81-
$lineStop = array_get($stackTrace, 'line') + 5;
82-
103+
//file starts counting from 0, debug_stacktrace from 1
104+
$stackTraceLine = array_get($stackTrace, 'line') - 1;
105+
106+
$lineStart = $stackTraceLine - 5;
107+
$lineStop = $stackTraceLine + 5;
108+
83109
return $line >= $lineStart && $line <= $lineStop;
84110
})->groupBy(function ($code, $line) use ($stackTrace) {
85111
if ($line < array_get($stackTrace, 'line')) {
@@ -117,7 +143,8 @@ protected function listenForQueries()
117143
$sourceCode = $this->getSourceCode($trace);
118144

119145
return [
120-
'function' => array_get($trace, 'function').array_get($trace, 'type').array_get($trace, 'function'),
146+
'function' => array_get($trace, 'function') . array_get($trace, 'type') . array_get($trace,
147+
'function'),
121148
'abs_path' => array_get($trace, 'file'),
122149
'filename' => basename(array_get($trace, 'file')),
123150
'lineno' => array_get($trace, 'line', 0),
@@ -128,11 +155,12 @@ protected function listenForQueries()
128155
'post_context' => optional($sourceCode->get('post_context'))->toArray(),
129156
];
130157
})->values();
131-
158+
132159
$query = [
133160
'name' => 'Eloquent Query',
134161
'type' => 'db.mysql.query',
135-
'start' => round((microtime(true) - $query->time/1000 - LARAVEL_START) * 1000, 3), // calculate start time from duration
162+
'start' => round((microtime(true) - $query->time / 1000 - LARAVEL_START) * 1000, 3),
163+
// calculate start time from duration
136164
'duration' => round($query->time, 3),
137165
'stacktrace' => $stackTrace,
138166
'context' => [
@@ -144,7 +172,7 @@ protected function listenForQueries()
144172
],
145173
],
146174
];
147-
175+
148176
app('query-log')->push($query);
149177
});
150178
}

0 commit comments

Comments
 (0)