npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smoketail

v0.2.2

Published

Library and utility for tailing AWS CloudWatch Logs.

Downloads

198

Readme

smoketail

Library and utility for tailing AWS CloudWatch Logs.

Installation

  # utility
  $ npm install -g smoketail
  
  # library
  $ npm install smoketail

Utility

The smoketail utility is a command line app that allows you to download or follow CloudWatch Log Events. Authorization is handled via the default mechanisms (~/.aws/credentials) used by the AWS Node.js SDK .


  Usage: smoketail [options] <logGroupName>

  Options:

    -h, --help                       output usage information
    -V, --version                    output the version number
    -c, --credentials <name>         Profile name from ~/.aws/credentials ini.
    -i, --interleaved                Interleave the log results. (auto on if using -f).
    -f, --follow                     When at the end of the logstream, poll for more messages.
    -p, --pattern <pattern>          CloudWatch Logs filter pattern.
    -r, --region <region>            Set the AWS region for your logGroup. Default is us-east-1.
    -s, --streams <s1,s2,s3>         Comma spearateed list of logStreamNames.
    -t, --time-range <start>..<end>  Start and end range for events.

  Getting Started
  ---------------
  smoketail is a utility wrapping calls to the AWS node.js api CloudWatchLogs::filterLogEvents.
  Authenticate via default methods supported by the sdk. More information available at: 
  https://aws.amazon.com/sdk-for-node-js/ 

  Time Ranges
  -----------
  Either end of a time range is optional.  Accepts string and int formatstaken by the Javascript
  Date constructor. If passed a negative, will interpet as now - seconds (ie -300 is 5 minutes ago).

  Examples:

    $ smoketail my/logGroup -t -60
    $ smoketail my/logGroup -t -300..-60
    $ smoketail my/logGroup -t 2015-01-01T14:45:00Z..2015-01-01T15:00:00Z
    $ smoketail my/logGroup -t ..2015-01-01T15:00:00Z

Library

The smoketail library provides objects for iterating through or streaming CloudWatchLogData.

CWLogFilterEventStream

A readable stream for getting logEvents. The constructor takes two paramaters, a Filter Object (passed to CloudWatchLogs::filterLogEvents) and an AWS Config object (passed to CloudWatchLogs::constructor).

var CWLogFilterEventStream = require('smoketail').CWLogFilterEventStream;

var filterOpts = {
    logGroupName : 'myGroup',
    logStreamNames: ['stream1','stream2'],
    startTime: 1451606400000,
    follow: true // The follow parameter is specific to smoketail.  If set, once 
                 // all events are returned, the stream will be kept open an poll
                 // CloudWatchLogs for follow-on events using the current filter
                 // parameters.  Otherwise the 'end' event fill fire when all events
                 // are retuned.
};

var awsOpts = {
    region : 'us-east-1' // Note this is the default if the awsOpts param is ommitted.
};

var eventStream = new CWLogFilterEventStream(filterOpts,awsOpts);
eventStream.on('error', function(err){
    console.log(err);
});

eventStream.on('end', function(){
    console.log('The stream is over.');
});

eventStream.on('data',function(eventObject){
    ///**IMPORTANT**:  All data is returned in objectMode.
    console.log(
        'Timestamp: ', new Date(eventObject.timestamp),
        'Message: ', eventObject.message
    );
});

setTimeout(function(){
    eventStream.close();  // Close method will close stream, end app if nothing else is open
}, 30000);

Because all data presented by the CWLogFilterEventStream is raw objects, it cannot be piped directly to other streams expecting data in string or buffers. To faciliate this, the CWLogEventToMessage transformer is available. As a convenience, a default instantiation of the transformer object is included in the library (.logEventToMessage).

var CWLogFilterEventStream = require('smoketail').CWLogFilterEventStream,
    logEventToMessage      = require('smoketail').logEventToMessage;

var eventStream = new CWLogFilterEventStream({...},{...});
eventStream.pipe(logEventToMessage).pipe(process.stdout);

CWLogFilter

A promise based cursor-like object wrapping calls to the AWS filterLogEvents api. The CWLogFilterStream uses the CWLogFilter internally to implement its calls to the AWS api.

var CWLogFilter = require('smoketail').CWLogFilter;

var filter = new CWLogFilter( /* { region : 'us-east-1' } */);

// open takes the same parameters as a call to filterLogEvents
filter.open({
    logGroupName : 'myGroup',
    logStreamNames: ['stream1','stream2'],
    startTime: 1451606400000
})
.then(results){
    results.events.forEach(event){
        console.log(event.message);
    }
})
.catch(function(err){
    console.log(err);
});

If your logs query is going to require paginating results, the CWLogFilter object has a .next method that helps handle iteration with tokens. Calls to .next will use the filter parameters passed to .open along with the .nextToken on a filterLogEvents response to get the next page of rsults.

var CWLogFilter = require('smoketail').CWLogFilter;

var filter = new CWLogFilter( /* { region : 'us-east-1' } */);

// iterate wlll call next() and repeat until no more events
function iterate(results){
    if (!results || results.event) {
        return filter;
    }
    results.events.forEach(function(event){
        console.log(event.message);
    });
    
    if (filter.eof()){
        return filter;
    }
    return filter.next().then(iterateFilter);
}

// open takes the same parameters as a call to filterLogEvents
filter.open({ ... })
.then(iterate)
.catch(function(err){
    console.log(err);
});