Skip to content

Commit

Permalink
feat(query): Provide a default query if not specified (#188)
Browse files Browse the repository at this point in the history
**Old Behavior**

If the query parameter was not provided - the `[0]` arg - then an error
would be returned
```
No query provided
```

**New Behavior**

This merge request defaults the query to 
```
      fields @timestamp, @message, @logstream, @log
      | sort @timestamp desc
```

(if not provided).

**Testing**

Screenshot of the output from a manual test
```
No Cloudwatch Insight Query provided. Defaulting to:

      fields @timestamp, @message, @logstream, @log
      | sort @timestamp desc

Querying between 2023-10-06T20:43:15.096Z and 2023-10-06T21:43:15.096Z
    for 1 log group(s): ["/aws/lambda/CoreAPI-processesTaskGeneratePatientExport"]

    0 records (0.00 MB) scanned, 0 matched

No results returned.
```

---------

Co-authored-by: Kellen Stuart <[email protected]>
  • Loading branch information
Kellen-Stuart and Kellen Stuart authored Oct 18, 2023
1 parent 39b0982 commit 49fbf0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ cwq --log-group <log-group> '<query>'

Where `<log-group>` is the log group you want to search and `<query>` is the [CloudWatch Logs Insights query](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html) to search with.

If `<query>` is not specified, the [CloudWatch Logs Insights query](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html) will default to:

```
fields @timestamp, @message, @logStream, @log
| sort @timestamp desc
```

Further usage guidelines are described below and options can be found via the help argument:

```bash
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ process.on("SIGINT", async () => {
const endTime = time.parseTimeOrDuration(args.end);
const limit = args.limit;

const query = args._[0];
let query = args._[0];
if (!query) {
throw new Error("No query provided");
query = `
fields @timestamp, @message, @logStream, @log
| sort @timestamp desc
`;
console.error(
`No Cloudwatch Insight Query provided. Defaulting to: \n${query}`
);
}
const queryString = query.toString();

Expand Down

0 comments on commit 49fbf0c

Please sign in to comment.