Skip to content

Commit

Permalink
Extract endpoint request logging and metrics into function
Browse files Browse the repository at this point in the history
  • Loading branch information
remvee committed May 21, 2024
1 parent 4a69710 commit 31e3ff4
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions policies/aggregation/aggregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,45 +129,50 @@ 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
// service not available), meaning we have no HTTP status. We
// 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) {
Expand Down

0 comments on commit 31e3ff4

Please sign in to comment.