'use strict'; const assert = require('assert'); const urlJoin = require('url-join'); const asyncHandler = require('../lib/express-async-handler'); const { getSerializableSpans, getActiveTraceId } = require('../tracing/tracing-middleware'); const sanitizeHtml = require('../lib/sanitize-html'); const safeJson = require('../lib/safe-json'); const config = require('../lib/config'); const basePath = config.get('basePath'); assert(basePath); const requestTimeoutMs = config.get('requestTimeoutMs'); assert(requestTimeoutMs); // Based off of the `connect-timeout` middleware, // https://github.com/expressjs/timeout/blob/f2f520f335f2f2ae255d4778e908e8d38e3a4e68/index.js async function timeoutMiddleware(req, res, next) { const timeoutId = setTimeout(() => { const traceId = getActiveTraceId(); const serializableSpans = getSerializableSpans(); const serializedSpans = JSON.stringify(serializableSpans); let humanReadableSpans; if (serializableSpans.length > 0) { humanReadableSpans = serializableSpans.map((serializableSpan) => { const method = serializableSpan.attributes['http.method']; const url = serializableSpan.attributes['http.url']; const statusCode = serializableSpan.attributes['http.status_code']; let durationString = `request is still running (${ Date.now() - serializableSpan.startTimeInMs }ms so far)`; if (serializableSpan.durationInMs) { durationString = `took ${serializableSpan.durationInMs}ms`; } return `
Server was unable to respond in time (${requestTimeoutMs / 1000}s)