-
Notifications
You must be signed in to change notification settings - Fork 0
feat(plugin): bot_serve + page_age analytics — rollout success metrics; v0.26.0 #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { test, before, beforeEach } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
|
|
||
| /** | ||
| * recordServeOutcome — the serve-outcome analytics behind the rollout success metrics. | ||
| * | ||
| * The properties pinned here: | ||
| * - `bot_serve` dimension ORDER is (source, cacheStatus, botName). Dashboards key on the | ||
| * positional path/method/type triple Harper builds from these, so reordering is a silent | ||
| * breaking change. | ||
| * - `page_age` is recorded ONLY for a cache-served resource (source === 'cache'), so | ||
| * render-now responses never drag the freshness distribution toward zero. | ||
| * - lastCached may arrive as a Date, a number, or a serialized string — all must yield the | ||
| * same age. A missing value (NaN) or a negative age (cross-node clock skew) records | ||
| * nothing rather than poisoning the mean. | ||
| */ | ||
|
|
||
| let analytics = []; | ||
| let recordServeOutcome; | ||
|
|
||
| before(async () => { | ||
| // bot_request.js transitively imports the Harper resource classes; stub the runtime | ||
| // bindings, then dynamic-import so the stubs are in place before module evaluation. | ||
| globalThis.Resource = class {}; | ||
| globalThis.server = { | ||
| hostname: 'test-node', | ||
| nodes: [], | ||
| config: { http: { port: 9926 } }, | ||
| recordAnalytics: (...args) => analytics.push(args), | ||
| }; | ||
| globalThis.logger = { info() {}, warn() {}, error() {} }; | ||
| globalThis.databases = { | ||
| coordination: { | ||
| SharedBuffer: { | ||
| primaryStore: { | ||
| getUserSharedBuffer: (_key, buf) => buf, | ||
| tryLock: () => true, | ||
| unlock() {}, | ||
| }, | ||
| }, | ||
| }, | ||
| render_service: { Target: class {}, QueueControl: class {} }, | ||
| render_schedule: { RenderSchedule: class {} }, | ||
| page_cache: { PrerenderedPage: class {} }, | ||
| }; | ||
| ({ recordServeOutcome } = await import('../src/http_handlers/bot_request.js')); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| analytics = []; | ||
| }); | ||
|
|
||
| const request = { botName: 'Googlebot' }; | ||
|
|
||
| test('bot_serve records (source, cacheStatus, botName) in that order', () => { | ||
| recordServeOutcome({}, request, { source: 'origin', cacheStatus: 'miss' }, 'desktop'); | ||
| assert.deepEqual(analytics, [[true, 'bot_serve', 'origin', 'miss', 'Googlebot']]); | ||
| }); | ||
|
|
||
| test('a cache hit also records page_age with (botName, deviceType)', () => { | ||
| const lastCached = Date.now() - 5000; | ||
| // The three shapes a schema Date reaches this code in. | ||
| for (const value of [new Date(lastCached), lastCached, new Date(lastCached).toISOString()]) { | ||
| analytics = []; | ||
| recordServeOutcome({ lastCached: value }, request, { source: 'cache', cacheStatus: 'hit' }, 'mobile'); | ||
| assert.equal(analytics.length, 2); | ||
| const [age, metric, bot, device] = analytics[1]; | ||
| assert.equal(metric, 'page_age'); | ||
| assert.equal(bot, 'Googlebot'); | ||
| assert.equal(device, 'mobile'); | ||
| assert.ok(age >= 4000 && age <= 7000, `expected age ~5000ms, got ${age}`); | ||
| } | ||
| }); | ||
|
|
||
| test('page_age is skipped for a non-cache source, even with lastCached present', () => { | ||
| recordServeOutcome({ lastCached: Date.now() }, request, { source: 'rendered', cacheStatus: 'miss' }, 'desktop'); | ||
| assert.equal(analytics.length, 1); | ||
| assert.equal(analytics[0][1], 'bot_serve'); | ||
| }); | ||
|
|
||
| test('page_age is skipped when lastCached is missing, null, or in the future', () => { | ||
| recordServeOutcome({}, request, { source: 'cache', cacheStatus: 'hit' }, 'desktop'); | ||
| // null is the trap case: new Date(null) is epoch 0, not NaN — unguarded, this would | ||
| // record age ≈ Date.now() instead of nothing. | ||
| recordServeOutcome({ lastCached: null }, request, { source: 'cache', cacheStatus: 'hit' }, 'desktop'); | ||
| recordServeOutcome({ lastCached: Date.now() + 60_000 }, request, { source: 'cache', cacheStatus: 'hit' }, 'desktop'); | ||
| assert.equal(analytics.length, 3); | ||
| assert.ok(analytics.every(([, metric]) => metric === 'bot_serve')); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In JavaScript,
new Date(null).getTime()returns0rather thanNaN. Ifresource.lastCachedisnull(which is a common representation for missing or unset date fields in database schemas),new Date(resource.lastCached).getTime()will evaluate to0. This results inagebeing calculated asDate.now(), which is a very large positive number. This will poison thepage_agemetric with incorrect, extremely high values.Additionally, to prevent runtime exceptions if
resourceitself is null or undefined, we should use optional chaining (resource?.lastCached).Using a conditional check to ensure
resource?.lastCachedis truthy before parsing avoids both issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in afb6691 — good catch,
new Date(null)is epoch 0 so the NaN reasoning in my comment didn't cover null. Now guarded with the same truthiness-first pattern as theexpiresAtread above it. (Skipped theresource?.chaining:resolveResourcealways returns an object on this path.) Also swept both open PRs for the same class — every other date coercion already guards truthiness first.