Skip to content

Commit

Permalink
feat: tail sns/sqs/kinesis shows timestamp
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
theburningmonk committed Oct 4, 2019
1 parent bd5a23e commit f944a76
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/commands/tail-kinesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const _ = require("lodash");
const AWS = require("aws-sdk");
const {Command, flags} = require("@oclif/command");
const {checkVersion} = require("../lib/version-check");
require("colors");

class TailKinesisCommand extends Command {
async run() {
Expand Down Expand Up @@ -90,8 +91,9 @@ const pollKinesis = async (streamName, shardIds) => {

if (!_.isEmpty(resp.Records)) {
resp.Records.forEach(record => {
const timestamp = new Date().toJSON().grey.bold.bgWhite;
const data = Buffer.from(record.Data, "base64").toString("utf-8");
console.log(data);
console.log(timestamp, "\n", data);
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/commands/tail-sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const AWS = require("aws-sdk");
const {getQueueUrl} = require("../lib/sqs");
const {Command, flags} = require("@oclif/command");
const {checkVersion} = require("../lib/version-check");
require("colors");

let seenMessageIds = [];

Expand Down Expand Up @@ -77,7 +78,8 @@ const pollSqs = async (queueUrl) => {

resp.Messages.forEach(msg => {
if (!seenMessageIds.includes(msg.MessageId)) {
console.log(msg.Body);
const timestamp = new Date().toJSON().grey.bold.bgWhite;
console.log(timestamp, "\n", msg.Body);
seenMessageIds.push(msg.MessageId);

// only remember 1000 messages
Expand Down
4 changes: 3 additions & 1 deletion src/lib/webserver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const restify = require("restify");
const axios = require("axios");
const ngrok = require("ngrok");
require("colors");

const respond = (onConfirmed) => (req, res, next) => {
const body = JSON.parse(req.body);
Expand All @@ -13,7 +14,8 @@ const respond = (onConfirmed) => (req, res, next) => {
next();
});
} else {
console.log(body.Message);
const timestamp = new Date().toJSON().grey.bold.bgWhite;
console.log(timestamp, "\n", body.Message);

res.send(200);
next();
Expand Down
7 changes: 4 additions & 3 deletions test/commands/tail-sns.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require("lodash");
const {expect, test} = require("@oclif/test");
const AWS = require("aws-sdk");
const Promise = require("bluebird");
Expand Down Expand Up @@ -80,7 +81,7 @@ describe("tail-sns", () => {
.command(["tail-sns", "-n", "my-topic-dev", "-r", "us-east-1"])
.it("creates a new webserver and connects to ngrok", ctx => {
expect(ctx.stdout).to.contain("finding the topic [my-topic-dev] in [us-east-1]");
const logMessages = consoleLog.mock.calls.map(x => x[0]).join("\n");
const logMessages = _.flatMap(consoleLog.mock.calls, call => call).join("\n");
expect(logMessages).to.contain("listening at https://lumigo.io");
expect(mockConnect.mock.calls).to.have.length(1);
});
Expand All @@ -91,7 +92,7 @@ describe("tail-sns", () => {
.it("stops the webserver when disconnected", async () => {
await Promise.delay(1000); // wait for mockOpenStdin to trigger callback

const logMessages = consoleLog.mock.calls.map(x => x[0]).join("\n");
const logMessages = _.flatMap(consoleLog.mock.calls, call => call).join("\n");
expect(logMessages).to.contain("stopping webserver...");
expect(logMessages).to.contain("terminating ngrok process...");
});
Expand All @@ -111,7 +112,7 @@ describe("tail-sns", () => {
}));
})
.it("handles SNS subscription flow", async () => {
const logMessages = consoleLog.mock.calls.map(x => x[0]).join("\n");
const logMessages = _.flatMap(consoleLog.mock.calls, call => call).join("\n");
expect(logMessages).to.contain("listening at https://lumigo.io");
expect(logMessages).to.contain("confirmed SNS subscription");
expect(logMessages).to.contain("polling SNS topic [arn:aws:sns:us-east-1:12345:my-topic-dev]...");
Expand Down
5 changes: 3 additions & 2 deletions test/commands/tail-sqs.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require("lodash");
const {expect, test} = require("@oclif/test");
const AWS = require("aws-sdk");
const Promise = require("bluebird");
Expand Down Expand Up @@ -72,7 +73,7 @@ describe("tail-sqs", () => {

// unfortunately, ctx.stdout doesn't seem to capture the messages published by console.log
// hence this workaround...
const logMessages = consoleLog.mock.calls.map(x => x[0]).join("\n");
const logMessages = _.flatMap(consoleLog.mock.calls, call => call).join("\n");
expect(logMessages).to.contain("message 1");
expect(logMessages).to.contain("message 2");
});
Expand Down Expand Up @@ -106,7 +107,7 @@ describe("tail-sqs", () => {

// unfortunately, ctx.stdout doesn't seem to capture the messages published by console.log
// hence this workaround...
const logMessages = consoleLog.mock.calls.map(x => x[0]).join("\n");
const logMessages = _.flatMap(consoleLog.mock.calls, call => call).join("\n");
expect(logMessages).to.contain("message 3");
expect(logMessages).to.contain("message 4");
});
Expand Down

0 comments on commit f944a76

Please sign in to comment.