1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\Lumen\GraphQL\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Digia\JsonHelpers\JsonEncoder; |
7
|
|
|
use Digia\Lumen\GraphQL\Models\GraphQLError; |
8
|
|
|
use Digia\Lumen\GraphQL\Exceptions\MalformedNodeId; |
9
|
|
|
use Digia\Lumen\GraphQL\Exceptions\EntityNotFoundException; |
10
|
|
|
use Nord\Lumen\NewRelic\NewRelicMiddleware as BaseNewRelicMiddleware; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class NewRelicMiddleware |
15
|
|
|
* @package Digia\Lumen\GraphQL\Http\Middleware |
16
|
|
|
*/ |
17
|
|
|
class NewRelicMiddleware extends BaseNewRelicMiddleware |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public const ATTRIBUTE_ERROR = __CLASS__ . '_error'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A list of the exception types that should not be reported. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $dontReport = [ |
28
|
|
|
EntityNotFoundException::class, |
29
|
|
|
MalformedNodeId::class |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritDoc |
34
|
|
|
*/ |
35
|
|
|
public function handle(Request $request, Closure $next) |
36
|
|
|
{ |
37
|
|
|
$response = parent::handle($request, $next); |
38
|
|
|
|
39
|
|
|
// Manually "notice" GraphQL errors |
40
|
|
|
$graphqlError = $request->attributes->get(self::ATTRIBUTE_ERROR); |
41
|
|
|
|
42
|
|
|
if ($graphqlError !== null) { |
43
|
|
|
$this->report($graphqlError); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $response; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Report GraphQLError to New Relic |
51
|
|
|
* |
52
|
|
|
* @param GraphQLError $graphQLError |
53
|
|
|
*/ |
54
|
|
|
protected function report(GraphQLError $graphQLError) |
55
|
|
|
{ |
56
|
|
|
foreach ($graphQLError->getExceptions() as $exception) { |
57
|
|
|
if ($this->shouldReport($exception)) { |
58
|
|
|
$this->newRelic->noticeError(JsonEncoder::encode([ |
59
|
|
|
'query' => $graphQLError->getQuery(), |
60
|
|
|
'variables' => $graphQLError->getVariables(), |
61
|
|
|
'message' => $exception->getMessage() |
62
|
|
|
]), $exception); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Determine if the exception types is in the "do not report" list. |
69
|
|
|
* |
70
|
|
|
* @param \Exception $exception |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
protected function shouldReport(\Exception $exception) |
75
|
|
|
{ |
76
|
|
|
foreach ($this->dontReport as $type) { |
77
|
|
|
if ($exception instanceof $type) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function getTransactionName(Request $request) |
89
|
|
|
{ |
90
|
|
|
// Use the "operation name" if available, otherwise fall back to parent implementation |
91
|
|
|
$operationName = $request->get('operationName'); |
92
|
|
|
|
93
|
|
|
if ($operationName !== null) { |
94
|
|
|
return 'GraphQLController@' . $operationName; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return parent::getTransactionName($request); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|