Skip to content

Commit 0730158

Browse files
JasonEtcoheiskr
andauthored
Datadoge (#16464)
* Install hot-shots * Add statsd client * Add some metrics * Add a comment * Comment typo fix Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
1 parent 45398e7 commit 0730158

5 files changed

Lines changed: 41 additions & 1 deletion

File tree

lib/page.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const renderContent = require('./render-content')
1919
const frontmatter = require('./frontmatter')
2020
const products = require('./all-products')
2121
const slash = require('slash')
22+
const statsd = require('./statsd')
2223

2324
class Page {
2425
constructor (opts) {
@@ -84,6 +85,10 @@ class Page {
8485
: true
8586
}
8687

88+
// Instrument the `_render` method, so externally we call #render
89+
// but it's wrapped in a timer that reports to Datadog
90+
this.render = statsd.asyncTimer(this._render.bind(this), 'page.render')
91+
8792
return this
8893
}
8994

@@ -116,7 +121,7 @@ class Page {
116121
: this.renderProp('title', context, opts)
117122
}
118123

119-
async render (context) {
124+
async _render (context) {
120125
this.intro = await renderContent(this.rawIntro, context)
121126
this.introPlainText = await renderContent(this.rawIntro, context, { textOnly: true })
122127
this.title = await renderContent(this.rawTitle, context, { textOnly: true, encodeEntities: true })

lib/statsd.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const StatsD = require('hot-shots')
2+
3+
const mock = Boolean(process.env.NODE_ENV === 'test' || !process.env.DATADOG_API_KEY)
4+
5+
/**
6+
* @type {import('hot-shots').StatsD}
7+
*/
8+
module.exports = new StatsD({
9+
prefix: 'docs.',
10+
mock,
11+
globalTags: {
12+
app: 'docs'
13+
}
14+
})

middleware/render-page.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const patterns = require('../lib/patterns')
55
const layouts = require('../lib/layouts')
66
const getMiniTocItems = require('../lib/get-mini-toc-items')
77
const Page = require('../lib/page')
8+
const statsd = require('../lib/statsd')
89

910
// We've got lots of memory, let's use it
1011
// We can eventually throw this into redis
@@ -18,6 +19,7 @@ module.exports = async function renderPage (req, res, next) {
1819
if (!process.env.CI && process.env.NODE_ENV !== 'test') {
1920
if (req.method === 'GET' && pageCache[originalUrl]) {
2021
console.log(`Serving from cached version of ${originalUrl}`)
22+
statsd.increment('page.sent_from_cache')
2123
return res.send(pageCache[originalUrl])
2224
}
2325
}

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"got": "^9.6.0",
4343
"gray-matter": "^4.0.1",
4444
"helmet": "^3.21.2",
45+
"hot-shots": "^8.2.0",
4546
"html-entities": "^1.2.1",
4647
"html-truncate": "^1.2.2",
4748
"imurmurhash": "^0.1.4",

0 commit comments

Comments
 (0)