-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlogging.js
More file actions
171 lines (163 loc) · 5.24 KB
/
logging.js
File metadata and controls
171 lines (163 loc) · 5.24 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
/*
* Copyright 2021 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.
*/
//const {Logging} = require('@google-cloud/logging');
const { google } = require("googleapis");
const logging = google.logging("v2");
const auth = require("./auth.js");
const axios = require("axios");
const _ = require("lodash");
const fs = require("fs");
/*
* For now, the get_trip/get_task logs are too noisy and don't have any visualization
* integrations.
*/
const interestingLogs = [
// ODRD
"create_vehicle",
"update_vehicle",
"get_vehicle",
"create_trip",
"update_trip",
// LMFS
"create_task",
"update_task",
"create_delivery_vehicle",
"update_delivery_vehicle",
];
/**
* Uses cloud logging APIs to list log entries from the
* fleet engine resource matching the specified label.
*/
async function fetchLogs(label, labelValues, daysAgo = 2, extra = "") {
const endPoint = "fleetengine.googleapis.com";
const odrdMonitoredResource = "fleetengine.googleapis.com/Fleet";
const lmfsMonitoredResource = "fleetengine.googleapis.com/DeliveryFleet";
const labelToMonitoredResource = {
vehicle_id: odrdMonitoredResource,
trip_id: odrdMonitoredResource,
delivery_vehicle_id: lmfsMonitoredResource,
task_id: lmfsMonitoredResource,
};
const monitoredResource = labelToMonitoredResource[label];
const resourceName = "projects/" + auth.getProjectId();
// TODO better handling of date range for search: allow start/end
let startDate = new Date(Date.now() - daysAgo * 24 * 3600 * 1000);
const logFilterString = _.map(
interestingLogs,
(logName) => `${resourceName}/logs/${endPoint}%2F${logName}`
).join(" OR ");
const labelFilterString = labelValues.join(" OR ");
const filterString = `resource.type=${monitoredResource} labels.${label}=(${labelFilterString}) timestamp>="${startDate.toISOString()}" ${extra} log_name=(${logFilterString})`;
console.log("log filter", filterString);
let entries = [];
try {
const request = {
resourceNames: [resourceName],
filter: filterString,
auth: auth.getAuthClient(),
pageSize: 500,
order_by: "timestamp desc",
};
let logs;
do {
if (logs && logs.data.nextPageToken) {
request.pageToken = logs.data.nextPageToken;
}
logs = await logging.entries.list(request);
if (logs.data.entries) {
entries = _.concat(entries, logs.data.entries);
}
} while (logs.data.nextPageToken);
} catch (e) {
console.log("failed to list logs", e);
}
// Remove get_trip calls -- there's too many of them
return _.filter(entries, (le) => !le.logName.endsWith("get_trip"));
}
/**
* Fetches logs from Fleet Archive using direct API calls.
*/
async function fetchLogsFromArchive(
label,
labelValue,
startTimeSeconds,
endTimeSeconds,
jwt
) {
const timeLogStr = `fetchLogsFromArchive with ${label}=${labelValue}`;
console.time(timeLogStr);
const odrdEndPoint = "https://fleetengine.googleapis.com/v1/archive";
const lmfsEndPoint = "https://fleetengine.googleapis.com/v1/deliveryArchive";
const labelToEndpoint = {
vehicles: odrdEndPoint,
trips: odrdEndPoint,
deliveryVehicles: lmfsEndPoint,
tasks: lmfsEndPoint,
};
const endPoint = labelToEndpoint[label];
const api = "collectCalls";
const config = {
url: `${endPoint}/providers/${auth.getProjectId()}/${label}/${labelValue}:${api}`,
headers: {
Authorization: "Bearer " + jwt,
},
params: {
"time_window.start_time.seconds": startTimeSeconds,
"time_window.end_time.seconds": endTimeSeconds,
page_size: 50,
modifying_calls_only: true,
},
};
let entries = [];
let pages = 0;
let lastNumPagesLogged = 0;
const loggingThresholdInPages = 10;
try {
let response;
do {
if (response && response.data.nextPageToken) {
config.params.page_token = response.data.nextPageToken;
}
response = await axios(config);
if (response.data.apiCalls) {
entries = _.concat(entries, response.data.apiCalls);
}
pages++;
if (pages >= lastNumPagesLogged + loggingThresholdInPages) {
console.timeLog(
timeLogStr,
`Fetched ${pages} pages, got ${entries.length} entries`
);
lastNumPagesLogged = pages;
}
} while (response.data.nextPageToken);
} catch (err) {
console.log(err);
if (err.response) console.log(JSON.stringify(err.response.data));
}
console.timeEnd(timeLogStr);
return entries;
}
/**
* Generates & writes a valid javascript to specified file. Existing file at location
* will be overwritten.
*/
function writeLogs(filePath, data) {
fs.writeFileSync(filePath, JSON.stringify(data));
}
exports.fetchLogs = fetchLogs;
exports.fetchLogsFromArchive = fetchLogsFromArchive;
exports.writeLogs = writeLogs;