I have a small function that gets sent log to it. In the function, I am trying to send the to AWS Cloudwatch however I have a few issues. Once one log has been sent I can not send another one until the first one has finished as I need the next sequenceToken so the next log knows where to add itself. I know that async iterators are the key here but not sure how to implement them in my code. Here is my current code which fails after the first log is sent:
const build = require('pino-abstract-stream');
const stream = async (options) => {
// Creates the AWS connection
const client = await createClient();
// Gets the first token
let sequenceToken = await getInitSequenceToken(client);
return build(function (source) {
source.on("data", async function (obj) {
// Every time the log is sent to my worker thread it prints out here. Obj contains the info from the log
const command = new PutLogEventsCommand({
logGroupName: "api",
logStreamName: `executive-${Config.env}-${Config.location}`,
logEvents: [
{
message: obj.msg,
timestamp: obj.time,
},
],
sequenceToken,
});
// Here I am sending the log to Clouwatch
const response = await client.send(command);
// Here I was updating the token but this fails as the next log is already sending
sequenceToken = response.nextSequenceToken;
});
});
};
source, a websocket ?for await, that is the "sync" version.responsein an outside variable(array), then once the stream close (source.on('end')), you'll loop with a reduce over the client.send