Skip to content

Commit c4aa4e9

Browse files
Peter Bengtssonrsese
andauthored
Measure repeat backend calls for the same pageinfo (#43341)
Co-authored-by: Robert Sese <734194+rsese@users.noreply.github.com>
1 parent 05bd510 commit c4aa4e9

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/pageinfo/middleware.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ const pageinfoMiddleware = (req, res, next) => {
8383
return next()
8484
}
8585

86+
// THIS IS AN EXPERIMENT. THIS CODE WILL BE DELETED IN THE NEAR FUTURE.
87+
// The cache-control headers (and surrogate-control) are set to be
88+
// aggressive. That means that in theory, after a deployment, when Fastly
89+
// has been purged accordingly, the next time a pageinfo is requested,
90+
// it'll come here to the backend code and produce the JSON response,
91+
// and once it's done it once, it won't be needed again, because,
92+
// in theory, the CDN will have cached it.
93+
// But pageinfo requests are very frequent. So frequent that sometimes,
94+
// the delay of CDN is longer than the chance of it being requested again
95+
// from the backend. This can possibly still happen even with a origin
96+
// shield.
97+
// In this experiment we want to measure how often this happens.
98+
// We are going to test how often the CDN requests the *same* pageinfo from the
99+
// backend.
100+
const CACHEABLE_PATHNAMES = new Map()
101+
86102
router.get(
87103
'/v1',
88104
validationMiddleware,
@@ -159,8 +175,20 @@ router.get(
159175
intro,
160176
}
161177

162-
const tags = ['version:v1', `pathname:${pathname}`]
163-
statsd.increment('api.pageinfo', 1, tags)
178+
const tags = [
179+
// According to https://docs.datadoghq.com/getting_started/tagging/#define-tags
180+
// the max length of a tag is 200 characters. Most of ours are less than
181+
// that but we truncate just to be safe.
182+
`pathname:${pathname}`.slice(0, 200),
183+
]
184+
statsd.increment('pageinfo.lookup', 1, tags)
185+
186+
// See lengthy comment above about the experiment and the
187+
// CACHEABLE_PATHNAMES global Map object.
188+
if (CACHEABLE_PATHNAMES.has(pathname)) {
189+
statsd.increment('pageinfo.cacheable', CACHEABLE_PATHNAMES.get(pathname), tags)
190+
}
191+
CACHEABLE_PATHNAMES.set(pathname, (CACHEABLE_PATHNAMES.get(pathname) || 0) + 1)
164192

165193
defaultCacheControl(res)
166194

0 commit comments

Comments
 (0)