Skip to content

perf: optimize AlertHistory queries (HDX-3706) - #1928

Merged
dhable merged 6 commits into
mainfrom
dan/hdx-3706-perf-issue-alerting-history-query
Mar 17, 2026
Merged

dhable merged 6 commits into
mainfrom
dan/hdx-3706-perf-issue-alerting-history-query

Conversation

@dhable

@dhable dhable commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • getRecentAlertHistories: Add interval-based createdAt lower bound to $match (scans only limit * interval instead of the full 30-day TTL window) and insert $sort: { createdAt: -1 } before $group to enable the streaming group optimization using the existing { alert: 1, createdAt: -1 } index. For a 1-minute alert with limit 20, this reduces the scan from ~43k docs to ~20 docs (~2,160x).
  • getPreviousAlertHistories: Add a new { alert: 1, group: 1, createdAt: -1 } compound index and align the $sort stage to match it, so the $group/$first pattern can leverage the index directly. Also add a 7-day createdAt lower bound to the $match stage (~4x scan reduction vs. 30-day TTL).
  • Update tests to use Date.now()-relative timestamps and pass the new interval parameter.

Test plan

  • alertHistory.test.ts integration tests pass (9/9)
  • checkAlerts.test.ts integration tests (existing tests use dates within the 7-day window relative to their now parameter, so no changes needed)
  • Verify compound index creation on staging/dev MongoDB

Made with Cursor

dhable added 4 commits March 17, 2026 11:34
Limit the $match scan to `limit * interval` instead of the full 30-day
TTL window. Add $sort by createdAt before $group to enable the streaming
group optimization using the existing { alert: 1, createdAt: -1 } index.

Made-with: Cursor
…lertHistories

Add a new compound index on { alert: 1, group: 1, createdAt: -1 } to
directly support the $sort + $group/$first pattern used by
getPreviousAlertHistories. Update the $sort stage to match the new index.
The existing { alert: 1, createdAt: -1 } index is kept for
getRecentAlertHistories.

Made-with: Cursor
Limit the $match scan to the last 7 days instead of the full 30-day TTL
window. The check task runs every minute so the latest entry is normally
~1 minute old; 7 days provides ample margin for extended system outages.

Made-with: Cursor
Use Date.now()-relative timestamps instead of hardcoded 2024 dates so
test data falls within the new interval-based lookback window. Add the
required interval parameter to all getRecentAlertHistories calls.

Made-with: Cursor
@changeset-bot

changeset-bot Bot commented Mar 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 59135a8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@hyperdx/api Patch
@hyperdx/app Patch
@hyperdx/otel-collector Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Mar 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hyperdx-oss Ready Ready Preview, Comment Mar 17, 2026 6:27pm

Request Review

@github-actions

github-actions Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

E2E Test Results

✅ All tests passed • 90 passed • 3 skipped • 1017s

Status Count
✅ Passed 90
❌ Failed 0
⚠️ Flaky 3
⏭️ Skipped 3

Tests ran across 4 shards in parallel.

View full report →

@github-actions

github-actions Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

PR Review

✅ The implementation is correct and the performance optimizations are sound. No critical bugs or security issues found.

One minor concern worth acknowledging:

  • ⚠️ Lookback window in getRecentAlertHistories is a hard cutoff — if alert processing has a gap longer than limit * interval (e.g., system downtime), real history within the 30-day TTL will be silently excluded from the UI display. This is an intentional trade-off for the ~2,160x scan reduction, but consider whether the UI should indicate when results may be truncated.

  • 📋 New compound index { alert: 1, group: 1, createdAt: -1 } production rollout — index creation on large collections can temporarily impact MongoDB performance. The PR checklist only mentions staging/dev verification. Ensure the production deployment plan accounts for background index build monitoring.

Everything else (sort ordering, streaming group optimization alignment, null group handling in the index, chunk size for $in queries) looks correct.

@wrn14897 wrn14897 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good optimization

@dhable
dhable merged commit a345b83 into main Mar 17, 2026
15 checks passed
@dhable
dhable deleted the dan/hdx-3706-perf-issue-alerting-history-query branch March 17, 2026 18:34
knudtty pushed a commit that referenced this pull request Apr 16, 2026
- **`getRecentAlertHistories`**: Add interval-based `createdAt` lower bound to `$match` (scans only `limit * interval` instead of the full 30-day TTL window) and insert `$sort: { createdAt: -1 }` before `$group` to enable the streaming group optimization using the existing `{ alert: 1, createdAt: -1 }` index. For a 1-minute alert with limit 20, this reduces the scan from ~43k docs to ~20 docs (~2,160x).
- **`getPreviousAlertHistories`**: Add a new `{ alert: 1, group: 1, createdAt: -1 }` compound index and align the `$sort` stage to match it, so the `$group`/`$first` pattern can leverage the index directly. Also add a 7-day `createdAt` lower bound to the `$match` stage (~4x scan reduction vs. 30-day TTL).
- Update tests to use `Date.now()`-relative timestamps and pass the new `interval` parameter.

This branch was successfully deployed

1 active deployment
Preview — 59135a80 Deployed Mar 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants