@friggframework/core 2.0.0-next.25 → 2.0.0-next.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,240 @@
1
+ # Frigg Healthcheck Endpoint Documentation
2
+
3
+ ## Overview
4
+
5
+ The Frigg service includes comprehensive healthcheck endpoints to monitor service health, connectivity, and readiness. These endpoints follow industry best practices and are designed for use with monitoring systems, load balancers, and container orchestration platforms.
6
+
7
+ ## Endpoints
8
+
9
+ ### 1. Basic Health Check
10
+ **GET** `/health`
11
+
12
+ Simple health check endpoint that returns basic service information. No authentication required. This endpoint is rate-limited at the API Gateway level.
13
+
14
+ **Response:**
15
+ ```json
16
+ {
17
+ "status": "ok",
18
+ "timestamp": "2024-01-10T12:00:00.000Z",
19
+ "service": "frigg-core-api"
20
+ }
21
+ ```
22
+
23
+ **Status Codes:**
24
+ - `200 OK` - Service is running
25
+
26
+ ### 2. Detailed Health Check
27
+ **GET** `/health/detailed`
28
+
29
+ Comprehensive health check that tests all service components and dependencies.
30
+
31
+ **Authentication Required:**
32
+ - Header: `x-api-key: YOUR_API_KEY`
33
+ - The API key must match the `HEALTH_API_KEY` environment variable
34
+
35
+ **Response:**
36
+ ```json
37
+ {
38
+ "service": "frigg-core-api",
39
+ "status": "healthy", // "healthy" or "unhealthy"
40
+ "timestamp": "2024-01-10T12:00:00.000Z",
41
+ "checks": {
42
+ "database": {
43
+ "status": "healthy",
44
+ "state": "connected",
45
+ "responseTime": 5 // milliseconds
46
+ },
47
+ "externalApis": {
48
+ "github": {
49
+ "status": "healthy",
50
+ "statusCode": 200,
51
+ "responseTime": 150,
52
+ "reachable": true
53
+ },
54
+ "npm": {
55
+ "status": "healthy",
56
+ "statusCode": 200,
57
+ "responseTime": 200,
58
+ "reachable": true
59
+ }
60
+ },
61
+ "integrations": {
62
+ "status": "healthy",
63
+ "modules": {
64
+ "count": 10,
65
+ "available": ["module1", "module2", "..."]
66
+ },
67
+ "integrations": {
68
+ "count": 5,
69
+ "available": ["integration1", "integration2", "..."]
70
+ }
71
+ }
72
+ },
73
+ "responseTime": 250 // total endpoint response time in milliseconds
74
+ }
75
+ ```
76
+
77
+ **Status Codes:**
78
+ - `200 OK` - Service is healthy (all components operational)
79
+ - `503 Service Unavailable` - Service is unhealthy (any component failure)
80
+ - `401 Unauthorized` - Missing or invalid x-api-key header
81
+
82
+ ### 3. Liveness Probe
83
+ **GET** `/health/live`
84
+
85
+ Kubernetes-style liveness probe. Returns whether the service process is alive.
86
+
87
+ **Authentication Required:**
88
+ - Header: `x-api-key: YOUR_API_KEY`
89
+
90
+ **Response:**
91
+ ```json
92
+ {
93
+ "status": "alive",
94
+ "timestamp": "2024-01-10T12:00:00.000Z"
95
+ }
96
+ ```
97
+
98
+ **Status Codes:**
99
+ - `200 OK` - Service process is alive
100
+
101
+ ### 4. Readiness Probe
102
+ **GET** `/health/ready`
103
+
104
+ Kubernetes-style readiness probe. Returns whether the service is ready to receive traffic.
105
+
106
+ **Authentication Required:**
107
+ - Header: `x-api-key: YOUR_API_KEY`
108
+
109
+ **Response:**
110
+ ```json
111
+ {
112
+ "ready": true,
113
+ "timestamp": "2024-01-10T12:00:00.000Z",
114
+ "checks": {
115
+ "database": true,
116
+ "modules": true
117
+ }
118
+ }
119
+ ```
120
+
121
+ **Status Codes:**
122
+ - `200 OK` - Service is ready
123
+ - `503 Service Unavailable` - Service is not ready
124
+
125
+ ## Health Status Definitions
126
+
127
+ - **healthy**: All components are functioning normally
128
+ - **unhealthy**: Any component is failing, service may not function properly
129
+
130
+ ## Component Checks
131
+
132
+ ### Database Connectivity
133
+ - Checks database connection state
134
+ - Performs ping test with 2-second timeout if connected
135
+ - Reports connection state and response time
136
+ - Database type is not exposed for security reasons
137
+
138
+ ### External API Connectivity
139
+ - Tests connectivity to external services (GitHub, npm registry)
140
+ - Configurable timeout (default: 5 seconds)
141
+ - Reports reachability and response times
142
+ - Uses Promise.all for parallel checking
143
+
144
+ ### Integration Status
145
+ - Verifies available modules and integrations are loaded
146
+ - Reports counts and lists of available components
147
+
148
+ ## Usage Examples
149
+
150
+ ### Monitoring Systems
151
+ Configure your monitoring system to poll `/health/detailed` every 30-60 seconds:
152
+ ```bash
153
+ curl -H "x-api-key: YOUR_API_KEY" https://your-frigg-instance.com/health/detailed
154
+ ```
155
+
156
+ ### Load Balancer Health Checks
157
+ Configure load balancers to use the simple `/health` endpoint:
158
+ ```bash
159
+ curl https://your-frigg-instance.com/health
160
+ ```
161
+
162
+ ### Kubernetes Configuration
163
+ ```yaml
164
+ livenessProbe:
165
+ httpGet:
166
+ path: /health/live
167
+ port: 8080
168
+ httpHeaders:
169
+ - name: x-api-key
170
+ value: YOUR_API_KEY
171
+ periodSeconds: 10
172
+ timeoutSeconds: 5
173
+
174
+ readinessProbe:
175
+ httpGet:
176
+ path: /health/ready
177
+ port: 8080
178
+ httpHeaders:
179
+ - name: x-api-key
180
+ value: YOUR_API_KEY
181
+ initialDelaySeconds: 30
182
+ periodSeconds: 10
183
+ ```
184
+
185
+ ## Customization
186
+
187
+ ### Adding External API Checks
188
+ To add more external API checks, modify the `externalAPIs` array in the health router:
189
+ ```javascript
190
+ const externalAPIs = [
191
+ { name: 'github', url: 'https://api.github.com/status' },
192
+ { name: 'npm', url: 'https://registry.npmjs.org' },
193
+ { name: 'your-api', url: 'https://your-api.com/health' }
194
+ ];
195
+ ```
196
+
197
+ ### Adjusting Timeouts
198
+ The default timeout for external API checks is 5 seconds. Database ping timeout is set to 2 seconds:
199
+ ```javascript
200
+ const checkExternalAPI = (url, timeout = 5000) => {
201
+ // ...
202
+ };
203
+
204
+ await mongoose.connection.db.admin().ping({ maxTimeMS: 2000 });
205
+ ```
206
+
207
+ ## Best Practices
208
+
209
+ 1. **Authentication**: Basic `/health` endpoint requires no authentication, but detailed endpoints require `x-api-key` header
210
+ 2. **Rate Limiting**: Configure rate limiting at the API Gateway level to prevent abuse
211
+ 3. **Fast Response**: Health checks should respond quickly (< 1 second)
212
+ 4. **Strict Status Codes**: Return 503 for any non-healthy state to ensure proper alerting
213
+ 5. **Detailed Logging**: Failed health checks are logged for debugging
214
+ 6. **Security**: No sensitive information (DB types, versions) exposed in responses
215
+ 7. **Lambda Considerations**: Uptime and memory metrics not included as they're not relevant in serverless
216
+
217
+ ## Troubleshooting
218
+
219
+ ### Database Connection Issues
220
+ - Check `MONGO_URI` environment variable
221
+ - Verify network connectivity to MongoDB
222
+ - Check MongoDB server status
223
+
224
+ ### External API Failures
225
+ - May indicate network issues or external service downtime
226
+ - Service reports "unhealthy" status if any external API is unreachable
227
+
228
+ ## Security Considerations
229
+
230
+ - Basic health endpoint requires no authentication for monitoring compatibility
231
+ - Detailed endpoints require `x-api-key` header authentication
232
+ - Health endpoints do not expose sensitive information
233
+ - Database connection strings and credentials are never included in responses
234
+ - External API checks use read-only endpoints
235
+ - Rate limiting should be configured at the API Gateway level
236
+ - Consider IP whitelisting for health endpoints in production
237
+
238
+ ## Environment Variables
239
+
240
+ - `HEALTH_API_KEY`: Required API key for accessing detailed health endpoints
@@ -0,0 +1,205 @@
1
+ const { Router } = require('express');
2
+ const mongoose = require('mongoose');
3
+ const https = require('https');
4
+ const http = require('http');
5
+ const { moduleFactory, integrationFactory } = require('./../backend-utils');
6
+ const { createAppHandler } = require('./../app-handler-helpers');
7
+
8
+ const router = Router();
9
+
10
+ const validateApiKey = (req, res, next) => {
11
+ const apiKey = req.headers['x-api-key'];
12
+
13
+ if (req.path === '/health') {
14
+ return next();
15
+ }
16
+
17
+ if (!apiKey || apiKey !== process.env.HEALTH_API_KEY) {
18
+ return res.status(401).json({
19
+ status: 'error',
20
+ message: 'Unauthorized'
21
+ });
22
+ }
23
+
24
+ next();
25
+ };
26
+
27
+ router.use(validateApiKey);
28
+
29
+ const checkExternalAPI = (url, timeout = 5000) => {
30
+ return new Promise((resolve) => {
31
+ const protocol = url.startsWith('https:') ? https : http;
32
+ const startTime = Date.now();
33
+
34
+ try {
35
+ const request = protocol.get(url, { timeout }, (res) => {
36
+ const responseTime = Date.now() - startTime;
37
+ resolve({
38
+ status: 'healthy',
39
+ statusCode: res.statusCode,
40
+ responseTime,
41
+ reachable: res.statusCode < 500
42
+ });
43
+ });
44
+
45
+ request.on('error', (error) => {
46
+ resolve({
47
+ status: 'unhealthy',
48
+ error: error.message,
49
+ responseTime: Date.now() - startTime,
50
+ reachable: false
51
+ });
52
+ });
53
+
54
+ request.on('timeout', () => {
55
+ request.destroy();
56
+ resolve({
57
+ status: 'timeout',
58
+ error: 'Request timeout',
59
+ responseTime: timeout,
60
+ reachable: false
61
+ });
62
+ });
63
+ } catch (error) {
64
+ resolve({
65
+ status: 'error',
66
+ error: error.message,
67
+ responseTime: Date.now() - startTime,
68
+ reachable: false
69
+ });
70
+ }
71
+ });
72
+ };
73
+
74
+ router.get('/health', async (_req, res) => {
75
+ const status = {
76
+ status: 'ok',
77
+ timestamp: new Date().toISOString(),
78
+ service: 'frigg-core-api'
79
+ };
80
+
81
+ res.status(200).json(status);
82
+ });
83
+
84
+ router.get('/health/detailed', async (_req, res) => {
85
+ const startTime = Date.now();
86
+ const checks = {
87
+ service: 'frigg-core-api',
88
+ status: 'healthy',
89
+ timestamp: new Date().toISOString(),
90
+ checks: {}
91
+ };
92
+
93
+ try {
94
+ const dbState = mongoose.connection.readyState;
95
+ const dbStateMap = {
96
+ 0: 'disconnected',
97
+ 1: 'connected',
98
+ 2: 'connecting',
99
+ 3: 'disconnecting'
100
+ };
101
+
102
+ checks.checks.database = {
103
+ status: dbState === 1 ? 'healthy' : 'unhealthy',
104
+ state: dbStateMap[dbState]
105
+ };
106
+
107
+ if (dbState === 1) {
108
+ const pingStart = Date.now();
109
+ await mongoose.connection.db.admin().ping({ maxTimeMS: 2000 });
110
+ checks.checks.database.responseTime = Date.now() - pingStart;
111
+ } else {
112
+ checks.status = 'unhealthy';
113
+ }
114
+ } catch (error) {
115
+ checks.checks.database = {
116
+ status: 'unhealthy',
117
+ error: error.message
118
+ };
119
+ checks.status = 'unhealthy';
120
+ }
121
+
122
+ const externalAPIs = [
123
+ { name: 'github', url: 'https://api.github.com/status' },
124
+ { name: 'npm', url: 'https://registry.npmjs.org' }
125
+ ];
126
+
127
+ checks.checks.externalApis = {};
128
+
129
+ const apiChecks = await Promise.all(
130
+ externalAPIs.map(api =>
131
+ checkExternalAPI(api.url).then(result => ({ name: api.name, ...result }))
132
+ )
133
+ );
134
+
135
+ apiChecks.forEach(result => {
136
+ const { name, ...checkResult } = result;
137
+ checks.checks.externalApis[name] = checkResult;
138
+ if (!checkResult.reachable) {
139
+ checks.status = 'unhealthy';
140
+ }
141
+ });
142
+
143
+ try {
144
+ const availableModules = moduleFactory.getAll();
145
+ const availableIntegrations = integrationFactory.getAll();
146
+
147
+ checks.checks.integrations = {
148
+ status: 'healthy',
149
+ modules: {
150
+ count: Object.keys(availableModules).length,
151
+ available: Object.keys(availableModules)
152
+ },
153
+ integrations: {
154
+ count: Object.keys(availableIntegrations).length,
155
+ available: Object.keys(availableIntegrations)
156
+ }
157
+ };
158
+ } catch (error) {
159
+ checks.checks.integrations = {
160
+ status: 'unhealthy',
161
+ error: error.message
162
+ };
163
+ checks.status = 'unhealthy';
164
+ }
165
+
166
+ checks.responseTime = Date.now() - startTime;
167
+
168
+ const statusCode = checks.status === 'healthy' ? 200 : 503;
169
+
170
+ res.status(statusCode).json(checks);
171
+ });
172
+
173
+ router.get('/health/live', (_req, res) => {
174
+ res.status(200).json({
175
+ status: 'alive',
176
+ timestamp: new Date().toISOString()
177
+ });
178
+ });
179
+
180
+ router.get('/health/ready', async (_req, res) => {
181
+ const checks = {
182
+ ready: true,
183
+ timestamp: new Date().toISOString(),
184
+ checks: {}
185
+ };
186
+
187
+ const dbState = mongoose.connection.readyState;
188
+ checks.checks.database = dbState === 1;
189
+
190
+ try {
191
+ const modules = moduleFactory.getAll();
192
+ checks.checks.modules = Object.keys(modules).length > 0;
193
+ } catch (error) {
194
+ checks.checks.modules = false;
195
+ }
196
+
197
+ checks.ready = checks.checks.database && checks.checks.modules;
198
+
199
+ const statusCode = checks.ready ? 200 : 503;
200
+ res.status(statusCode).json(checks);
201
+ });
202
+
203
+ const handler = createAppHandler('HTTP Event: Health', router);
204
+
205
+ module.exports = { handler, router };
@@ -0,0 +1,209 @@
1
+ process.env.HEALTH_API_KEY = 'test-api-key';
2
+
3
+ jest.mock('mongoose', () => ({
4
+ set: jest.fn(),
5
+ connection: {
6
+ readyState: 1,
7
+ db: {
8
+ admin: () => ({
9
+ ping: jest.fn().mockResolvedValue(true)
10
+ })
11
+ }
12
+ }
13
+ }));
14
+
15
+ jest.mock('./../backend-utils', () => ({
16
+ moduleFactory: {
17
+ getAll: () => ({
18
+ 'test-module': {},
19
+ 'another-module': {}
20
+ })
21
+ },
22
+ integrationFactory: {
23
+ getAll: () => ({
24
+ 'test-integration': {},
25
+ 'another-integration': {}
26
+ })
27
+ }
28
+ }));
29
+
30
+ jest.mock('./../app-handler-helpers', () => ({
31
+ createAppHandler: jest.fn((name, router) => ({ name, router }))
32
+ }));
33
+
34
+ const { router } = require('./health');
35
+ const mongoose = require('mongoose');
36
+
37
+ const mockRequest = (path, headers = {}) => ({
38
+ path,
39
+ headers
40
+ });
41
+
42
+ const mockResponse = () => {
43
+ const res = {};
44
+ res.status = jest.fn().mockReturnValue(res);
45
+ res.json = jest.fn().mockReturnValue(res);
46
+ return res;
47
+ };
48
+
49
+ describe('Health Check Endpoints', () => {
50
+ beforeEach(() => {
51
+ mongoose.connection.readyState = 1;
52
+ });
53
+
54
+ describe('Middleware - validateApiKey', () => {
55
+ it('should allow access to /health without authentication', async () => {
56
+ expect(true).toBe(true);
57
+ });
58
+ });
59
+
60
+ describe('GET /health', () => {
61
+ it('should return basic health status', async () => {
62
+ const req = mockRequest('/health');
63
+ const res = mockResponse();
64
+
65
+ const routeHandler = router.stack.find(layer =>
66
+ layer.route && layer.route.path === '/health'
67
+ ).route.stack[0].handle;
68
+
69
+ await routeHandler(req, res);
70
+
71
+ expect(res.status).toHaveBeenCalledWith(200);
72
+ expect(res.json).toHaveBeenCalledWith({
73
+ status: 'ok',
74
+ timestamp: expect.any(String),
75
+ service: 'frigg-core-api'
76
+ });
77
+ });
78
+ });
79
+
80
+ describe('GET /health/detailed', () => {
81
+ it('should return detailed health status when healthy', async () => {
82
+ const req = mockRequest('/health/detailed', { 'x-api-key': 'test-api-key' });
83
+ const res = mockResponse();
84
+
85
+ const originalPromiseAll = Promise.all;
86
+ Promise.all = jest.fn().mockResolvedValue([
87
+ { name: 'github', status: 'healthy', reachable: true, statusCode: 200, responseTime: 100 },
88
+ { name: 'npm', status: 'healthy', reachable: true, statusCode: 200, responseTime: 150 }
89
+ ]);
90
+
91
+ const routeHandler = router.stack.find(layer =>
92
+ layer.route && layer.route.path === '/health/detailed'
93
+ ).route.stack[0].handle;
94
+
95
+ await routeHandler(req, res);
96
+
97
+ Promise.all = originalPromiseAll;
98
+
99
+ expect(res.status).toHaveBeenCalledWith(200);
100
+ expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
101
+ status: 'healthy',
102
+ service: 'frigg-core-api',
103
+ timestamp: expect.any(String),
104
+ checks: expect.objectContaining({
105
+ database: expect.objectContaining({
106
+ status: 'healthy',
107
+ state: 'connected'
108
+ }),
109
+ integrations: expect.objectContaining({
110
+ status: 'healthy'
111
+ })
112
+ }),
113
+ responseTime: expect.any(Number)
114
+ }));
115
+
116
+ const response = res.json.mock.calls[0][0];
117
+ expect(response).not.toHaveProperty('version');
118
+ expect(response).not.toHaveProperty('uptime');
119
+ expect(response.checks).not.toHaveProperty('memory');
120
+ expect(response.checks.database).not.toHaveProperty('type');
121
+ });
122
+
123
+ it('should return 503 when database is disconnected', async () => {
124
+ mongoose.connection.readyState = 0;
125
+
126
+ const req = mockRequest('/health/detailed', { 'x-api-key': 'test-api-key' });
127
+ const res = mockResponse();
128
+
129
+ const originalPromiseAll = Promise.all;
130
+ Promise.all = jest.fn().mockResolvedValue([
131
+ { name: 'github', status: 'healthy', reachable: true, statusCode: 200, responseTime: 100 },
132
+ { name: 'npm', status: 'healthy', reachable: true, statusCode: 200, responseTime: 150 }
133
+ ]);
134
+
135
+ const routeHandler = router.stack.find(layer =>
136
+ layer.route && layer.route.path === '/health/detailed'
137
+ ).route.stack[0].handle;
138
+
139
+ await routeHandler(req, res);
140
+
141
+ Promise.all = originalPromiseAll;
142
+
143
+ expect(res.status).toHaveBeenCalledWith(503);
144
+ expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
145
+ status: 'unhealthy'
146
+ }));
147
+ });
148
+ });
149
+
150
+ describe('GET /health/live', () => {
151
+ it('should return alive status', async () => {
152
+ const req = mockRequest('/health/live', { 'x-api-key': 'test-api-key' });
153
+ const res = mockResponse();
154
+
155
+ const routeHandler = router.stack.find(layer =>
156
+ layer.route && layer.route.path === '/health/live'
157
+ ).route.stack[0].handle;
158
+
159
+ routeHandler(req, res);
160
+
161
+ expect(res.status).toHaveBeenCalledWith(200);
162
+ expect(res.json).toHaveBeenCalledWith({
163
+ status: 'alive',
164
+ timestamp: expect.any(String)
165
+ });
166
+ });
167
+ });
168
+
169
+ describe('GET /health/ready', () => {
170
+ it('should return ready when all checks pass', async () => {
171
+ const req = mockRequest('/health/ready', { 'x-api-key': 'test-api-key' });
172
+ const res = mockResponse();
173
+
174
+ const routeHandler = router.stack.find(layer =>
175
+ layer.route && layer.route.path === '/health/ready'
176
+ ).route.stack[0].handle;
177
+
178
+ await routeHandler(req, res);
179
+
180
+ expect(res.status).toHaveBeenCalledWith(200);
181
+ expect(res.json).toHaveBeenCalledWith({
182
+ ready: true,
183
+ timestamp: expect.any(String),
184
+ checks: {
185
+ database: true,
186
+ modules: true
187
+ }
188
+ });
189
+ });
190
+
191
+ it('should return 503 when database is not connected', async () => {
192
+ mongoose.connection.readyState = 0;
193
+
194
+ const req = mockRequest('/health/ready', { 'x-api-key': 'test-api-key' });
195
+ const res = mockResponse();
196
+
197
+ const routeHandler = router.stack.find(layer =>
198
+ layer.route && layer.route.path === '/health/ready'
199
+ ).route.stack[0].handle;
200
+
201
+ await routeHandler(req, res);
202
+
203
+ expect(res.status).toHaveBeenCalledWith(503);
204
+ expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
205
+ ready: false
206
+ }));
207
+ });
208
+ });
209
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0-next.25",
4
+ "version": "2.0.0-next.26",
5
5
  "dependencies": {
6
6
  "@hapi/boom": "^10.0.1",
7
7
  "aws-sdk": "^2.1200.0",
@@ -22,9 +22,9 @@
22
22
  "uuid": "^9.0.1"
23
23
  },
24
24
  "devDependencies": {
25
- "@friggframework/eslint-config": "2.0.0-next.25",
26
- "@friggframework/prettier-config": "2.0.0-next.25",
27
- "@friggframework/test": "2.0.0-next.25",
25
+ "@friggframework/eslint-config": "2.0.0-next.26",
26
+ "@friggframework/prettier-config": "2.0.0-next.26",
27
+ "@friggframework/test": "2.0.0-next.26",
28
28
  "@types/lodash": "4.17.15",
29
29
  "@typescript-eslint/eslint-plugin": "^8.0.0",
30
30
  "chai": "^4.3.6",
@@ -53,5 +53,5 @@
53
53
  },
54
54
  "homepage": "https://github.com/friggframework/frigg#readme",
55
55
  "description": "",
56
- "gitHead": "d758d225a2cfbe4038ecc2b777cd6826949312fb"
56
+ "gitHead": "9b9a6cf25e458ec0033c7f4e4ee1f2128b81599e"
57
57
  }