-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(health): expose running app version metadata #4757
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
Open
byigitt
wants to merge
2
commits into
simstudioai:main
Choose a base branch
from
byigitt:byigitt/issue-2014-health-version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,17 +1,56 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import appPackage from '@/package.json' | ||
| import { NextRequest } from 'next/server' | ||
| import { afterEach, describe, expect, it } from 'vitest' | ||
| import { GET } from '@/app/api/health/route' | ||
|
|
||
| afterEach(() => { | ||
| process.env.APP_VERSION = '' | ||
| process.env.NEXT_PUBLIC_APP_VERSION = '' | ||
| process.env.GIT_SHA = '' | ||
| process.env.VERCEL_GIT_COMMIT_SHA = '' | ||
| process.env.COMMIT_SHA = '' | ||
| }) | ||
|
|
||
| describe('GET /api/health', () => { | ||
| it('returns an ok status payload', async () => { | ||
| const response = await GET() | ||
| it('returns status with runtime version metadata', async () => { | ||
| process.env.APP_VERSION = 'v1.2.3' | ||
| process.env.GIT_SHA = 'abc123' | ||
|
|
||
| const response = await GET(new NextRequest('http://localhost/api/health')) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| await expect(response.json()).resolves.toEqual({ | ||
| status: 'ok', | ||
| timestamp: expect.any(String), | ||
| version: 'v1.2.3', | ||
| commit: 'abc123', | ||
| }) | ||
| }) | ||
|
|
||
| it('accepts query parameters from health-checking clients', async () => { | ||
| const response = await GET(new NextRequest('http://localhost/api/health?_=123')) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| }) | ||
|
|
||
| it('falls back to the package version when runtime metadata is not provided', async () => { | ||
| process.env.APP_VERSION = '' | ||
| process.env.NEXT_PUBLIC_APP_VERSION = '' | ||
| process.env.GIT_SHA = '' | ||
| process.env.VERCEL_GIT_COMMIT_SHA = '' | ||
| process.env.COMMIT_SHA = '' | ||
|
|
||
| const response = await GET(new NextRequest('http://localhost/api/health')) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| await expect(response.json()).resolves.toEqual({ | ||
| status: 'ok', | ||
| timestamp: expect.any(String), | ||
| version: appPackage.version, | ||
| commit: null, | ||
| }) | ||
| }) | ||
| }) |
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,22 @@ | ||
| import { z } from 'zod' | ||
| import { defineRouteContract } from '@/lib/api/contracts/types' | ||
|
|
||
| const healthQuerySchema = z.object({}).passthrough() | ||
| export const healthResponseSchema = z.object({ | ||
| status: z.literal('ok'), | ||
| timestamp: z.string(), | ||
| version: z.string(), | ||
| commit: z.string().nullable(), | ||
| }) | ||
|
|
||
| export type HealthResponse = z.output<typeof healthResponseSchema> | ||
|
|
||
| export const healthContract = defineRouteContract({ | ||
| method: 'GET', | ||
| path: '/api/health', | ||
| query: healthQuerySchema, | ||
| response: { | ||
| mode: 'json', | ||
| schema: healthResponseSchema, | ||
| }, | ||
| }) |
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
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.
healthContractusesnoInputSchemawhich isz.object({}).strict(). Any request carrying unexpected query parameters — e.g.?_=1234567890(cache-busting),?format=json(monitoring tools), or any debugging param — will fail Zod strict validation and return a 400 before the health payload is produced. The previous handler accepted norequestargument at all and would return 200 unconditionally. Kubernetes liveness/readiness probes, uptime checkers, and other health-poll tools sometimes append arbitrary params; this change silently turns those into "unhealthy" signals.Note also that
parsed.datais never read inside the handler — the contract's query validation is pure overhead here. Consider removing theparseRequestcall entirely, or using a passthrough/permissive query schema, to preserve the unconditional 200 behaviour that health endpoints are expected to provide.