|
|
import config from '@automattic/calypso-config'; |
|
|
import { throttle } from 'lodash'; |
|
|
import { logServerEvent } from 'calypso/lib/analytics/statsd-utils'; |
|
|
|
|
|
|
|
|
const THROTTLE_MILLIS = 1000 / config( 'statsd_analytics_response_time_max_logs_per_second' ); |
|
|
|
|
|
const logAnalyticsThrottled = throttle( ( { sectionName, duration, loggedIn, usedSSRHandler } ) => { |
|
|
const events = [ |
|
|
|
|
|
{ |
|
|
name: 'response_time', |
|
|
value: duration, |
|
|
type: 'timing', |
|
|
isLegacy: true, |
|
|
}, |
|
|
|
|
|
{ |
|
|
name: `response_time.${ |
|
|
loggedIn ? 'logged-in' : 'logged-out' |
|
|
}.ssr_pipeline_${ usedSSRHandler }`, |
|
|
value: duration, |
|
|
type: 'timing', |
|
|
}, |
|
|
]; |
|
|
logServerEvent( sectionName, events ); |
|
|
}, THROTTLE_MILLIS ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function logSectionResponse( req, res, next ) { |
|
|
const startRenderTime = Date.now(); |
|
|
|
|
|
res.on( 'close', function () { |
|
|
if ( ! req.context?.sectionName ) { |
|
|
return; |
|
|
} |
|
|
const { user, sectionName, usedSSRHandler } = req.context; |
|
|
|
|
|
logAnalyticsThrottled( { |
|
|
loggedIn: !! user, |
|
|
usedSSRHandler: !! usedSSRHandler, |
|
|
duration: Date.now() - startRenderTime, |
|
|
sectionName, |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
next(); |
|
|
} |
|
|
|