-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathLogMessageFormatter.php
More file actions
173 lines (153 loc) · 6.1 KB
/
Copy pathLogMessageFormatter.php
File metadata and controls
173 lines (153 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Ads\GoogleAds\Lib\V21;
use Google\ApiCore\ArrayTrait;
use Google\Protobuf\Internal\Message;
/**
* Formats information about a single gRPC / REST call for logging.
*/
final class LogMessageFormatter
{
use ArrayTrait;
use GoogleAdsMetadataTrait;
private $statusMetadataExtractor;
private $infoRedactor;
public function __construct(
?StatusMetadataExtractor $statusMetadataExtractor = null,
?InfoRedactor $infoRedactor = null
) {
$this->statusMetadataExtractor = $statusMetadataExtractor ?? new StatusMetadataExtractor();
$this->infoRedactor = $infoRedactor ?? new InfoRedactor();
}
/**
* Extracts the customer ID, if present, from the provided request.
*
* @param Message $request the request to get its customer ID
* @return string the customer ID if present or the message saying that the customer ID is not
* available
*/
private static function extractCustomerId(Message $request): string
{
// Most requests contain customer ID in the request, so we aim to extract that.
if (method_exists($request, 'getCustomerId')) {
return $request->getCustomerId();
} elseif (method_exists($request, 'getResourceName')) {
// In some cases, customer ID is available in the form of resource name, such as many
// Get requests.
$resourceName = $request->getResourceName();
$segments = explode('/', $resourceName);
if ($segments[0] === 'customers') {
return $segments[1];
}
}
return '"No customer ID could be extracted from the request"';
}
/**
* Formats the request and response data for summary logging.
*
* @param array $requestData the request data
* @param array $responseData the response data
* @param string $endpoint the API endpoint that the request has been sent to
* @return string the formatted logging message
*/
public function formatSummary(
array $requestData,
array $responseData,
$endpoint
) {
$method = $this->pluck('method', $requestData);
$argument = $this->pluck('argument', $requestData);
$status = $this->pluck('status', $responseData);
if ($status->code !== 0) {
$errorMessageList =
$this->statusMetadataExtractor->extractErrorMessageList($status->metadata);
}
return sprintf(
'Request made: Host: "%s", Method: "%s", CustomerId: %s, RequestId: "%s", '
. 'IsFault: %b, FaultMessage: "%s"',
$endpoint,
$method,
self::extractCustomerId($argument),
$this->getFirstHeaderValue(self::$REQUEST_ID_HEADER_KEY, $status->metadata),
$status->code !== 0,
!empty($errorMessageList) ? json_encode($errorMessageList) : 'None'
);
}
/**
* Formats the request and response data for detailed logging.
*
* @param array $requestData the request data
* @param array $responseData the response data
* @param string $endpoint the API endpoint that the request has been sent to
* @return string the formatted logging message
*/
public function formatDetail(
array $requestData,
array $responseData,
$endpoint
) {
$logMessageTokens = [];
$method = $this->pluck('method', $requestData);
$argument = $this->pluck('argument', $requestData);
$metadata = $this->pluck('metadata', $requestData) ?: [];
$response = $this->pluck('response', $responseData);
$status = $this->pluck('status', $responseData);
$call = $this->pluck('call', $responseData);
$logMessageTokens[] = 'Request';
$logMessageTokens[] = '-------';
$logMessageTokens[] = "Method Name: $method";
$logMessageTokens[] = "Host: $endpoint";
$logMessageTokens[] = "Headers: " . json_encode(
$this->infoRedactor->redactHeaders(self::joinPluckedArrays($metadata)),
JSON_PRETTY_PRINT
);
$logMessageTokens[] = "Request: ";
$logMessageTokens[] = $this->infoRedactor->redactBody($argument)->serializeToJsonString();
$logMessageTokens[] = "\nResponse";
$logMessageTokens[] = '-------';
$logMessageTokens[] = "Headers: " . json_encode(
self::joinPluckedArrays($call->getMetadata()),
JSON_PRETTY_PRINT
);
if ($status->code === 0) {
$logMessageTokens[] = "Response: ";
$logMessageTokens[] = is_null($response)
? "None" : $this->infoRedactor->redactBody($response)->serializeToJsonString();
} else {
$googleAdsFailure =
$this->statusMetadataExtractor->extractGoogleAdsFailure($status->metadata);
$logMessageTokens[] = "\nFault";
$logMessageTokens[] = '-------';
$logMessageTokens[] = "Status code: {$status->code}";
$logMessageTokens[] = "Details: {$status->details}";
$logMessageTokens[] = "Failure: {$googleAdsFailure->serializeToJsonString()}";
}
return implode("\n", $logMessageTokens);
}
/**
* @param array $array
* @return array the joined array after plucking
*/
private function joinPluckedArrays(array $array)
{
$joinedArray = [];
foreach (array_keys($array) as $key) {
$joinedArray[$key] = $this->pluck($key, $array)[0];
}
return $joinedArray;
}
}