From 31e3ff4311ac953e85ca332e5bce2ed5403bc1b7 Mon Sep 17 00:00:00 2001 From: Remco van 't Veer Date: Thu, 16 May 2024 15:27:13 +0200 Subject: [PATCH] Extract endpoint request logging and metrics into function --- policies/aggregation/aggregation.js | 49 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/policies/aggregation/aggregation.js b/policies/aggregation/aggregation.js index 08cb0990..1b02241c 100644 --- a/policies/aggregation/aggregation.js +++ b/policies/aggregation/aggregation.js @@ -129,26 +129,36 @@ module.exports = (config, { gatewayConfig: { serviceEndpoints } }) => { } concurrentRequestsMetric.labels(labels).inc() + // log and keep metrics for request to endpoint + const report = ({ statusCode, reqTimerEnd, ...rest }) => { + jsonLog.info({ + client: 'PROXY', + http_status: statusCode, + traceparent_id: outgoingTraceParent.id, + traceparent_parent_id: outgoingTraceParent.parent_id, + traceparent_trace_id: outgoingTraceParent.traceId, + ...rest + }) + + requestsTotalMetric.labels({ ...labels, code: statusCode }).inc() + concurrentRequestsMetric.labels(labels).dec() + requestDurationSecondsMetric.labels({ ...labels, code: statusCode }).observe((reqTimerEnd - reqTimerStart) / 1000) + } + proxy.on('proxyRes', (proxyRes, req, res) => { const remoteUrl = endpoint.url.replace(/\/$/, '') + req.url const statusCode = proxyRes.statusCode const method = req.method proxy.on('end', () => { const reqTimerEnd = new Date() - jsonLog.info({ - short_message: `${req.traceparent.traceId} - ${method} ${remoteUrl} ${statusCode}`, - traceparent_trace_id: outgoingTraceParent.traceId, - traceparent_id: outgoingTraceParent.id, - traceparent_parent_id: outgoingTraceParent.parent_id, - client: 'PROXY', - http_status: statusCode, + report({ + statusCode, + reqTimerEnd, request_method: method, - url: remoteUrl, - time_ms: reqTimerEnd - reqTimerStart + short_message: `${req.traceparent.traceId} - ${method} ${remoteUrl} ${statusCode}`, + time_ms: reqTimerEnd - reqTimerStart, + url: remoteUrl }) - requestsTotalMetric.labels({ ...labels, code: statusCode }).inc() - concurrentRequestsMetric.labels(labels).dec() - requestDurationSecondsMetric.labels({ ...labels, code: statusCode }).observe((reqTimerEnd - reqTimerStart) / 1000) }) }) // Error here means we got no HTTP response (timeout or @@ -156,18 +166,13 @@ module.exports = (config, { gatewayConfig: { serviceEndpoints } }) => { // log status code "0" in this case. proxy.on('error', (e) => { const reqTimerEnd = new Date() - jsonLog.error({ - traceparent_trace_id: outgoingTraceParent.traceId, - traceparent_id: outgoingTraceParent.id, - traceparent_parent_id: outgoingTraceParent.parent_id, - short_message: `${outgoingTraceParent.traceId} - ${e} 0`, - client: 'PROXY', + const statusCode = 0 + report({ + statusCode, + reqTimerEnd, error_msg: e.toString(), - http_status: 0 + short_message: `${outgoingTraceParent.traceId} - ${e} 0` }) - requestsTotalMetric.labels({ ...labels, code: 0 }).inc() - concurrentRequestsMetric.labels(labels).dec() - requestDurationSecondsMetric.labels({ ...labels, code: 0 }).observe((reqTimerEnd - reqTimerStart) / 1000) }) if (envelopRequest) {