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

Feat/fetch retry and error handling #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Slightly naive retry mechanism
TheMagoo73 committed Aug 17, 2020

Verified

This commit was signed with the committer’s verified signature.
flaeppe Petter Friberg
commit 7dc5f1b43403abb183dedc0244467bfe37f8dafc
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ $ yarn add datadog-winston
- **ddsource**: The technology from which the logs originated
- **ddtags**: Metadata assoicated with the logs
- **intakeRegion**: The datadog intake to use. set to `eu` to force logs to be sent to the EU specific intake
- **retries**: The number of retry attempts to make sending logs to DataDog if there are communication errors. Defaults to 0, i.e. no retries

## Usage
```javascript
@@ -36,7 +37,8 @@ logger.add(
hostname: 'my_machine',
service: 'super_service',
ddsource: 'nodejs',
ddtags: 'foo:bar,boo:baz'
ddtags: 'foo:bar,boo:baz',
retries: 5
})
)
```
25 changes: 17 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Transport = require('winston-transport')
const querystring = require('querystring')
const retry = require('async-retry')
const fetch = require('node-fetch')

/**
@@ -74,15 +75,23 @@ module.exports = class DatadogTransport extends Transport {
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)
})
retry(async bail => {
const res = await fetch(api, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(logs)
})

// TODO: Bail if the status code means we shouldn't retry (e.g. 401)

if (res.statusCode > 299) {
throw new Error(`DataDog API error ${res.statusCode}`)
}
}, { retries: this.opts.retries || 0 })
} catch (err) {
// Just throw the error away as there's nothing more we can do
} finally {
callback()
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.4.5",
"async-retry": "^1.3.1",
"node-fetch": "^2.6.0",
"winston-transport": "^4.3.0"
},
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -1052,6 +1052,13 @@ async-limiter@~1.0.0:
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==

async-retry@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.3.1.tgz#139f31f8ddce50c0870b0ba558a6079684aaed55"
integrity sha512-aiieFW/7h3hY0Bq5d+ktDBejxuwR78vRu9hDUdR8rNhSaQ29VzPL4AoIRG7D/c7tdenwOcKvgPM6tIxB3cB6HA==
dependencies:
retry "0.12.0"

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
@@ -4279,6 +4286,11 @@ ret@~0.1.10:
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==

retry@0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b"
integrity sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=

rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.3, rimraf@~2.6.2:
version "2.6.3"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
@@ -5104,4 +5116,4 @@ yargs@^12.0.2:
string-width "^2.0.0"
which-module "^2.0.0"
y18n "^3.2.1 || ^4.0.0"
yargs-parser "^11.1.1"
yargs-parser "^11.1.1"