From 57aefa7df8ffffe74f87cac72465f79329451921 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 19 Jan 2022 15:40:50 +0100 Subject: [PATCH] fix: logtail to log request start and end --- packages/gateway/src/env.js | 2 ++ packages/gateway/src/index.js | 7 +++++-- packages/gateway/src/logs.js | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/gateway/src/env.js b/packages/gateway/src/env.js index ac2bdaf49f..a419040206 100644 --- a/packages/gateway/src/env.js +++ b/packages/gateway/src/env.js @@ -42,7 +42,9 @@ export function envAll(request, env, ctx) { env.genericMetricsDurable = env.GENERICMETRICS env.cidsTrackerDurable = env.CIDSTRACKER env.REQUEST_TIMEOUT = env.REQUEST_TIMEOUT || 20000 + env.log = new Logging(request, env, ctx) + env.log.time('request') } /** diff --git a/packages/gateway/src/index.js b/packages/gateway/src/index.js index 647defb347..f2f5ca2ae5 100644 --- a/packages/gateway/src/index.js +++ b/packages/gateway/src/index.js @@ -36,9 +36,12 @@ function serverError(error, request, env) { export default { async fetch(request, env, ctx) { try { - return await router.handle(request, env, ctx) + const res = await router.handle(request, env, ctx) + env.log.timeEnd('request') + return env.log.end(res) } catch (error) { - return serverError(error, request, env) + env.log.timeEnd('request') + return env.log.end(serverError(error, request, env)) } }, } diff --git a/packages/gateway/src/logs.js b/packages/gateway/src/logs.js index 5bd39e343c..103162d8a2 100644 --- a/packages/gateway/src/logs.js +++ b/packages/gateway/src/logs.js @@ -88,6 +88,10 @@ export class Logging { } async postBatch() { + if (!this.env.LOGTAIL_TOKEN) { + return + } + if (this.logEventsBatch.length > 0) { const batchInFlight = [...this.logEventsBatch] this.logEventsBatch = []