Skip to content

Commit

Permalink
conslidating under one commans to allow for future extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Sep 12, 2020
1 parent 887a3c8 commit d29fdb5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 74 deletions.
52 changes: 19 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ This tool lets you generate and open a CloudWatch Logs Insights URL with your de
`npm i -g @mhlabs/cwlogs-cli`

## Usage
You can specify which log groups you want to include as follows:

## By tag (Lambda logs only)

Command: `cwlogs tag [options]`

Usage:
```
$ cwlogs tag --help
Usage: cwlogs tag|t [options]
$ cwlogs insights --help
Usage: cwlogs insights|i [options]
Opens a CloudWatch Logs console with multiple Lambda log groups matching a tag
Opens a CloudWatch Logs console with multiple log groups matching a tag or prefix
Options:
-t, --tag [tag] Tag to filter on. Format: tagName:value1,value2. Optional. If omitted you will get prompted by available tags
-p, --pattern [pattern] Regex pattern used to filter the log group names
-r, --region [region] AWS region. Defaults to environment variable AWS_REGION
--profile [profile] AWS profile. Defaults to environment variable AWS_PROFILE
-h, --help display help for command
-t, --tag [tag] Tag to filter on. Format: tagName:value1,value2. Optional. If omitted you will get
prompted by available tags (default: false)
-p, --prefix [prefix] Log group prefix. I.e /aws/lambda/. The more granular this is specified, the faster the
command will run
--pattern [pattern] Regex pattern used to filter the log group names
--region [region] AWS region. Defaults to environment variable AWS_REGION
--profile [profile] AWS profile. Defaults to en
```

You can either use `--tag` or `--prefix` to group log groups
## By tag (Lambda logs only)

Command: `cwlogs insights --tag <tag?> [options]`

Example 1:
Query logs from lambda functions tagged with `team:ecommerce` that has the word `Api` in its name
`cwlogs tag -t team:ecommerce -p Api`
`cwlogs insights --tag team:ecommerce --pattern Api`

Example 2:
Query logs from lambda functions by cloudformation stack name tag using the guided menu
Expand All @@ -40,27 +40,13 @@ Query logs from lambda functions by cloudformation stack name tag using the guid

## By log group prefix

Command: `cwlogs prefix <prefix> [options]`

Usage:
```
$ cwlogs prefix --help
Usage: `cwlogs prefix|p [prefix] [options] [prefix]`
Opens a CloudWatch Logs console with multiple log groups matching a prefix
Options:
-p, --pattern [pattern] Regex pattern used to filter the log group names
-r, --region [region] AWS region. Defaults to environment variable AWS_REGION
--profile [profile] AWS profile. Defaults to environment variable AWS_PROFILE
-h, --help display help for command
```
Command: `cwlogs insights --prefix <prefix> [options]`

Example:
Query all RDS clusters' error logs
`cwlogs prefix /aws/rds/cluster --pattern \/error$`
`cwlogs insights --prefix /aws/rds/cluster --pattern \/error$`
![Demo](https://raw.githubusercontent.com/mhlabs/cwlogs-cli/master/demo/demo-prefix.gif)

## Known limitations
CloudWatch Logs Insights currently only allows up to 20 log groups in one query. The total numbe rof characters (query + log groups) may bot exceed 10000 characters.
CloudWatch Logs Insights currently only allows up to 20 log groups in one query. The total number of characters (query + log groups) may bot exceed 10000 characters.

Binary file modified demo/demo-prefix.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/demo-tag.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 26 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,58 @@ const open = require("open");
const package = require("./package.json");
require("@mhlabs/aws-sdk-sso");
const tagCommand = require("./src/TagCommand");
const regexCommand = require("./src/PrefixCommand");
const prefixCommand = require("./src/PrefixCommand");

AWS.config.credentialProvider.providers.unshift(
new AWS.SingleSignOnCredentials()
);

program.version(package.version, "-v, --vers", "output the current version");
program.version(package.version, "-v, --version", "output the current version");

program
.command("tag")
.alias("t")
.command("insights")
.alias("i")
.option(
"-t, --tag [tag]",
"Tag to filter on. Format: tagName:value1,value2. Optional. If omitted you will get prompted by available tags"
"Tag to filter on. Format: tagName:value1,value2. Optional. If omitted you will get prompted by available tags",
false
)
.option(
"-p, --pattern [pattern]",
"Regex pattern used to filter the log group names"
)
.option(
"-r, --region [region]",
"AWS region. Defaults to environment variable AWS_REGION"
)
.option(
"--profile [profile]",
"AWS profile. Defaults to environment variable AWS_PROFILE"
)
.description(
"Opens a CloudWatch Logs console with multiple Lambda log groups matching a tag"
"-p, --prefix [prefix]",
"Log group prefix. I.e /aws/lambda/. The more granular this is specified, the faster the command will run"
)
.action(async (cmd) => {
AWS.config.region = cmd.region || process.env.AWS_REGION;
process.env.AWS_PROFILE = cmd.profile || process.env.AWS_PROFILE;

const url = await tagCommand.generateUrl(cmd.tag, cmd.pattern);
open(url);
});
program
.command("prefix [prefix]")
.alias("p [prefix]")
.option(
"-p, --pattern [pattern]",
"--pattern [pattern]",
"Regex pattern used to filter the log group names"
)
.option(
"-r, --region [region]",
"--region [region]",
"AWS region. Defaults to environment variable AWS_REGION"
)
.option(
"--profile [profile]",
"AWS profile. Defaults to environment variable AWS_PROFILE"
)
.description(
"Opens a CloudWatch Logs console with multiple log groups matching a prefix"
"Opens a CloudWatch Logs console with multiple log groups matching a tag or prefix"
)
.action(async (prefix, cmd) => {
.action(async (cmd) => {
AWS.config.region = cmd.region || process.env.AWS_REGION;
process.env.AWS_PROFILE = cmd.profile || process.env.AWS_PROFILE;
const url = await regexCommand.generateUrl(prefix, cmd.pattern);
open(url);
const args = cmd.parent.args;
let url;
if (args.includes("--tag") || args.includes("-t")) {
url = await tagCommand.generateUrl(cmd.tag, cmd.pattern);
}
if (args.includes("--prefix") || args.includes("-p")) {
url = await prefixCommand.generateUrl(cmd.prefix, cmd.pattern);
}
if (url) {
open(url);
} else {
program.help();
}

});

program.parse(process.argv);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mhlabs/cwlogs-cli",
"version": "1.0.0",
"version": "1.0.1",
"description": "CLI tool to quickly set up cross log group search in CloudWatch Logs Insights based on prefix, tags and/or RegEx pattern",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions src/TagCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ async function generateUrl(tag, pattern) {
let tagKey;
let tagValues;

if (tag) {
if (typeof tag === "string" || tag instanceof String) {
const split = tag.split(":");
tagKey = split[0].trim();
tagValue = split[1].split(",").map((p) => p.trim());
} else {
} else if (tag === true) {
spinner.start();
const tagKeys = await getAllTagKeys(tags);
spinner.stop();
Expand All @@ -28,10 +28,10 @@ async function generateUrl(tag, pattern) {
name: "value",
type: "list",
choices: tagKeys.sort((a, b) => {
if (a.toLowerCase() > b.toLowerCase()) return 1;
if (a.toLowerCase() < b.toLowerCase()) return -1;
return 0;
}),
if (a.toLowerCase() > b.toLowerCase()) return 1;
if (a.toLowerCase() < b.toLowerCase()) return -1;
return 0;
}),
})
).value;
spinner.start();
Expand Down

0 comments on commit d29fdb5

Please sign in to comment.