Last active
December 28, 2017 15:17
-
-
Save luqmaan/f5c046c33d871a86e29a792cc9c924ab to your computer and use it in GitHub Desktop.
Use hot-shots with promises. https://github.com/brightcove/hot-shots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const StatsD = require("hot-shots"); | |
const statsdclient = new StatsD(); | |
const properties = Object.getOwnPropertyNames( | |
Object.getPrototypeOf(statsdclient) | |
); | |
const client = properties.reduce((prev, property) => { | |
if (property === "constructor") { | |
return prev; | |
} | |
prev[property] = (...args) => { | |
return new Promise((resolve, reject) => { | |
try { | |
statsdclient[property](...args, (err, ...rest) => { | |
if (err) { | |
reject(err, ...rest); | |
} else { | |
resolve(...rest); | |
} | |
}); | |
} catch (err) { | |
reject(err); | |
} | |
}); | |
}; | |
return prev; | |
}, {}); | |
module.exports = client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const statsd = require('./hot-shots-with-promises'); | |
await statsd.increment("bob"); | |
await statsd.increment("bob", ["asdf:1", "site:bob.com"]); | |
await statsd.histogram("bob", 6, ["asdf:1", "site:bob.com"]); | |
await statsd.close(); | |
try { | |
await statsd.histogram("bob", 6, ["asdf:1", "site:bob.com"]); | |
} catch (err) { | |
expect(err.message).toContain('Error sending hot-shots message: Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment