Skip to content

Commit 7c2de71

Browse files
committed
PWA-1311: New Relic is not being given useful transaction names for graphql requests
- refactor to reduce complexity and improve naming
1 parent 4e62e39 commit 7c2de71

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
192192

193193
// log information about the query, unless it is an introspection query
194194
if (strpos($data['query'], 'IntrospectionQuery') === false) {
195-
$queryInformation = $this->logDataHelper->getRequestInformation($request, $data, $schema, $this->httpResponse);
195+
$queryInformation = $this->logDataHelper->getLogData($request, $data, $schema, $this->httpResponse);
196196
$this->loggerPool->execute($queryInformation);
197197
}
198198

app/code/Magento/GraphQl/Helper/Query/Logger/LogData.php

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,75 @@ class LogData
3232
* @param HttpResponse|null $response
3333
* @return array
3434
*/
35-
public function getRequestInformation(
35+
public function getLogData(
3636
RequestInterface $request,
3737
array $data,
38-
Schema $schema = null,
39-
HttpResponse $response = null
38+
?Schema $schema,
39+
?HttpResponse $response
4040
) : array {
41-
$requestInformation = [];
41+
$logData = [];
42+
$logData = array_merge($logData, $this->gatherRequestInformation($request));
43+
if ($schema) {
44+
$logData = array_merge($logData, $this->gatherQueryInformation($schema));
45+
}
46+
$logData[LoggerInterface::COMPLEXITY] = $this->getFieldCount($data['query'] ?? '');
47+
if ($response) {
48+
$logData = array_merge($logData, $this->gatherResponseInformation($response));
49+
}
50+
51+
return $logData;
52+
}
53+
54+
/**
55+
* Gets the information needed from the request
56+
*
57+
* @param RequestInterface $request
58+
* @return array
59+
*/
60+
private function gatherRequestInformation(RequestInterface $request) : array
61+
{
4262
$requestInformation[LoggerInterface::HTTP_METHOD] = $request->getMethod();
4363
$requestInformation[LoggerInterface::STORE_HEADER] = $request->getHeader('Store') ?: '';
4464
$requestInformation[LoggerInterface::CURRENCY_HEADER] = $request->getHeader('Currency') ?: '';
4565
$requestInformation[LoggerInterface::HAS_AUTH_HEADER] = $request->getHeader('Authorization') ? 'true' : 'false';
46-
$requestInformation[LoggerInterface::IS_CACHEABLE] =
47-
($response->getHeader('X-Magento-Tags') && $response->getHeader('X-Magento-Tags') !== '')
48-
? 'true'
49-
: 'false';
5066
$requestInformation[LoggerInterface::REQUEST_LENGTH] = $request->getHeader('Content-Length') ?: '';
67+
return $requestInformation;
68+
}
5169

52-
if ($schema) {
53-
$schemaConfig = $schema->getConfig();
54-
$mutationOperations = $schemaConfig->getMutation()->getFields();
55-
$queryOperations = $schemaConfig->getQuery()->getFields();
56-
$requestInformation[LoggerInterface::HAS_MUTATION] = count($mutationOperations) > 0 ? 'true' : 'false';
57-
$requestInformation[LoggerInterface::NUMBER_OF_OPERATIONS] =
58-
count($mutationOperations) + count($queryOperations);
59-
$operationNames = array_merge(array_keys($mutationOperations), array_keys($queryOperations));
60-
$requestInformation[LoggerInterface::OPERATION_NAMES] =
61-
count($operationNames) > 0 ? implode(",", $operationNames) : 'operationNameNotFound';
62-
}
63-
64-
$requestInformation[LoggerInterface::COMPLEXITY] = $this->getFieldCount($data['query'] ?? '');
65-
66-
if ($response) {
67-
$requestInformation[LoggerInterface::HTTP_RESPONSE_CODE] = $response->getHttpResponseCode();
68-
}
70+
/**
71+
* Gets the information needed from the schema
72+
*
73+
* @param Schema $schema
74+
* @return array
75+
*/
76+
private function gatherQueryInformation(Schema $schema) : array
77+
{
78+
$schemaConfig = $schema->getConfig();
79+
$mutationOperations = $schemaConfig->getMutation()->getFields();
80+
$queryOperations = $schemaConfig->getQuery()->getFields();
81+
$queryInformation[LoggerInterface::HAS_MUTATION] = count($mutationOperations) > 0 ? 'true' : 'false';
82+
$queryInformation[LoggerInterface::NUMBER_OF_OPERATIONS] =
83+
count($mutationOperations) + count($queryOperations);
84+
$operationNames = array_merge(array_keys($mutationOperations), array_keys($queryOperations));
85+
$queryInformation[LoggerInterface::OPERATION_NAMES] =
86+
count($operationNames) > 0 ? implode(",", $operationNames) : 'operationNameNotFound';
87+
return $queryInformation;
88+
}
6989

70-
return $requestInformation;
90+
/**
91+
* Gets the information needed from the response
92+
*
93+
* @param HttpResponse $response
94+
* @return array
95+
*/
96+
private function gatherResponseInformation(HttpResponse $response) : array
97+
{
98+
$responseInformation[LoggerInterface::IS_CACHEABLE] =
99+
($response->getHeader('X-Magento-Tags') && $response->getHeader('X-Magento-Tags') !== '')
100+
? 'true'
101+
: 'false';
102+
$responseInformation[LoggerInterface::HTTP_RESPONSE_CODE] = $response->getHttpResponseCode();
103+
return $responseInformation;
71104
}
72105

73106
/**

dev/tests/integration/testsuite/Magento/GraphQl/Helper/Query/Logger/LogDataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testGetQueryInformation(string $query, array $headers, array $ex
8282
$queryFields = $this->objectManager->get(Fields::class);
8383
$queryFields->setQuery($query);
8484

85-
$queryInformation = $this->logData->getRequestInformation(
85+
$queryInformation = $this->logData->getLogData(
8686
$this->request,
8787
$postData,
8888
$this->schemaGenerator->generate(),

0 commit comments

Comments
 (0)