Socket
Socket
Sign inDemoInstall

@printtracker/lambda-usage-tracker

Package Overview
Dependencies
3
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @printtracker/lambda-usage-tracker

Tracking your lambda usage can be hard, in fact it's impossible right now because AWS doesn't provide an API to get that from within the lambda function itself, your only bet is CloudWatch.


Version published
Maintainers
1
Created

Readme

Source

Lambda Billing Tracker

What is it?

Tracking your lambda usage can be hard, in fact it's impossible right now because AWS doesn't provide an API to get that from within the lambda function itself, your only bet is CloudWatch.

This library uses a timer to track how long your functions within the lambda are taking. This will get you a close guess at how long the lambda is running.

How accurate is it?

On a cold start this is less accurate considering this function doesn't actually track usage until the function is called by the Lambda container. Everything after is close enough that we can use this library reliably.

Installation

$ npm install --save @printtracker/lambda-usage-tracker

Clean Usage

Start by instantiating a new instance of $BillingService

import $BillingService from '@printtracker/lambda-usage-tracker';

let billingService = new $BillingService()

This service is ready to go! There are four methods you can use:

  • start
  • end
  • getUsage
  • measure
  • log

They are methods on the $BillingService instance that can be called like this:

billingService.start()

Lambda Usage

Implementation in a Typescript Lambda handler.ts file could look something like this:

import { Handler, Context } from 'aws-lambda';
import $BillingService from '@printtracker/lambda-usage-tracker';
//mongoose.set('debug', true);

// You should only ever need to import services in the handler
import { HelloWorldService } from './services';

// Initialize the handler function
const helloWorld: Handler = async (event: any, context: Context) => {
    // Instantiate a new instance of the billing service which tracks how long a function runs in ms
    let billingService = new $BillingService()

    // Execute any needed code here
    const data: [] = await HelloWorldService.helloWorld();

    // Construct the resposne object with the total lambda usage
    let response = {
            statusCode: statusCode || 400,
            headers: {
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Credentials': true,
            },
            body: {]
                // Send the data back and measure the usage, this needs to be done before calling getUsage
                payload: billingService.measure(data),
                usage: billingService.getUsage()
            }
        }

    return response;
}

export { helloWorld };

Method Reference

start

Use this method to explicitly start the timer where you want. By default when you instantiate a new instance of $BillingService the timer is started. To use the start method to explicitly start the time, do this:

let billingService = new $BillingService(false)
// Do whatever you need to do before you start the timer
billingService.start()

Notice that in order to not start the timer at class instantiation, we need to pass false as a parameter into the new $BillingService instance.

end

Use this method to explicitly state when the timer should end. No special usage is needed. This function does not return any data.

getUsage

Use this method to get the measurements the service saved. These measurements are returned in the form of the interface:

interface BaseBillingUsageInterface {
	startTime: number;
	endTime: number;
	totalTime: number;
	memorySize: number;
	recordCount: number;
	billedDuration?: number;
}

memorySize looks to the process.env.memorySize variable which you would need to create in a lambda environment. This variable assists in calulating the billedDuration.

measure

This method takes an array as input and returns an untouched version of that array but sets the $BillingService instance variable, recordCount to determine how many records in a JSON array were passed through the lambda. It returns the identical array so that you can use it inline similar to a pipeline process. Normally you would return your data like this:

let response = dataFromMyService;

Instead we do this:

let response = measure(dataFromMyService);

As long as the data type is an [] this works.

FAQs

Last updated on 09 Aug 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc