/
core.js
60 lines (53 loc) · 1.56 KB
/
core.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
const logger = require('logzio-nodejs');
const DataParser = require('./data-parser');
function getCallBackFunction(context) {
return function callback(err, bulk) {
if (err) {
context.err(`logzio-logger error: ${err}`, err);
context.bindings.outputBlob = bulk;
}
context.done();
};
}
const getParserOptions = () => ({
metrics: {
token: process.env.LogzioMetricsToken,
host: process.env.LogzioMetricsHost,
},
logs: {
token: process.env.LogzioLogsToken,
host: process.env.LogzioLogsHost,
},
});
module.exports = function processEventHubMessages(context, eventHubMessages, {
enableMetric = false,
}) {
const {
host,
token,
} = enableMetric ? getParserOptions().metrics : getParserOptions().logs;
context.log(`Starting Logz.io Azure function with enableMetrics: ${enableMetric}`);
context.log(JSON.stringify(eventHubMessages));
const callBackFunction = getCallBackFunction(context);
const logzioShipper = logger.createLogger({
token,
host,
type: 'eventHub',
protocol: 'https',
internalLogger: context,
compress: true,
debug: true,
callback: callBackFunction,
});
const dataParser = new DataParser({
internalLogger: context,
enableMetric,
});
const parseMessagesArray = dataParser.parseEventHubLogMessagesToArray(eventHubMessages);
context.log(`About to send ${parseMessagesArray.length} logs...`);
parseMessagesArray.forEach((log) => {
context.log(JSON.stringify(`logging: ${log}`));
logzioShipper.log(log);
});
logzioShipper.sendAndClose(callBackFunction);
};