diff --git a/packages/doorman-api/src/common/DoormanHandler.ts b/packages/doorman-api/src/common/DoormanHandler.ts index dac3ca4..900e0d9 100644 --- a/packages/doorman-api/src/common/DoormanHandler.ts +++ b/packages/doorman-api/src/common/DoormanHandler.ts @@ -30,6 +30,18 @@ export function getMetricFromRegistry(metricsRegistry: Registry, metric: stri return metricsRegistry.getSingleMetric(metric) as T; }; +export function gracefullyEndLogger(logger: Logger): Promise { + return new Promise((fulfill, reject) => { + // Add logic for other transports here ... + + logger.on('finish', () => { + fulfill(); + }); + + logger.end(); + }); +} + /** * A decorator for twilio handlers. It provides a metrics registry and * should implement timeout and cleanup jobs based on lambda timeout @@ -98,8 +110,7 @@ export function withMetrics clearOnError: true, batching: true, onConnectionError: (error) => { - console.log("in error block"); - console.error(error); + console.log("[CommonHandler] failed connecting to loki", error); getMetricFromRegistry(metricsRegistry, CommonMetrics.LOKI_ERROR).inc(1); }, }), @@ -200,22 +211,23 @@ export function withMetrics console.log("[CommonHandler] attempting to push metrics..."); try { - logger.end(async () => { - await pushGateway.push({ - jobName: functionName, - groupings: { - stage: context.STAGE, - }, - }); - console.log("[CommonHandler] pushed metrics successfully"); - clearTimeout(metricsTimeout); - callback(...result); + // push logs + await gracefullyEndLogger(logger); + + // push metrics + await pushGateway.push({ + jobName: functionName, + groupings: { + stage: context.STAGE, + }, }); - + console.log("[CommonHandler] pushed metrics / logs successfully"); + } catch (e: any) { - console.log("[CommonHandler] failed to push metrics, quietly discarding them", e); - clearTimeout(metricsTimeout); - callback(...result); + console.log("[CommonHandler] failed to push metrics and or logs; quietly discarding them", e); } + + clearTimeout(metricsTimeout); + callback(...result); }; };