-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dont use async #26
base: master
Are you sure you want to change the base?
dont use async #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ module.exports = class DatadogTransport extends Transport { | |
* @param {!Object} info Information to be logged | ||
* @param {function} callback Continuation to respond when complete | ||
*/ | ||
async log (info, callback) { | ||
log (info, callback) { | ||
setImmediate(() => { | ||
this.emit('logged', info) | ||
}) | ||
|
@@ -73,18 +73,19 @@ module.exports = class DatadogTransport extends Transport { | |
const queryString = querystring.encode(query) | ||
const api = querystring ? `${this.api}?${queryString}` : this.api | ||
|
||
try { | ||
// Perform the writing to the remote service | ||
await fetch(api, { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify(logs) | ||
}) | ||
} catch (err) { | ||
} finally { | ||
// Perform the writing to the remote service | ||
fetch(api, { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify(logs) | ||
}).then(() => { | ||
this.emit('logged', info) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These new events were influenced by https://github.com/adrianhall/winston-splunk-httplogger/blob/main/index.js#L148-L151, https://github.com/TheAppleFreak/winston-slack-webhook-transport/blob/master/slackHook.js#L45-L52 and https://github.com/winstonjs/winston/blob/master/lib/winston/transports/http.js#L57-L61. They also feel useful for supporting #21 |
||
callback() | ||
} | ||
}, (err) => { | ||
this.emit('error', err) | ||
callback() | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See winstonjs/winston#1532. Winston transports do not support async log functions
This appears to work fine as-is for the most part, but in corner cases where the program may be terminated soon after trying to send a log, you are not currently able to rely on winston's documented feature of a 'finish' event being emitted (I suspect because the callback is immediately executed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This specific line is a jest feature to allow the jest test execution to wait until the callback is executed (or timeout and fail the test). It appears to be the recommended way to test callback based functions, and this was also required to be able to test the new function (without
async
/await
)