Skip to content

Instantly share code, notes, and snippets.

@luqmaan
Last active December 28, 2017 15:17
Show Gist options
  • Save luqmaan/f5c046c33d871a86e29a792cc9c924ab to your computer and use it in GitHub Desktop.
Save luqmaan/f5c046c33d871a86e29a792cc9c924ab to your computer and use it in GitHub Desktop.
Use hot-shots with promises. https://github.com/brightcove/hot-shots
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;
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