gracefully end logger so logs are pushed
This commit is contained in:
parent
334635c004
commit
3b763ae5e7
@ -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);
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user