File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import type express from "express" ;
22import { LogId } from "../common/logging/loggingDefinitions.js" ;
3+ import { packageInfo } from "../common/packageInfo.js" ;
34import type {
45 DefaultMetrics ,
56 MonitoringServerFeature ,
@@ -53,7 +54,14 @@ export class MonitoringServer<TMetrics extends DefaultMetrics = DefaultMetrics>
5354 protected override setupRoutes ( ) : Promise < void > {
5455 if ( this . features . includes ( "health-check" ) ) {
5556 this . app . get ( "/health" , ( _req : express . Request , res : express . Response ) => {
56- res . json ( { status : "ok" } ) ;
57+ // Health responses should never be cached by proxies or load balancers.
58+ res . set ( "Cache-Control" , "no-store" ) ;
59+ res . json ( {
60+ status : "ok" ,
61+ version : packageInfo . version ,
62+ uptimeSeconds : Math . floor ( process . uptime ( ) ) ,
63+ timestamp : new Date ( ) . toISOString ( ) ,
64+ } ) ;
5765 } ) ;
5866 }
5967
Original file line number Diff line number Diff line change @@ -47,9 +47,20 @@ describe("MonitoringServer", () => {
4747
4848 const response = await fetch ( `${ server . serverAddress } /health` ) ;
4949 expect ( response . status ) . toBe ( 200 ) ;
50- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
51- const body = await response . json ( ) ;
52- expect ( body ) . toEqual ( { status : "ok" } ) ;
50+ expect ( response . headers . get ( "cache-control" ) ) . toBe ( "no-store" ) ;
51+ const body = ( await response . json ( ) ) as {
52+ status : string ;
53+ version : string ;
54+ uptimeSeconds : number ;
55+ timestamp : string ;
56+ } ;
57+ // status remains "ok" for backward compatibility with existing probes
58+ expect ( body . status ) . toBe ( "ok" ) ;
59+ expect ( typeof body . version ) . toBe ( "string" ) ;
60+ expect ( typeof body . uptimeSeconds ) . toBe ( "number" ) ;
61+ expect ( body . uptimeSeconds ) . toBeGreaterThanOrEqual ( 0 ) ;
62+ // timestamp is a valid ISO date string
63+ expect ( Number . isNaN ( Date . parse ( body . timestamp ) ) ) . toBe ( false ) ;
5364 } ) ;
5465
5566 it ( "does not expose health endpoint when health-check feature is disabled" , async ( ) => {
You can’t perform that action at this time.
0 commit comments