
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
@printtracker/lambda-usage-tracker
Advanced tools
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.
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.
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.
$ npm install --save @printtracker/lambda-usage-tracker
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:
They are methods on the $BillingService instance that can be called like this:
billingService.start()
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 };
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.
Use this method to explicitly state when the timer should end. No special usage is needed. This function does not return any data.
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
.
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
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.
The npm package @printtracker/lambda-usage-tracker receives a total of 0 weekly downloads. As such, @printtracker/lambda-usage-tracker popularity was classified as not popular.
We found that @printtracker/lambda-usage-tracker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.