gracefully end logger so logs are pushed
All checks were successful
Build and push image for doorman-homeassistant / docker (push) Successful in 28s
Build and push Doorman UI / API / docker (push) Successful in 1m32s
Build and push image for doorman-homeassistant / deploy-gitainer (push) Successful in 4s

This commit is contained in:
Martin Dimitrov 2025-02-10 20:49:20 -08:00
parent 334635c004
commit 3b763ae5e7

View File

@ -30,6 +30,18 @@ export function getMetricFromRegistry<T>(metricsRegistry: Registry, metric: stri
return metricsRegistry.getSingleMetric(metric) as T;
};
export function gracefullyEndLogger(logger: Logger): Promise<void> {
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<T extends DoormanLambdaContext, U extends BaseEvent>
clearOnError: true,
batching: true,
onConnectionError: (error) => {
console.log("in error block");
console.error(error);
console.log("[CommonHandler] failed connecting to loki", error);
getMetricFromRegistry<Counter>(metricsRegistry, CommonMetrics.LOKI_ERROR).inc(1);
},
}),
@ -200,22 +211,23 @@ export function withMetrics<T extends DoormanLambdaContext, U extends BaseEvent>
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);
};
};