-
Notifications
You must be signed in to change notification settings - Fork 377
/
index.js
604 lines (560 loc) · 20.5 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2021 Datadog, Inc.
var https = require('https');
const VERSION = '1.0.1';
const STRING = 'string'; // example: 'some message'
const STRING_ARRAY = 'string-array'; // example: ['one message', 'two message', ...]
const JSON_OBJECT = 'json-object'; // example: {"key": "value"}
const JSON_ARRAY = 'json-array'; // example: [{"key": "value"}, {"key": "value"}, ...] or [{"records": [{}, {}, ...]}, {"records": [{}, {}, ...]}, ...]
const BUFFER_ARRAY = 'buffer-array'; // example: [<Buffer obj>, <Buffer obj>]
const JSON_STRING = 'json-string'; // example: '{"key": "value"}'
const JSON_STRING_ARRAY = 'json-string-array'; // example: ['{"records": [{}, {}]}'] or ['{"key": "value"}']
const INVALID = 'invalid';
const JSON_TYPE = 'json';
const STRING_TYPE = 'string';
const DD_API_KEY = process.env.DD_API_KEY || '<DATADOG_API_KEY>';
const DD_SITE = process.env.DD_SITE || 'datadoghq.com';
const DD_HTTP_URL = process.env.DD_URL || 'http-intake.logs.' + DD_SITE;
const DD_HTTP_PORT = process.env.DD_PORT || 443;
const DD_TAGS = process.env.DD_TAGS || ''; // Replace '' by your comma-separated list of tags
const DD_SERVICE = process.env.DD_SERVICE || 'azure';
const DD_SOURCE = process.env.DD_SOURCE || 'azure';
const DD_SOURCE_CATEGORY = process.env.DD_SOURCE_CATEGORY || 'azure';
/*
To scrub PII from your logs, uncomment the applicable configs below. If you'd like to scrub more than just
emails and IP addresses, add your own config to this map in the format
NAME: {pattern: <regex_pattern>, replacement: <string to replace matching text with>}
*/
const SCRUBBER_RULE_CONFIGS = {
// REDACT_IP: {
// pattern: /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/,
// replacement: 'xxx.xxx.xxx.xxx'
// },
// REDACT_EMAIL: {
// pattern: /[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+/,
// replacement: 'xxxxx@xxxxx.com'
// }
};
/*
To split array-type fields in your logs into individual logs, you can add sections to the map below. An example of
a potential use case with azure.datafactory is there to show the format:
{
source_type:
paths: [list of [list of fields in the log payload to iterate through to find the one to split]],
keep_original_log: bool, if you'd like to preserve the original log in addition to the split ones or not,
preserve_fields: bool, whether or not to keep the original log fields in the new split logs
}
You can also set the DD_LOG_SPLITTING_CONFIG env var with a JSON string in this format.
*/
const DD_LOG_SPLITTING_CONFIG = {
// 'azure.datafactory': {
// paths: [['properties', 'Output', 'value']],
// keep_original_log: true,
// preserve_fields: true
// }
};
function getLogSplittingConfig() {
try {
return JSON.parse(process.env.DD_LOG_SPLITTING_CONFIG);
} catch {
return DD_LOG_SPLITTING_CONFIG;
}
}
class ScrubberRule {
constructor(name, pattern, replacement) {
this.name = name;
this.replacement = replacement;
this.regexp = RegExp(pattern, 'g');
}
}
class Batcher {
constructor(context, maxItemSizeBytes, maxBatchSizeBytes, maxItemsCount) {
this.maxItemSizeBytes = maxItemSizeBytes;
this.maxBatchSizeBytes = maxBatchSizeBytes;
this.maxItemsCount = maxItemsCount;
}
batch(items) {
var batches = [];
var batch = [];
var sizeBytes = 0;
var sizeCount = 0;
for (var i = 0; i < items.length; i++) {
var item = items[i];
var itemSizeBytes = this.getSizeInBytes(item);
if (
sizeCount > 0 &&
(sizeCount >= this.maxItemsCount ||
sizeBytes + itemSizeBytes > this.maxBatchSizeBytes)
) {
batches.push(batch);
batch = [];
sizeBytes = 0;
sizeCount = 0;
}
// all items exceeding maxItemSizeBytes are dropped here
if (itemSizeBytes <= this.maxItemSizeBytes) {
batch.push(item);
sizeBytes += itemSizeBytes;
sizeCount += 1;
}
}
if (sizeCount > 0) {
batches.push(batch);
}
return batches;
}
getSizeInBytes(string) {
if (typeof string !== 'string') {
string = JSON.stringify(string);
}
return Buffer.byteLength(string, 'utf8');
}
}
class HTTPClient {
constructor(context) {
this.context = context;
this.httpOptions = {
hostname: DD_HTTP_URL,
port: DD_HTTP_PORT,
path: '/v1/input',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'DD-API-KEY': DD_API_KEY
}
};
this.scrubber = new Scrubber(this.context, SCRUBBER_RULE_CONFIGS);
this.batcher = new Batcher(
this.context,
256 * 1000,
4 * 1000 * 1000,
400
);
}
async sendAll(records) {
var batches = this.batcher.batch(records);
var promises = [];
for (var i = 0; i < batches.length; i++) {
promises.push(this.sendWithRetry(batches[i]));
}
return await Promise.all(
promises.map(p => p.catch(e => this.context.log.error(e)))
);
}
sendWithRetry(record) {
return new Promise((resolve, reject) => {
return this.send(record)
.then(res => {
resolve(true);
})
.catch(err => {
this.send(record)
.then(res => {
resolve(true);
})
.catch(err => {
reject(
`unable to send request after 2 tries, err: ${err}`
);
});
});
});
}
send(record) {
return new Promise((resolve, reject) => {
const req = https
.request(this.httpOptions, resp => {
if (resp.statusCode < 200 || resp.statusCode > 299) {
reject(`invalid status code ${resp.statusCode}`);
} else {
resolve(true);
}
})
.on('error', error => {
reject(error);
});
req.write(this.scrubber.scrub(JSON.stringify(record)));
req.end();
});
}
}
class Scrubber {
constructor(context, configs) {
var rules = [];
for (const [name, settings] of Object.entries(configs)) {
try {
rules.push(
new ScrubberRule(
name,
settings['pattern'],
settings['replacement']
)
);
} catch {
context.log.error(
`Regexp for rule ${name} pattern ${settings['pattern']} is malformed, skipping. Please update the pattern for this rule to be applied.`
);
}
}
this.rules = rules;
}
scrub(record) {
if (!this.rules) {
return record;
}
this.rules.forEach(rule => {
record = record.replace(rule.regexp, rule.replacement);
});
return record;
}
}
class BlobStorageLogHandler {
constructor(context) {
this.context = context;
this.logSplittingConfig = getLogSplittingConfig();
this.records = [];
}
findSplitRecords(record, fields) {
var tempRecord = record;
for (const fieldName in fields) {
// loop through the fields to find the one we want to split
if (
tempRecord[fieldName] === undefined ||
tempRecord[fieldName] === null
) {
// if the field is null or undefined, return
return null;
} else {
// there is some value for the field
try {
// if for some reason we can't index into it, return null
tempRecord = tempRecord[fieldName];
} catch {
return null;
}
}
}
return tempRecord;
}
formatLog(messageType, record) {
if (messageType == JSON_TYPE) {
var originalRecord = this.addTagsToJsonLog(record);
var source = originalRecord['ddsource'];
var config = this.logSplittingConfig[source];
if (config !== undefined) {
var splitFieldFound = false;
for (var i = 0; i < config.paths.length; i++) {
var fields = config.paths[i];
var recordsToSplit = this.findSplitRecords(record, fields);
if (
recordsToSplit === null ||
!(recordsToSplit instanceof Array)
) {
// if we were unable find the field or if the field isn't an array, skip it
continue;
}
splitFieldFound = true;
for (var j = 0; j < recordsToSplit.length; j++) {
var splitRecord = recordsToSplit[j];
if (typeof splitRecord === 'string') {
try {
splitRecord = JSON.parse(splitRecord);
} catch (err) {}
}
var formattedSplitRecord = {};
var temp = formattedSplitRecord;
// re-create the same nested attributes with only the split log
for (var k = 0; k < fields.length; k++) {
if (k === fields.length - 1) {
// if it is the last field, add the split record
temp[fields[k]] = splitRecord;
} else {
temp[fields[k]] = {};
temp = temp[fields[k]];
}
}
formattedSplitRecord = {
parsed_arrays: formattedSplitRecord
};
if (config.preserve_fields) {
var newRecord = { ...originalRecord };
} else {
var newRecord = {
ddsource: source,
ddsourcecategory:
originalRecord['ddsourcecategory'],
service: originalRecord['service'],
ddtags: originalRecord['ddtags']
};
if (originalRecord['time'] !== undefined) {
newRecord['time'] = originalRecord['time'];
}
}
Object.assign(newRecord, formattedSplitRecord);
this.records.push(newRecord);
}
}
if (config.keep_original_log || splitFieldFound !== true) {
// keep the original log if it is set in the config
// if it is false in the config, we should still write the log when we don't split
this.records.push(originalRecord);
}
} else {
this.records.push(originalRecord);
}
} else {
record = this.addTagsToStringLog(record);
this.records.push(record);
}
}
handleLogs(logs) {
var logsType = this.getLogFormat(logs);
switch (logsType) {
case STRING:
this.formatLog(STRING_TYPE, logs);
break;
case JSON_STRING:
logs = JSON.parse(logs);
this.formatLog(JSON_TYPE, logs);
break;
case JSON_OBJECT:
this.formatLog(JSON_TYPE, logs);
break;
case STRING_ARRAY:
logs.forEach(log => this.formatLog(STRING_TYPE, log));
break;
case JSON_ARRAY:
this.handleJSONArrayLogs(logs, JSON_ARRAY);
break;
case BUFFER_ARRAY:
this.handleJSONArrayLogs(logs, BUFFER_ARRAY);
break;
case JSON_STRING_ARRAY:
this.handleJSONArrayLogs(logs, JSON_STRING_ARRAY);
break;
case INVALID:
this.context.log.error('Log format is invalid: ', logs);
break;
default:
this.context.log.error('Log format is invalid: ', logs);
break;
}
return this.records;
}
handleJSONArrayLogs(logs, logsType) {
for (var i = 0; i < logs.length; i++) {
var message = logs[i];
if (logsType == JSON_STRING_ARRAY) {
try {
message = JSON.parse(message);
} catch (err) {
this.context.log.warn(
'log is malformed json, sending as string'
);
this.formatLog(STRING_TYPE, message);
continue;
}
}
// If the message is a buffer object, the data type has been set to binary.
if (logsType == BUFFER_ARRAY) {
try {
message = JSON.parse(message.toString());
} catch (err) {
this.context.log.warn(
'log is malformed json, sending as string'
);
this.formatLog(STRING_TYPE, message.toString());
continue;
}
}
if (message.records != undefined) {
message.records.forEach(message =>
this.formatLog(JSON_TYPE, message)
);
} else {
this.formatLog(JSON_TYPE, message);
}
}
}
getLogFormat(logs) {
if (typeof logs === 'string') {
if (this.isJsonString(logs)) {
return JSON_STRING;
}
return STRING;
}
if (!Array.isArray(logs) && typeof logs === 'object' && logs !== null) {
return JSON_OBJECT;
}
if (!Array.isArray(logs)) {
return INVALID;
}
if (Buffer.isBuffer(logs[0])) {
return BUFFER_ARRAY;
}
if (typeof logs[0] === 'object') {
return JSON_ARRAY;
}
if (typeof logs[0] === 'string') {
if (this.isJsonString(logs[0])) {
return JSON_STRING_ARRAY;
} else {
return STRING_ARRAY;
}
}
return INVALID;
}
isJsonString(record) {
try {
JSON.parse(record);
return true;
} catch (err) {
return false;
}
}
createDDTags(tags) {
const forwarderNameTag = 'forwardername:' + this.context.executionContext.functionName;
const fowarderVersionTag = 'forwarderversion:' + VERSION;
var ddTags = tags.concat([DD_TAGS, forwarderNameTag, fowarderVersionTag]);
return ddTags.filter(Boolean).join(',');
}
addTagsToJsonLog(record) {
var [metadata, record] = this.extractMetadataFromLog(record);
record['ddsource'] = metadata.source || DD_SOURCE;
record['ddsourcecategory'] = DD_SOURCE_CATEGORY;
record['service'] = metadata.service || DD_SERVICE;
record['ddtags'] = this.createDDTags(metadata.tags);
return record;
}
addTagsToStringLog(stringLog) {
var jsonLog = { message: stringLog };
return this.addTagsToJsonLog(jsonLog);
}
createResourceIdArray(resourceId) {
// Convert a valid resource ID to an array, handling beginning/ending slashes
var resourceIdArray = resourceId.toLowerCase().split('/');
if (resourceIdArray[0] === '') {
resourceIdArray = resourceIdArray.slice(1);
}
if (resourceIdArray[resourceIdArray.length - 1] === '') {
resourceIdArray.pop();
}
return resourceIdArray;
}
isSource(resourceIdPart) {
// Determine if a section of a resource ID counts as a "source," in our case it means it starts with 'microsoft.'
return resourceIdPart.startsWith('microsoft.');
}
formatSourceType(sourceType) {
return sourceType.replace('microsoft.', 'azure.');
}
getResourceId(record) {
// Most logs have resourceId, but some logs have ResourceId instead
var id = record.resourceId || record.ResourceId;
if (typeof id !== 'string') {
return null;
}
return id;
}
extractMetadataFromLog(record) {
return [this.extractMetadataFromStandardLog(record), record];
}
extractMetadataFromStandardLog(record) {
var metadata = { tags: [], source: '', service: '' };
var resourceId = this.getResourceId(record);
if (resourceId === null || resourceId === '') {
return metadata;
}
resourceId = this.createResourceIdArray(resourceId);
if (resourceId[0] === 'subscriptions') {
if (resourceId.length > 1) {
metadata.tags.push('subscription_id:' + resourceId[1]);
if (resourceId.length == 2) {
metadata.source = 'azure.subscription';
return metadata;
}
}
if (resourceId.length > 3) {
if (
resourceId[2] === 'providers' &&
this.isSource(resourceId[3])
) {
// handle provider-only resource IDs
metadata.source = this.formatSourceType(resourceId[3]);
} else {
metadata.tags.push('resource_group:' + resourceId[3]);
if (resourceId.length == 4) {
metadata.source = 'azure.resourcegroup';
return metadata;
}
}
}
if (resourceId.length > 5 && this.isSource(resourceId[5])) {
metadata.source = this.formatSourceType(resourceId[5]);
}
} else if (resourceId[0] === 'tenants') {
if (resourceId.length > 3 && resourceId[3]) {
metadata.tags.push('tenant:' + resourceId[1]);
metadata.source = this.formatSourceType(resourceId[3]).replace(
'aadiam',
'activedirectory'
);
}
}
return metadata;
}
}
module.exports = async function(context, blobContent) {
if (!DD_API_KEY || DD_API_KEY === '<DATADOG_API_KEY>') {
context.log.error(
'You must configure your API key before starting this function (see ## Parameters section)'
);
return;
}
var logs;
if (typeof blobContent === 'string') {
logs = blobContent.trim().split('\n');
} else if (Buffer.isBuffer(blobContent)) {
logs = blobContent
.toString('utf8')
.trim()
.split('\n');
} else {
logs = JSON.stringify(blobContent)
.trim()
.split('\n');
}
try {
var handler = new BlobStorageLogHandler(context);
var parsedLogs = handler.handleLogs(logs);
} catch (err) {
context.log.error('Error raised when parsing logs: ', err);
throw err;
}
var results = await new HTTPClient(context).sendAll(parsedLogs);
if (results.every(v => v === true) !== true) {
context.log.error(
'Some messages were unable to be sent. See other logs for details.'
);
}
context.done();
};
module.exports.forTests = {
BlobStorageLogHandler,
Scrubber,
ScrubberRule,
Batcher,
HTTPClient,
constants: {
STRING,
STRING_ARRAY,
JSON_OBJECT,
JSON_ARRAY,
BUFFER_ARRAY,
JSON_STRING,
JSON_STRING_ARRAY,
INVALID
}
};