forked from wegift/datadog-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_log.js
166 lines (150 loc) · 5.42 KB
/
export_log.js
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
import { v2 } from "@datadog/datadog-api-client";
import * as fs from "fs";
const configuration = v2.createConfiguration();
const apiInstance = new v2.LogsApi(configuration);
import { storage } from "./storage/storage.js";
const verbose = process.env.verbose ?? false;
const outputFile = process.env.outputFile;
const outputFormat = process.env.outputFormat;
const destFileName = process.env.destFileName;
let initialParams = {};
function yesterday() {
return new Date(new Date() - 86400000);
}
const startTime = new Date();
const columns = JSON.parse(process.env.columns);
/*
Performs a deep walk into an object to see if a specific key (or sub key) exists:
- if the key was found, it returns the value
- if the key doesn't exists, it returns null
*/
function deepCheck(rif, key) {
let k = key.shift();
if (rif[k] != undefined) {
if (key.length == 0) {
return rif[k];
} else {
return deepCheck(rif[k], key);
}
} else {
return null;
}
}
initialParams = {
filterQuery: process.env.query,
filterFrom: process.env.from ? new Date(process.env.from) : yesterday(),
filterTo: process.env.to ? new Date(process.env.to) : new Date(),
pageLimit: process.env.pageSize ? Math.min(process.env.pageSize, 5000) : 1000,
};
if (verbose) {
console.log(destFileName + " *** Downloading logs:\n" + JSON.stringify(initialParams, null, 2) + "\noutputFormat: " + outputFormat + "\noutputFile: " + outputFile + "\nVerbose: " + verbose.toString() + "\n");
console.log(destFileName + ' *** Start at ' + startTime);
}
getLogs(apiInstance, initialParams, (data) => {
if (storage != null) {
storage.uploadFile(outputFile, destFileName,
(e) => {
if (verbose) {
console.log(destFileName + ' *** Error occurred: ' + e);
}
storage.uploadFile('./semaphore', destFileName + '.ERR',
(e) => { },
() => {
storage.deleteFile(destFileName + '.sem');
}
);
},
() => {
fs.unlinkSync(outputFile, () => { });
storage.deleteFile(destFileName + '.sem');
}
);
} else {
storage.uploadFile('./semaphore', destFileName + '.ERR',
(e) => { },
() => {
storage.deleteFile(destFileName + '.sem');
}
);
}
});
async function getLogs(apiInstance, params, cb) {
let data = [];
let contentLen = 0;
let nextPage = null;
let n = 0;
try {
if (outputFormat == "CSV") {
let head = 'date';
columns.forEach(c => { head += ',' + c.label });
fs.writeFileSync(outputFile, head + "\n");
}
do {
if (verbose) {
console.log(`${destFileName} *** Requesting page ${++n}`);
}
const query = nextPage ? { ...params, pageCursor: nextPage } : params;
const result = await apiInstance.listLogsGet(query);
let csvData = "";
result.data.forEach(tmpRow => {
if (outputFormat == "CSV") {
csvData += '"' + (new Date(tmpRow.attributes.timestamp).toISOString()) + '"';
}
if (outputFormat == "JSON") {
let tmpData = { date: tmpRow.attributes.timestamp }
}
columns.forEach(c => {
let attrs = c.path.split('#');
let value = deepCheck(tmpRow.attributes, attrs);
if (typeof value == 'object') {
value = JSON.stringify(value);
}
if (value == null) {
value = c.default ? c.default.toString() : '';
}
if (outputFormat == "CSV") {
csvData += ',"' + value + '"';
}
if (outputFormat == "JSON") {
tmpData[c.label] = value;
}
});
if (outputFormat == "CSV") {
csvData += '\n';
}
if (outputFormat == "JSON") {
data.push(tmpData);
}
});
if (outputFormat == "CSV") {
fs.appendFileSync(outputFile, csvData);
}
contentLen += result.data.length;
if (verbose) {
console.log(`${destFileName} *** Writing ${result.data.length} logs to ${outputFile} (${contentLen} total)`);
}
nextPage = result?.meta?.page?.after;
} while (nextPage);
if (outputFormat == "JSON") {
fs.writeFileSync(outputFile, JSON.stringify(data, null, 2));
}
} catch (e) {
if (verbose) {
console.log(destFileName + " *** An error occurred: " + e);
fs.writeFileSync('./error', e.toString());
storage.uploadFile('./error', destFileName + '.ERR',
(e) => {
fs.unlinkSync('./error');
},
() => {
fs.unlinkSync('./error');
storage.deleteFile(destFileName + '.sem');
}
);
}
}
if (verbose) {
console.log(destFileName + " *** Process ended.");
}
cb(contentLen);
}