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;
|
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
|
* A decorator for twilio handlers. It provides a metrics registry and
|
||||||
* should implement timeout and cleanup jobs based on lambda timeout
|
* 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,
|
clearOnError: true,
|
||||||
batching: true,
|
batching: true,
|
||||||
onConnectionError: (error) => {
|
onConnectionError: (error) => {
|
||||||
console.log("in error block");
|
console.log("[CommonHandler] failed connecting to loki", error);
|
||||||
console.error(error);
|
|
||||||
getMetricFromRegistry<Counter>(metricsRegistry, CommonMetrics.LOKI_ERROR).inc(1);
|
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...");
|
console.log("[CommonHandler] attempting to push metrics...");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.end(async () => {
|
// push logs
|
||||||
await pushGateway.push({
|
await gracefullyEndLogger(logger);
|
||||||
jobName: functionName,
|
|
||||||
groupings: {
|
// push metrics
|
||||||
stage: context.STAGE,
|
await pushGateway.push({
|
||||||
},
|
jobName: functionName,
|
||||||
});
|
groupings: {
|
||||||
console.log("[CommonHandler] pushed metrics successfully");
|
stage: context.STAGE,
|
||||||
clearTimeout(metricsTimeout);
|
},
|
||||||
callback(...result);
|
|
||||||
});
|
});
|
||||||
|
console.log("[CommonHandler] pushed metrics / logs successfully");
|
||||||
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.log("[CommonHandler] failed to push metrics, quietly discarding them", e);
|
console.log("[CommonHandler] failed to push metrics and or logs; quietly discarding them", e);
|
||||||
clearTimeout(metricsTimeout);
|
|
||||||
callback(...result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearTimeout(metricsTimeout);
|
||||||
|
callback(...result);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user