Skip to content

Commit bb0bcee

Browse files
authored
Add counters to datadog for /api/article and /api/pagelist (#54743)
1 parent 7049fbc commit bb0bcee

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/article-api/middleware/article.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import catchMiddlewareError from '@/observability/middleware/catch-middleware-er
77
import { ExtendedRequestWithPageInfo } from '../types'
88
import { pageValidationMiddleware, pathValidationMiddleware } from './validation'
99
import contextualize from '#src/frame/middleware/context/context.js'
10+
import statsd from '#src/observability/lib/statsd.js'
1011

1112
/** START helper functions */
1213

@@ -59,6 +60,23 @@ async function getArticleBody(req: ExtendedRequestWithPageInfo) {
5960
return await page.render(renderingReq.context)
6061
}
6162

63+
function incrementArticleLookup(
64+
pathname: string,
65+
language: string,
66+
type: 'full' | 'body' | 'meta',
67+
) {
68+
const tags = [
69+
// According to https://docs.datadoghq.com/getting_started/tagging/#define-tags
70+
// the max length of a tag is 200 characters. Most of ours are less than
71+
// that but we truncate just to be safe.
72+
`pathname:${pathname}`.slice(0, 200),
73+
`language:${language}`,
74+
`type:${type}`,
75+
]
76+
77+
statsd.increment('api.article.lookup', 1, tags)
78+
}
79+
6280
/** END helper functions */
6381

6482
/** START routes */
@@ -82,6 +100,8 @@ router.get(
82100
return res.status(403).json({ error: (error as Error).message })
83101
}
84102

103+
incrementArticleLookup(req.pageinfo.pathname, req.pageinfo.page.languageCode, 'full')
104+
85105
defaultCacheControl(res)
86106
return res.json({
87107
meta: metaData,
@@ -101,6 +121,9 @@ router.get(
101121
} catch (error) {
102122
return res.status(403).json({ error: (error as Error).message })
103123
}
124+
125+
incrementArticleLookup(req.pageinfo.pathname, req.pageinfo.page.languageCode, 'body')
126+
104127
defaultCacheControl(res)
105128
return res.type('text/markdown').send(bodyContent)
106129
}),
@@ -112,6 +135,9 @@ router.get(
112135
pageValidationMiddleware as RequestHandler,
113136
catchMiddlewareError(async function (req: ExtendedRequestWithPageInfo, res: Response) {
114137
const metaData = await getArticleMetadata(req)
138+
139+
incrementArticleLookup(req.pageinfo.pathname, req.pageinfo.page.languageCode, 'meta')
140+
115141
defaultCacheControl(res)
116142
return res.json(metaData)
117143
}),

src/article-api/middleware/pagelist.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getProductStringFromPath, getVersionStringFromPath } from '#src/frame/l
77
import { getLanguageCodeFromPath } from '#src/languages/middleware/detect-language.js'
88
import { pagelistValidationMiddleware } from './validation'
99
import catchMiddlewareError from '#src/observability/middleware/catch-middleware-error.js'
10+
import statsd from '#src/observability/lib/statsd.js'
1011

1112
const router = express.Router()
1213

@@ -78,6 +79,7 @@ router.get(
7879
return
7980
}
8081

82+
incrementPagelistLookup(req.context!.currentVersion!, req.context!.currentLanguage!)
8183
defaultCacheControl(res)
8284

8385
// new line added at the end so `wc` works as expected with `-l` and `-w`.
@@ -101,4 +103,10 @@ function versionMatcher(key: string, targetVersion: string, targetLang: string)
101103
if (versionFromPermalink === targetVersion && langFromPermalink === targetLang) return key
102104
}
103105

106+
function incrementPagelistLookup(version: string, language: string) {
107+
const tags = [`version:${version}`, `language:${language}`]
108+
109+
statsd.increment('api.pagelist.lookup', 1, tags)
110+
}
111+
104112
export default router

0 commit comments

Comments
 (0)