forked from microsoft/opensource-management-portal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhealthCheck.ts
194 lines (178 loc) Β· 6 KB
/
healthCheck.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// Copyright (c) Microsoft.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
import Debug from 'debug';
import { IncomingHttpHeaders } from 'http';
import { NextFunction, Response } from 'express';
import type {
ConfiguredHeaderProbe,
ConfiguredGeneralProbe,
ConfiguredProbeBase,
} from '../config/webHealthProbes.types';
import { IReposApplication, ReposAppRequest, SiteConfiguration } from '../interfaces';
import { CreateError } from '../lib/transitional';
const dbg = Debug.debug('health');
const supportedHeaderProbeTypes = ['kubernetes', 'azurefrontdoor'];
const supportedGeneralProbeTypes = ['external', 'azureappservice-windows', 'azureappservice-linux'];
enum HealthProbeType {
Readiness = 'ready',
Liveness = 'healthy',
}
enum ProbeType {
General = 'general',
Header = 'header',
}
export default function initializeHealthCheck(
app: IReposApplication,
config: SiteConfiguration /* WebHealthProbeSubsetConfiguration */
) {
const { webHealthProbes: healthConfig } = config;
const enabledHeaderProbes =
healthConfig?.enabled === true
? supportedHeaderProbeTypes
.map((typeName) => {
const probeConfig = healthConfig[typeName] as ConfiguredHeaderProbe;
return probeConfig?.allowed === true ? probeConfig : null;
})
.filter((configured) => configured)
: [];
const enabledGenericProbes =
healthConfig?.enabled === true
? supportedGeneralProbeTypes
.map((typeName) => {
const probeConfig = healthConfig[typeName] as ConfiguredGeneralProbe;
return probeConfig?.allowed && (probeConfig.endpointSuffix || probeConfig.endpoint)
? probeConfig
: null;
})
.filter((configured) => configured)
: [];
const configuredHealthDelays = {
[HealthProbeType.Readiness]: healthConfig?.delay?.readiness || 0,
[HealthProbeType.Liveness]: healthConfig?.delay?.liveness || 0,
};
function checkHealth(checkType: HealthProbeType) {
const started = app.settings.started;
const startupSeconds = configuredHealthDelays[checkType];
if (configuredHealthDelays[checkType] === undefined || configuredHealthDelays[checkType] === null) {
throw new Error('Invalid health check type');
}
const now = new Date();
const startupMs = startupSeconds * 1000;
if (now.getTime() - started.getTime() <= startupMs) {
// Still in the startup period
dbg(`Returning ${checkType} OK: within the startup delay window`);
return true;
}
const isHealthy = !!provider[checkType as string];
const asString = isHealthy ? 'OK' : 'FALSE';
dbg(`Returning ${checkType} ${asString}`);
return isHealthy;
}
function multipleHeaderHealthCheck(
checkType: HealthProbeType,
probeType: ProbeType,
probeConfigs: ConfiguredHeaderProbe[],
req: ReposAppRequest,
res: Response,
next: NextFunction
) {
for (const probeConfig of probeConfigs) {
if (requestEligibleForCheck(checkType, probeType, probeConfig, req.headers)) {
return returnExpressHealthCheck(checkType, req, res, next);
}
}
return next();
}
function requestEligibleForCheck(
checkType: HealthProbeType,
probeType: ProbeType,
probeConfig: ConfiguredProbeBase,
headers: IncomingHttpHeaders
) {
switch (probeType) {
case ProbeType.General: {
return true;
}
case ProbeType.Header: {
const specificConfig = probeConfig as ConfiguredHeaderProbe;
const { expectedHeader } = specificConfig;
if (!headers[expectedHeader.name]) {
dbg(
`Health probe for ${checkType} skipped: ${expectedHeader.name} header was not present in the HTTP request`
);
return false;
}
if (headers[expectedHeader.name] !== expectedHeader.value) {
dbg(
`Health probe for ${checkType} skipped: requested with the ${
expectedHeader.name
} header. The value "${headers[expectedHeader.name]}" didn't match the expected value of "${
expectedHeader.value
}"`
);
return false;
}
break;
}
default: {
throw CreateError.InvalidParameters(`Invalid health probe type ${probeType}`);
}
}
return true;
}
function returnExpressHealthCheck(
checkType: HealthProbeType,
req: ReposAppRequest,
res: Response,
next: NextFunction
) {
let result = null;
try {
result = checkHealth(checkType);
} catch (error) {
error.statusCode = 500;
return next(error);
}
res.status(result ? 200 : 500).end();
}
const provider = {
ready: false,
healthy: true,
};
if (enabledHeaderProbes.length > 0 && app) {
dbg(`Configured header health probes: ${enabledHeaderProbes.length}`);
app.get(
'/health/readiness',
multipleHeaderHealthCheck.bind(null, HealthProbeType.Readiness, ProbeType.Header, enabledHeaderProbes)
);
app.get(
'/health/liveness',
multipleHeaderHealthCheck.bind(null, HealthProbeType.Liveness, ProbeType.Header, enabledHeaderProbes)
);
}
if (enabledGenericProbes.length > 0 && app) {
// General probes listen on their own type endpoint
for (const genericProbeConfig of enabledGenericProbes) {
const url = genericProbeConfig.endpoint || `/health/${genericProbeConfig.endpointSuffix}`;
dbg(`Configured general health probe: ${url}`);
app.get(
url,
multipleHeaderHealthCheck.bind(null, HealthProbeType.Liveness, ProbeType.General, [
genericProbeConfig,
])
);
}
}
if (app && enabledGenericProbes.length + enabledGenericProbes.length > 0) {
dbg('Health probes listening');
// 404 on anything that was not handled by any active, allowed probe listeners
app.use('/health/*', (req, res) => {
return res.status(404).end();
});
} else {
dbg('No health probes listening');
}
return provider;
}