Skip to content

Add query and metrics endpoints for grafana - #528

Merged
simonaspiridon merged 22 commits into
mainfrom
feature/add-grafana-endpoints
Sep 17, 2026
Merged

simonaspiridon merged 22 commits into
mainfrom
feature/add-grafana-endpoints

Conversation

@simonaspiridon

@simonaspiridon simonaspiridon commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

What does this change?

Add 2 new endpoints to be consumed by grafana: /metrics and /query
These will pull data from our database to grafana to show stats about notifications that have been sent.
This PR also adds CORS and adds Grafana to the allowed origins.

These are the columns we will display in the grafana visualistion:

1. Sent At            (time)   — notification.createdAt
2. Notification ID    (string) — notification.id
3. Channel            (string) — keys of notification.channels (e.g. "app-push, newsletter")
4. Created By         (string) — notification.createdByEmail
5. Notification Status (string) — notification.status (e.g. "partially_delivered")
6. Failed Audiences   (string) — notification.failedTargets (e.g. "breaking-news [uk], UK")
7. Errors             (string) — failed dispatches: audience: reason (code); ... (e.g. "UK: braze_rejected (400); breaking-news [uk]: http_error (500)")

How has this change been tested?

I can't seem to be able to test on CODE, there seems to be a problem with the cookie which does not get sent when Grafana makes the request to CODE.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

Copilot AI 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.

🟡 Changes recommended

CORS scope, query correctness, input handling, scalability, API contract, and lockfile issues remain unresolved.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds authenticated Grafana datasource endpoints for querying notification delivery data.

Changes:

  • Adds /metrics and /query endpoints backed by notification dispatch data.
  • Adds credentialed Grafana CORS support.
  • Adds OpenAPI documentation and endpoint tests.
File summaries
File Description
src/packages/database/repositories/notifications-repository.ts Adds windowed notification queries with dispatches.
src/packages/database/index.ts Exports the new query options type.
src/apps/backend/utils/test-utils/database.ts Adds a repository mock.
src/apps/backend/routers/grafana/index.ts Implements Grafana endpoints.
src/apps/backend/routers/grafana/index.test.ts Tests authentication, CORS, and responses.
src/apps/backend/routers/docs/openapi/paths/index.ts Registers Grafana paths.
src/apps/backend/routers/docs/openapi/paths/grafana.ts Documents Grafana operations.
src/apps/backend/routers/docs/openapi/components/schemas/index.ts Registers Grafana schemas.
src/apps/backend/routers/docs/openapi/components/schemas/grafana-query.ts Defines query payload schemas.
src/apps/backend/routers/docs/openapi/components/schemas/grafana-metrics.ts Defines metrics schema.
src/apps/backend/package.json Adds CORS dependencies.
src/apps/backend/middleware/grafana-cors.ts Configures allowed Grafana origins.
src/apps/backend/app.ts Mounts CORS and Grafana routes.
bun.lock Locks CORS dependencies.
Review details
  • Files reviewed: 13/14 changed files
  • Comments generated: 7
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/apps/backend/app.ts Outdated
Comment thread src/apps/backend/routers/docs/openapi/components/schemas/grafana-query.ts Outdated
Comment thread src/apps/backend/routers/grafana/index.ts Outdated
Comment thread src/apps/backend/routers/grafana/index.ts Outdated
Comment thread src/packages/database/repositories/notifications-repository.ts
Comment thread src/packages/database/repositories/notifications-repository.ts
Comment thread src/packages/database/repositories/notifications-repository.ts
simonaspiridon and others added 8 commits September 9, 2026 16:33
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@simonaspiridon
simonaspiridon marked this pull request as ready for review September 10, 2026 11:10
@simonaspiridon
simonaspiridon requested a review from a team as a code owner September 10, 2026 11:10
@groakland groakland added the maintenance Departmental tracking: maintenance work, not a fix or a feature label Sep 14, 2026
).listSendsWithDispatchesInWindow({
from: dateRange.from,
to: dateRange.to,
limit: maxNotifications,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we know if this limit is likely to be reached? If it is, should we signal that the results were truncated so Grafana doesn’t present incomplete data as complete?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think we might reach the hard limit if the user selects a wide timeframe. I am adding a meta object containing a message that Grafana should use it when results are truncated (haven't tested it though)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If there are exactly 5,000 results, we show a truncation warning even though none were omitted. Could we fetch maxNotifications + 1, use the extra row to detect truncation, and return only the first 5,000?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great catch!

Comment thread src/apps/backend/app.ts
Comment thread src/apps/backend/routers/grafana/index.ts Outdated
Comment thread src/apps/backend/routers/grafana/index.ts Outdated
Comment thread src/apps/backend/routers/grafana/index.ts Outdated
Co-authored-by: mgosz-guardian <marcin.gosz@theguardian.com>
@simonaspiridon
simonaspiridon merged commit c4d2ea6 into main Sep 17, 2026
5 checks passed
@simonaspiridon
simonaspiridon deleted the feature/add-grafana-endpoints branch September 17, 2026 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Departmental tracking: maintenance work, not a fix or a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Grafana Visualisation to show detailed view of the notifications send by environment

4 participants