forked from Talenttrust/Talenttrust-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics-service.ts
More file actions
705 lines (592 loc) · 22.8 KB
/
Copy pathmetrics-service.ts
File metadata and controls
705 lines (592 loc) · 22.8 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import { NextFunction, Request, Response } from 'express';
import {
collectDefaultMetrics,
Counter,
Gauge,
Histogram,
Registry,
} from 'prom-client';
import { logger } from '../logger';
import { ServiceStatus } from './types';
import {
assertDlqDepth,
assertDisputesErrorCause,
assertServiceStatus,
assertWebhookOutcome,
DisputesErrorCause,
WebhookOutcome as ValidatedWebhookOutcome,
} from './metrics-validation';
import { DEFAULT_HISTOGRAM_BUCKETS, validateHistogramBuckets } from './observability-config';
import { Logger, logger as rootLogger } from '../logger';
/**
* Re-exported from metrics-validation to preserve existing import paths.
* The canonical definition lives in metrics-validation.ts where all metric
* input types are colocated.
*/
export type WebhookOutcome = ValidatedWebhookOutcome;
/**
* Canonical list of metric family names documented in docs/observability.md.
* This constant enables round-trip verification: tests assert that the set of
* metrics registered by MetricsService matches this list exactly.
*
* Note: auth_cache_hits_total and auth_cache_misses_total are registered by
* AuthCache (not MetricsService) and are documented separately.
*/
export const CATALOG_METRIC_NAMES: readonly string[] = [
'http_requests_total',
'http_request_duration_seconds',
'api_keys_requests_total',
'api_keys_request_duration_seconds',
'api_keys_errors_total',
'auth_requests_total',
'auth_request_duration_seconds',
'auth_errors_total',
'service_health_status',
'webhook_deliveries_total',
'webhook_dlq_depth',
'webhook_rate_limit_tokens',
'webhook_rate_limit_queue_depth',
'milestone_operations_total',
'milestone_operation_duration_seconds',
] as const;
/** The type of milestone operation being instrumented. */
export type MilestoneOperation = 'create' | 'update' | 'read';
/**
* The outcome category of a milestone operation.
*
* - success: the operation completed with a 2xx response
* - client_error: the operation was rejected due to bad input (4xx)
* - server_error: an unexpected error occurred (5xx)
*/
export type MilestoneOperationStatus = 'success' | 'client_error' | 'server_error';
export interface MetricsServiceLike {
contentType: string;
trackHttpRequest: (req: Request, res: Response, next: NextFunction) => void;
trackApiKeysRequest: (req: Request, res: Response, next: NextFunction) => void;
trackAuthRequest: (req: Request, res: Response, next: NextFunction) => void;
getMetrics: () => Promise<string>;
recordReputationRequest: (metric: ReputationRequestMetric) => void;
recordHealthStatus: (status: ServiceStatus) => void;
recordWebhookDelivery: (outcome: WebhookOutcome) => void;
setWebhookDlqDepth: (depth: number) => void;
recordDisputesRequest: (input: DisputesRequestMetricInput) => void;
startRateLimitMetricsSampling?: (limiter: any, intervalMs?: number) => void;
stopRateLimitMetricsSampling?: () => void;
recordMilestoneOperation: (
operation: MilestoneOperation,
status: MilestoneOperationStatus,
durationSeconds: number,
errorCause?: string,
) => void;
}
const HEALTH_STATUS_VALUE: Record<ServiceStatus, number> = {
up: 2,
degraded: 1,
down: 0,
};
const DEFAULT_HTTP_ROUTE_LABEL_LIMIT = 100;
const OTHER_ROUTE_LABEL = 'other';
const UNMATCHED_ROUTE_LABEL = 'unmatched';
export interface MetricsServiceOptions {
httpRouteLabelLimit?: number;
/**
* Custom histogram bucket boundaries (in seconds) for
* `http_request_duration_seconds`. Must be a non-empty array of strictly
* increasing positive numbers. Falls back to {@link DEFAULT_HISTOGRAM_BUCKETS}
* when absent or invalid.
*/
histogramBuckets?: number[];
}
/**
* Manages Prometheus metrics registration and request instrumentation.
*/
export class MetricsService implements MetricsServiceLike {
readonly contentType: string;
private readonly register: Registry;
private readonly httpRequestsTotal: Counter;
private readonly httpRequestDurationSeconds: Histogram;
private readonly apiKeysRequestsTotal: Counter;
private readonly apiKeysRequestDurationSeconds: Histogram;
private readonly apiKeysErrorsTotal: Counter;
private readonly authRequestsTotal: Counter;
private readonly authRequestDurationSeconds: Histogram;
private readonly authErrorsTotal: Counter;
private readonly serviceHealthStatus: Gauge;
private readonly webhookDeliveriesTotal: Counter;
private readonly webhookDlqDepth: Gauge;
private readonly webhookRateLimitTokens: Gauge;
private readonly webhookRateLimitQueueDepth: Gauge;
private readonly milestoneOperationsTotal: Counter;
private readonly milestoneOperationDurationSeconds: Histogram;
private readonly httpRouteLabelLimit: number;
private readonly observedHttpRouteLabels = new Set<string>();
private rateLimitStopSampling: (() => void) | null = null;
constructor(
private readonly serviceName: string,
register?: Registry,
options: MetricsServiceOptions = {},
) {
this.register = register ?? new Registry();
this.httpRouteLabelLimit = options.httpRouteLabelLimit ?? DEFAULT_HTTP_ROUTE_LABEL_LIMIT;
// Resolve histogram buckets: validate caller-supplied values and fall back
// to defaults when absent or invalid, so misconfiguration is non-fatal.
const resolvedBuckets = resolveHistogramBuckets(options.histogramBuckets);
collectDefaultMetrics({
register: this.register,
prefix: `${sanitizeMetricPrefix(serviceName)}_`,
});
this.httpRequestsTotal = new Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests.',
labelNames: ['method', 'route', 'status_code', 'error_cause'],
registers: [this.register],
});
this.httpRequestDurationSeconds = new Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds.',
labelNames: ['method', 'route', 'status_code', 'error_cause'],
buckets: resolvedBuckets,
registers: [this.register],
});
this.apiKeysRequestsTotal = new Counter({
name: 'api_keys_requests_total',
help: 'Total number of API key management requests.',
labelNames: ['operation', 'status_code'],
registers: [this.register],
});
this.apiKeysRequestDurationSeconds = new Histogram({
name: 'api_keys_request_duration_seconds',
help: 'Duration of API key management requests in seconds.',
labelNames: ['operation', 'status_code'],
buckets: resolvedBuckets,
registers: [this.register],
});
this.apiKeysErrorsTotal = new Counter({
name: 'api_keys_errors_total',
help: 'Total number of API key management request errors by cause.',
labelNames: ['operation', 'cause'],
registers: [this.register],
});
this.authRequestsTotal = new Counter({
name: 'auth_requests_total',
help: 'Total number of authentication requests.',
labelNames: ['operation', 'status_code'],
registers: [this.register],
});
this.authRequestDurationSeconds = new Histogram({
name: 'auth_request_duration_seconds',
help: 'Duration of authentication requests in seconds.',
labelNames: ['operation', 'status_code'],
buckets: resolvedBuckets,
registers: [this.register],
});
this.authErrorsTotal = new Counter({
name: 'auth_errors_total',
help: 'Total number of authentication request errors by cause.',
labelNames: ['operation', 'cause'],
registers: [this.register],
});
this.serviceHealthStatus = new Gauge({
name: 'service_health_status',
help: 'Current service health status. up=2, degraded=1, down=0.',
labelNames: ['service'],
registers: [this.register],
});
this.serviceHealthStatus.set({ service: this.serviceName }, HEALTH_STATUS_VALUE.up);
this.contentType = this.register.contentType;
this.webhookDeliveriesTotal = new Counter({
name: 'webhook_deliveries_total',
help: 'Total webhook delivery attempts by outcome.',
labelNames: ['outcome'],
registers: [this.register],
});
this.webhookDlqDepth = new Gauge({
name: 'webhook_dlq_depth',
help: 'Current number of entries in the webhook dead-letter queue.',
registers: [this.register],
});
this.webhookRateLimitTokens = new Gauge({
name: 'webhook_rate_limit_tokens',
help: 'Current token count per provider in the rate-limiter bucket.',
labelNames: ['provider_id'],
registers: [this.register],
});
this.webhookRateLimitQueueDepth = new Gauge({
name: 'webhook_rate_limit_queue_depth',
help: 'Current queue depth (number of waiting deliveries) per provider in the rate-limiter.',
labelNames: ['provider_id'],
registers: [this.register],
});
this.milestoneOperationsTotal = new Counter({
name: 'milestone_operations_total',
help: 'Total number of milestone operations by type, status, and error cause.',
labelNames: ['operation', 'status', 'error_cause'],
registers: [this.register],
});
this.milestoneOperationDurationSeconds = new Histogram({
name: 'milestone_operation_duration_seconds',
help: 'Duration of milestone operations in seconds, labelled by operation type and status.',
labelNames: ['operation', 'status'],
buckets: resolvedBuckets,
registers: [this.register],
});
}
trackHttpRequest(req: Request, res: Response, next: NextFunction): void {
const start = process.hrtime.bigint();
res.on('finish', () => {
const durationNs = process.hrtime.bigint() - start;
const duration = Number(durationNs) / 1_000_000_000;
const durationMs = Number(durationNs) / 1_000_000;
const route = this.boundRouteLabel(extractRoute(req));
const errorCause = resolveErrorCause(res);
const labels = {
method: req.method,
route,
status_code: String(res.statusCode),
error_cause: errorCause,
};
this.httpRequestsTotal.inc(labels);
this.httpRequestDurationSeconds.observe(labels, duration);
const locals = res.locals ?? {};
const requestId = typeof locals.requestId === 'string' ? locals.requestId : undefined;
const correlationId =
typeof locals.correlationId === 'string' ? locals.correlationId : undefined;
logger.info('http request metric', {
metric: 'http_request',
method: req.method,
route,
statusCode: res.statusCode,
errorCause,
durationMs: parseFloat(durationMs.toFixed(3)),
...(requestId !== undefined && { requestId }),
...(correlationId !== undefined && { correlationId }),
});
});
next();
}
trackApiKeysRequest(req: Request, res: Response, next: NextFunction): void {
const start = process.hrtime.bigint();
res.on('finish', () => {
const durationSeconds = Number(process.hrtime.bigint() - start) / 1_000_000_000;
const statusCode = res.statusCode;
const operation = apiKeysOperation(req.method, req.route?.path);
const labels = { operation, status_code: String(statusCode) };
const errorCause = apiKeysErrorCause(statusCode);
this.apiKeysRequestsTotal.inc(labels);
this.apiKeysRequestDurationSeconds.observe(labels, durationSeconds);
if (errorCause !== null) {
this.apiKeysErrorsTotal.inc({ operation, cause: errorCause });
}
const log = (res.locals['log'] as Logger | undefined) ?? rootLogger;
const logFields = {
method: req.method,
route: apiKeysRouteTemplate(req.route?.path),
operation,
statusCode,
durationMs: Number((durationSeconds * 1000).toFixed(3)),
outcome: statusCode < 400 ? 'success' : 'error',
...(errorCause !== null && { errorCause }),
};
if (statusCode >= 500) {
log.error('api_keys_request', logFields);
} else if (statusCode >= 400) {
log.warn('api_keys_request', logFields);
} else {
log.info('api_keys_request', logFields);
}
});
next();
}
trackAuthRequest(req: Request, res: Response, next: NextFunction): void {
const start = process.hrtime.bigint();
res.on('finish', () => {
const durationSeconds = Number(process.hrtime.bigint() - start) / 1_000_000_000;
const statusCode = res.statusCode;
const operation = authOperation(req.method, req.route?.path);
const labels = { operation, status_code: String(statusCode) };
const errorCause = authErrorCause(statusCode, res);
this.authRequestsTotal.inc(labels);
this.authRequestDurationSeconds.observe(labels, durationSeconds);
if (errorCause !== null) {
this.authErrorsTotal.inc({ operation, cause: errorCause });
}
const log = (res.locals['log'] as Logger | undefined) ?? rootLogger;
const logFields = {
method: req.method,
route: authRouteTemplate(req.route?.path),
operation,
statusCode,
durationMs: Number((durationSeconds * 1000).toFixed(3)),
outcome: statusCode < 400 ? 'success' : 'error',
...(errorCause !== null && { errorCause }),
};
if (statusCode >= 500) {
log.error('auth_request', logFields);
} else if (statusCode >= 400) {
log.warn('auth_request', logFields);
} else {
log.info('auth_request', logFields);
}
});
next();
}
recordHealthStatus(status: ServiceStatus): void {
// Runtime guard: reject unknown status strings that bypass TypeScript types
// (e.g. from JSON-deserialized or cross-process call sites).
const validated = assertServiceStatus(status);
this.serviceHealthStatus.set(
{ service: this.serviceName },
HEALTH_STATUS_VALUE[validated],
);
}
recordWebhookDelivery(outcome: WebhookOutcome): void {
// Runtime guard: reject unknown outcome strings.
const validated = assertWebhookOutcome(outcome);
this.webhookDeliveriesTotal.inc({ outcome: validated });
}
setWebhookDlqDepth(depth: number): void {
// Runtime guard: reject NaN, ±Infinity, negative values, and unreasonably
// large values that would indicate a bug or injection attempt.
const validated = assertDlqDepth(depth);
this.webhookDlqDepth.set(validated);
}
/**
* Record a disputes API request observation (counter + duration histogram).
* Labels are bounded: route must be an Express template, error_cause a finite enum.
*/
recordDisputesRequest(input: DisputesRequestMetricInput): void {
const errorCause = assertDisputesErrorCause(input.errorCause);
const duration =
Number.isFinite(input.durationSeconds) && input.durationSeconds >= 0
? input.durationSeconds
: 0;
const labels = {
method: input.method,
route: this.boundRouteLabel(input.route || UNMATCHED_ROUTE_LABEL),
status_code: String(input.statusCode),
error_cause: errorCause,
};
this.disputesRequestsTotal.inc(labels);
this.disputesRequestDurationSeconds.observe(labels, duration);
}
startRateLimitMetricsSampling(limiter: any, intervalMs: number = 10000): void {
if (this.rateLimitStopSampling !== null) {
console.warn('[MetricsService] Rate limit metrics sampling already active.');
return;
}
this.rateLimitStopSampling = limiter.startMetricsSampling(
this.webhookRateLimitTokens,
this.webhookRateLimitQueueDepth,
intervalMs,
);
}
stopRateLimitMetricsSampling(): void {
if (this.rateLimitStopSampling !== null) {
this.rateLimitStopSampling();
this.rateLimitStopSampling = null;
}
}
/**
* Record a completed milestone operation.
*
* @param operation - The type of operation: create, update, or read.
* @param status - Outcome category: success, client_error, or server_error.
* @param durationSeconds - Wall-clock time in seconds.
* @param errorCause - Optional machine-readable cause label (e.g. "not_found",
* "contract_bounds_error") for failed operations. Defaults to the empty
* string for success outcomes. Never include PII.
*/
recordMilestoneOperation(
operation: MilestoneOperation,
status: MilestoneOperationStatus,
durationSeconds: number,
errorCause: string = '',
): void {
this.milestoneOperationsTotal.inc({ operation, status, error_cause: errorCause });
this.milestoneOperationDurationSeconds.observe({ operation, status }, durationSeconds);
}
getMetrics(): Promise<string> {
return this.register.metrics();
}
recordReputationRequest(metric: ReputationRequestMetric): void {
assertReputationRequestMetric(metric);
const labels = {
operation: metric.operation,
status: metric.status,
status_code: String(metric.statusCode),
error_cause: metric.errorCause,
};
this.reputationRequestsTotal.inc(labels);
this.reputationRequestDurationSeconds.observe(labels, metric.durationSeconds);
if (metric.errorCause !== 'none') {
this.reputationErrorsTotal.inc({
operation: metric.operation,
error_cause: metric.errorCause,
});
}
}
private boundRouteLabel(route: string): string {
// Never collapse unmatched routes — they are not user-controlled and must
// always be tracked separately so operators can monitor 404 rates.
if (route === UNMATCHED_ROUTE_LABEL) {
return route;
}
if (this.observedHttpRouteLabels.has(route)) {
return route;
}
if (this.observedHttpRouteLabels.size < this.httpRouteLabelLimit) {
this.observedHttpRouteLabels.add(route);
return route;
}
return OTHER_ROUTE_LABEL;
}
}
type ApiKeysErrorCause =
| 'validation_error'
| 'authentication_error'
| 'authorization_error'
| 'not_found'
| 'client_error'
| 'server_error';
function apiKeysErrorCause(statusCode: number): ApiKeysErrorCause | null {
if (statusCode < 400) return null;
if (statusCode === 400 || statusCode === 422) return 'validation_error';
if (statusCode === 401) return 'authentication_error';
if (statusCode === 403) return 'authorization_error';
if (statusCode === 404) return 'not_found';
if (statusCode < 500) return 'client_error';
return 'server_error';
}
function apiKeysOperation(method: string, routePath: unknown): string {
const route = typeof routePath === 'string' ? routePath : '';
if (method === 'POST' && route === '/api-keys') return 'create';
if (method === 'GET' && route === '/api-keys') return 'list';
if (method === 'GET' && route === '/api-keys/:id') return 'get';
if (method === 'POST' && route === '/api-keys/:id/rotate') return 'rotate';
if (method === 'DELETE' && route === '/api-keys/:id') return 'deactivate';
return 'unknown';
}
function apiKeysRouteTemplate(routePath: unknown): string {
return typeof routePath === 'string' ? `/api/v1${routePath}` : '/api/v1/api-keys';
}
type AuthErrorCause =
| 'validation_error'
| 'invalid_credentials'
| 'invalid_token'
| 'conflict'
| 'rate_limit'
| 'client_error'
| 'server_error';
function authErrorCause(statusCode: number, res: Response): AuthErrorCause | null {
if (statusCode < 400) return null;
const explicit = res.locals?.['errorCause'];
if (explicit === 'validation_error') return 'validation_error';
if (explicit === 'invalid_credentials') return 'invalid_credentials';
if (explicit === 'invalid_refresh_token' || explicit === 'unauthorized') return 'invalid_token';
if (explicit === 'conflict') return 'conflict';
if (statusCode === 400 || statusCode === 422) return 'validation_error';
if (statusCode === 401 || statusCode === 403) return 'invalid_token';
if (statusCode === 409) return 'conflict';
if (statusCode === 429) return 'rate_limit';
if (statusCode < 500) return 'client_error';
return 'server_error';
}
function authOperation(method: string, routePath: unknown): string {
const route = typeof routePath === 'string' ? routePath : '';
if (method !== 'POST') return 'unknown';
if (route === '/login') return 'login';
if (route === '/register') return 'register';
if (route === '/refresh') return 'refresh';
if (route === '/logout') return 'logout';
return 'unknown';
}
function authRouteTemplate(routePath: unknown): string {
const route = typeof routePath === 'string' ? routePath : '';
return ['/login', '/register', '/refresh', '/logout'].includes(route)
? `/api/v1/auth${route}`
: '/api/v1/auth';
}
/**
* Validate the caller-supplied bucket array and return it if valid.
* Falls back to {@link DEFAULT_HISTOGRAM_BUCKETS} when the input is absent or
* fails validation, ensuring that misconfiguration is non-fatal and existing
* dashboards keep working.
*/
function resolveHistogramBuckets(buckets: number[] | undefined): number[] {
if (buckets === undefined) {
return [...DEFAULT_HISTOGRAM_BUCKETS];
}
const result = validateHistogramBuckets(buckets);
if (!result.valid) {
console.warn(
`[MetricsService] Invalid histogramBuckets option (${(result as any).reason}); falling back to defaults.`,
);
return [...DEFAULT_HISTOGRAM_BUCKETS];
}
return result.buckets;
}
function sanitizeMetricPrefix(input: string): string {
const sanitized = input.replace(/[^a-zA-Z0-9_:]/g, '_');
return sanitized.length > 0 ? sanitized : 'service';
}
/**
* Returns a bounded, non-user-controlled route label for HTTP metrics.
*
* Express exposes the matched route template at `req.route.path`; joining it
* with the static mount point in `req.baseUrl` preserves useful labels such as
* `/api/v1/contracts/:id` without using concrete request paths that may contain
* attacker-controlled identifiers. Requests that never match a route collapse
* into one shared bucket.
*/
function extractRoute(req: Request): string {
const routePath = formatExpressPath(req.route?.path);
if (routePath === null) {
return UNMATCHED_ROUTE_LABEL;
}
const baseUrl = normalizeRoutePart(req.baseUrl);
const route = joinRouteParts(baseUrl, routePath);
return route.length > 0 ? route : '/';
}
function formatExpressPath(path: unknown): string | null {
if (typeof path === 'string') {
return normalizeRoutePart(path);
}
if (path instanceof RegExp) {
return path.toString();
}
if (Array.isArray(path)) {
const parts = path.map(formatExpressPath).filter((part): part is string => part !== null);
return parts.length > 0 ? parts.join('|') : null;
}
return null;
}
function normalizeRoutePart(part: string | undefined): string {
if (!part || part === '/') {
return '';
}
return part.startsWith('/') ? part : `/${part}`;
}
function joinRouteParts(baseUrl: string, routePath: string): string {
if (!baseUrl) {
return routePath;
}
if (!routePath) {
return baseUrl;
}
return `${baseUrl}${routePath}`;
}
function resolveErrorCause(res: Response): string {
const locals = res.locals ?? {};
const explicit = locals['errorCause'];
if (typeof explicit === 'string' && explicit.length > 0) {
return explicit;
}
const code = res.statusCode;
if (code < 400) {
return 'none';
}
if (code < 500) {
return 'client_error';
}
return 'server_error';
}