1
1
import type { RequestHandler , Request , Response , ErrorRequestHandler } from 'express' ;
2
+ import { Histogram } from '@opentelemetry/api' ;
2
3
3
4
import { ServiceError } from '../error' ;
4
5
import type { AnyServiceLocals , RequestWithApp , ServiceExpress , ServiceLocals } from '../types' ;
@@ -44,6 +45,7 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
44
45
error : Error | undefined ,
45
46
req : Request ,
46
47
res : Response ,
48
+ histogram : Histogram ,
47
49
) {
48
50
const prefs = ( res . locals as WithLogPrefs ) [ LOG_PREFS ] ;
49
51
if ( prefs . logged ) {
@@ -61,6 +63,14 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
61
63
dur,
62
64
} ;
63
65
66
+ const path = req . route ? req . route . path : null ;
67
+ histogram . record ( dur , {
68
+ status_code : endLog . s ,
69
+ method : endLog . m ,
70
+ path,
71
+ service : app . locals . name ,
72
+ } ) ;
73
+
64
74
if ( res . locals . user ?. id ) {
65
75
endLog . u = res . locals . user . id ;
66
76
}
@@ -95,7 +105,12 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
95
105
96
106
export function loggerMiddleware <
97
107
SLocals extends AnyServiceLocals = ServiceLocals < ConfigurationSchema > ,
98
- > ( app : ServiceExpress < SLocals > , logRequests ?: boolean , logResponses ?: boolean ) : RequestHandler {
108
+ > (
109
+ app : ServiceExpress < SLocals > ,
110
+ histogram : Histogram ,
111
+ logRequests ?: boolean ,
112
+ logResponses ?: boolean ,
113
+ ) : RequestHandler {
99
114
const { logger, service } = app . locals ;
100
115
return function gblogger ( req , res , next ) {
101
116
const prefs : LogPrefs = {
@@ -135,15 +150,15 @@ export function loggerMiddleware<
135
150
service . getLogFields ?.( req as RequestWithApp < SLocals > , preLog ) ;
136
151
logger . info ( preLog , 'pre' ) ;
137
152
138
- const logWriter = ( ) => finishLog ( app , undefined , req , res ) ;
153
+ const logWriter = ( ) => finishLog ( app , undefined , req , res , histogram ) ;
139
154
res . on ( 'finish' , logWriter ) ;
140
155
next ( ) ;
141
156
} ;
142
157
}
143
158
144
159
export function errorHandlerMiddleware <
145
160
SLocals extends AnyServiceLocals = ServiceLocals < ConfigurationSchema > ,
146
- > ( app : ServiceExpress < SLocals > , unnest ?: boolean , returnError ?: boolean ) {
161
+ > ( app : ServiceExpress < SLocals > , histogram : Histogram , unnest ?: boolean , returnError ?: boolean ) {
147
162
const gbErrorHandler : ErrorRequestHandler = ( error , req , res , next ) => {
148
163
let loggable : Partial < ServiceError > = error ;
149
164
const body = error . response ?. body || error . body ;
@@ -159,7 +174,7 @@ export function errorHandlerMiddleware<
159
174
// Set the status to error, even if we aren't going to render the error.
160
175
res . status ( loggable . status || 500 ) ;
161
176
if ( returnError ) {
162
- finishLog ( app , error , req , res ) ;
177
+ finishLog ( app , error , req , res , histogram ) ;
163
178
const prefs = ( res . locals as WithLogPrefs ) [ LOG_PREFS ] ;
164
179
prefs . logged = true ;
165
180
res . json ( {
0 commit comments