Skip to content

Instantly share code, notes, and snippets.

@lemonmade
Last active February 4, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemonmade/e3efb9d2db7a0c3f653599e87c42473b to your computer and use it in GitHub Desktop.
Save lemonmade/e3efb9d2db7a0c3f653599e87c42473b to your computer and use it in GitHub Desktop.
Web DataDog metrics
import {StatsD} from 'hot-shots';
const statsd = new Client({
host: process.env.STATSD_HOST,
port: process.env.STATSD_PORT,
prefix: 'ShopifyWeb.',
});
// An array of objects with some details about browser events
// (time to first byte, first paint, etc)
const events = [];
const metrics: [string, any, {[key: string]: string}][] = [];
// Formats all tags into snake case
function formatTags(object: Dictionary<any>) {
const output: Dictionary<string> = {};
for (const [key, value] of Object.entries(object)) {
output[snakeCase(key)] = value == null ? 'unknown' : String(value);
}
return output;
}
const tags = {
...formatTags({browserConnectionType: connection.effectiveType}),
...formatTags(additionalTags),
};
for (const event of events) {
metrics.push([eventMetricName(event), Math.round(event.start), tags]);
}
const distributions = metrics.map(([metric, value, tags]) => {
if (development) {
statsLogger.log(`Skipping sending metric in dev ${metric}: ${value}`);
} else {
return statsd.distribution(metric, value, tags);
}
});
await Promise.all(distributions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment