1
+ import { recordException } from '@hyperdx/node-opentelemetry' ;
1
2
import compression from 'compression' ;
2
3
import type { NextFunction , Request , Response } from 'express' ;
3
4
import express from 'express' ;
@@ -10,6 +11,10 @@ import routers from './routers/aggregator';
10
11
import { BaseError , StatusCode } from './utils/errors' ;
11
12
import logger , { expressLogger } from './utils/logger' ;
12
13
14
+ if ( ! config . AGGREGATOR_PAYLOAD_SIZE_LIMIT ) {
15
+ throw new Error ( 'AGGREGATOR_PAYLOAD_SIZE_LIMIT is not defined' ) ;
16
+ }
17
+
13
18
const app : express . Application = express ( ) ;
14
19
15
20
const healthCheckMiddleware = async (
@@ -51,13 +56,35 @@ app.use('/', healthCheckMiddleware, routers.rootRouter);
51
56
// ---------------------------------------------------------
52
57
53
58
// error handling
54
- app . use ( ( err : BaseError , _ : Request , res : Response , next : NextFunction ) => {
55
- logger . error ( {
56
- location : 'appErrorHandler' ,
57
- error : serializeError ( err ) ,
58
- } ) ;
59
- // WARNING: should always return 500 so the ingestor will queue logs
60
- res . status ( StatusCode . INTERNAL_SERVER ) . send ( 'Something broke!' ) ;
61
- } ) ;
59
+ app . use (
60
+ (
61
+ err : BaseError & { status ?: number } ,
62
+ _ : Request ,
63
+ res : Response ,
64
+ next : NextFunction ,
65
+ ) => {
66
+ void recordException ( err , {
67
+ mechanism : {
68
+ type : 'generic' ,
69
+ handled : false ,
70
+ } ,
71
+ } ) ;
72
+
73
+ // TODO: REPLACED WITH recordException once we enable tracing SDK
74
+ logger . error ( {
75
+ location : 'appErrorHandler' ,
76
+ error : serializeError ( err ) ,
77
+ } ) ;
78
+
79
+ // TODO: for all non 5xx errors
80
+ if ( err . status === StatusCode . CONTENT_TOO_LARGE ) {
81
+ // WARNING: return origin status code so the ingestor will tune up the adaptive concurrency properly
82
+ return res . sendStatus ( err . status ) ;
83
+ }
84
+
85
+ // WARNING: should always return 500 so the ingestor will queue logs
86
+ res . status ( StatusCode . INTERNAL_SERVER ) . send ( 'Something broke!' ) ;
87
+ } ,
88
+ ) ;
62
89
63
90
export default app ;
0 commit comments