Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, jest, expect, it */
/* global describe, expect, it */

const Transport = require('winston-transport')
const DatadogTransport = require('../lib')
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('DatadogTransport#log(info, callback)', () => {
}
]
.forEach(testCase => {
it(testCase.case, async () => {
it(testCase.case, callback => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why though?

Copy link
Author

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).

Copy link
Author

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)

const scope = nock(testCase.uri,
{
reqheaders: {
Expand Down Expand Up @@ -70,16 +70,14 @@ describe('DatadogTransport#log(info, callback)', () => {
}, testCase.opts ? testCase.opts : {})

const transport = new DatadogTransport(opts)
const callback = jest.fn()
await transport.log({
transport.log({
dd: {
trace_id: 'abc',
span_id: 'def'
},
foo: 'bar',
ddtags: 'tag_a:value_a,tag_b:value_b'
}, callback)
expect(callback).toHaveBeenCalled()
expect(scope.isDone()).toBe(true)
})
})
Expand Down
27 changes: 14 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback()
}
}, (err) => {
this.emit('error', err)
callback()
})
}
}