Skip to content

Commit 4b9a9f5

Browse files
committed
use promises and finally working retries
1 parent fa29b66 commit 4b9a9f5

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

azure/activity_logs_monitoring/index.js

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2020 Datadog, Inc.
55

6-
var https = require('https');
6+
const httpsLib = require('https');
77

88
const VERSION = '0.1.2';
99

@@ -26,63 +26,82 @@ const DD_SOURCE_CATEGORY = process.env.DD_SOURCE_CATEGORY || 'azure';
2626

2727
const ONE_SEC = 1000;
2828

29-
module.exports = function(context, eventHubMessages) {
29+
module.exports = async function(context, eventHubMessages) {
3030
if (!DD_API_KEY || DD_API_KEY === '<DATADOG_API_KEY>') {
3131
context.log.error(
3232
'You must configure your API key before starting this function (see ## Parameters section)'
3333
);
3434
return;
3535
}
36+
handleLogs(sender, eventHubMessages, context);
37+
};
3638

37-
const options = {
38-
hostname: DD_URL,
39-
port: 443,
40-
path: '/v1/input',
41-
method: 'POST',
42-
headers: {
43-
'Content-Type': 'application/json',
44-
'DD-API-KEY': DD_API_KEY
45-
},
46-
timeout: ONE_SEC
47-
};
48-
var sender = tagger => record => {
49-
record = tagger(record, context);
50-
51-
const request = https.request(options, res => {
52-
if (res.statusCode < 200 || res.statusCode > 299) {
53-
context.log.error(
54-
'unable to send message, err code: ' + res.statusCode
55-
);
56-
}
57-
});
39+
function sender(tagger, record, context) {
40+
record = tagger(record, context);
41+
// retry once
42+
asyncSend(tagger, record, context).catch(
43+
asyncSend(tagger, record, context).catch(handleFailure(context))
44+
);
45+
}
5846

59-
request.on('error', e => {
60-
context.log.error('unable to send request');
61-
});
47+
function handleFailure(context) {
48+
context.log.error('Unable to send message');
49+
}
6250

63-
// Write data to request body
64-
request.write(JSON.stringify(record));
65-
request.end();
66-
};
67-
handleLogs(sender, eventHubMessages, context);
68-
context.done();
69-
};
51+
async function asyncSend(tagger, record, context, tries) {
52+
return await send(record, context);
53+
}
54+
55+
async function send(record, context) {
56+
return new Promise((resolve, reject) => {
57+
const options = {
58+
hostname: DD_URL,
59+
port: 443,
60+
path: '/v1/input',
61+
method: 'POST',
62+
headers: {
63+
'Content-Type': 'application/json',
64+
'DD-API-KEY': DD_API_KEY
65+
},
66+
timeout: ONE_SEC
67+
};
68+
const myReq = httpsLib
69+
.request(options, myResponse => {
70+
if (
71+
myResponse.statusCode < 200 ||
72+
myResponse.statusCode > 299
73+
) {
74+
reject(`invalid status code ${myResponse.statusCode}`);
75+
} else {
76+
resolve();
77+
}
78+
})
79+
.on('error', error => {
80+
reject(error);
81+
});
82+
83+
myReq.write(JSON.stringify(record));
84+
myReq.end();
85+
});
86+
}
7087

7188
function handleLogs(sender, logs, context) {
7289
var logsType = getLogFormat(logs);
7390
switch (logsType) {
7491
case STRING:
75-
sender(addTagsToStringLog)(logs);
92+
sender(addTagsToStringLog, logs, context);
7693
break;
7794
case JSON_STRING:
7895
logs = JSON.parse(logs);
79-
sender(addTagsToJsonLog)(logs);
96+
sender(addTagsToJsonLog, logs, context);
8097
break;
8198
case JSON_OBJECT:
82-
sender(addTagsToJsonLog)(logs);
99+
sender(addTagsToJsonLog, logs, context);
83100
break;
84101
case STRING_ARRAY:
85-
logs.forEach(sender(addTagsToStringLog));
102+
logs.forEach(log => {
103+
sender(addTagsToStringLog, log, context);
104+
});
86105
break;
87106
case JSON_ARRAY:
88107
handleJSONArrayLogs(sender, context, logs, JSON_ARRAY);
@@ -104,14 +123,16 @@ function handleJSONArrayLogs(sender, context, logs, logsType) {
104123
message = JSON.parse(message);
105124
} catch (err) {
106125
context.log.warn('log is malformed json, sending as string');
107-
sender(addTagsToStringLog)(message);
126+
sender(addTagsToStringLog, message, context);
108127
return;
109128
}
110129
}
111130
if (message.records != undefined) {
112-
message.records.forEach(sender(addTagsToJsonLog));
131+
message.records.forEach(log => {
132+
sender(addTagsToJsonLog, log, context);
133+
});
113134
} else {
114-
sender(addTagsToJsonLog)(message);
135+
sender(addTagsToJsonLog, message, context);
115136
}
116137
});
117138
}

0 commit comments

Comments
 (0)