Last active
February 4, 2019 23:22
-
-
Save lemonmade/e3efb9d2db7a0c3f653599e87c42473b to your computer and use it in GitHub Desktop.
Web DataDog metrics
This file contains hidden or 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
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