{"id":16255268,"url":"https://github.com/rpgeeganage/ifto","last_synced_at":"2025-03-16T13:30:50.789Z","repository":{"id":57271511,"uuid":"166303521","full_name":"rpgeeganage/ifto","owner":"rpgeeganage","description":"A simple debugging module for AWS Lambda (λ) timeout","archived":false,"fork":false,"pushed_at":"2019-02-11T17:26:47.000Z","size":379,"stargazers_count":74,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-27T09:31:08.293Z","etag":null,"topics":["aws","aws-lambda","aws-lambda-node","debug","debugging-tool","lambda","node-js","node-module","nodejs","npm","npm-module","npm-package","typescript","typescript-library"],"latest_commit_sha":null,"homepage":"https://rpgeeganage.github.io/ifto/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rpgeeganage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-17T22:02:56.000Z","updated_at":"2024-11-30T05:49:48.000Z","dependencies_parsed_at":"2022-09-08T21:42:01.926Z","dependency_job_id":null,"html_url":"https://github.com/rpgeeganage/ifto","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fifto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fifto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fifto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpgeeganage%2Fifto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rpgeeganage","download_url":"https://codeload.github.com/rpgeeganage/ifto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243815567,"owners_count":20352191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-lambda","aws-lambda-node","debug","debugging-tool","lambda","node-js","node-module","nodejs","npm","npm-module","npm-package","typescript","typescript-library"],"created_at":"2024-10-10T15:29:12.585Z","updated_at":"2025-03-16T13:30:50.189Z","avatar_url":"https://github.com/rpgeeganage.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# IFTO - If (Lambda) Timeout\n[![License](https://img.shields.io/github/license/rpgeeganage/ifto.svg)](https://img.shields.io/github/license/rpgeeganage/ifto.svg)\n[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/rpgeeganage/ifto.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/rpgeeganage/ifto/context:javascript)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b9cab5f9a44143c48fd12df6fe1819ee)](https://www.codacy.com/app/rpgeeganage/ifto?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=rpgeeganage/ifto\u0026amp;utm_campaign=Badge_Grade)\n[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/b9cab5f9a44143c48fd12df6fe1819ee)](https://www.codacy.com/app/rpgeeganage/ifto?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=rpgeeganage/ifto\u0026utm_campaign=Badge_Coverage)\n[![Build Status](https://travis-ci.org/rpgeeganage/ifto.svg?branch=master)](https://travis-ci.org/rpgeeganage/ifto)\n[![Known Vulnerabilities](https://snyk.io/test/github/rpgeeganage/ifto/badge.svg?targetFile=package.json)](https://snyk.io/test/github/rpgeeganage/ifto?targetFile=package.json)\n[![Maintainability](https://api.codeclimate.com/v1/badges/5a3c79b82c052f0ac4ca/maintainability)](https://codeclimate.com/github/rpgeeganage/ifto/maintainability)\n\n### TypeScript Doc: [https://rpgeeganage.github.io/ifto/doc/](https://rpgeeganage.github.io/ifto/doc/)\n\nThe purpose of this module is to work as a debugging tool during a timeout. It is hard to debug when a lambda exit exists with ```Task timed out after 2.00 seconds```.\n\n* One solution is to increase the ```Timeout``` value. Although this fixed the problem (sometimes), there is no guarantee that, a timeout may not occur again until proper debugging has done.\n\n* Another solution is to a use a module like [```Debug```](https://www.npmjs.com/package/debug) and enable logging. The problem is this, will create log entries irrespective of the ```timeout```.\n\n## How is this module work?\nThis package [calculates](https://github.com/rpgeeganage/ifto#ifto_flush_when) possible ```timeout``` errors using ```getRemainingTimeInMillis()``` provided in the [```Context```](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html) object. [```Context```](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html) is passed during the executing of the lambda as mentioned below.\n\n```ts\nasync handler(event: SQSEvent, context: Context) {\n  ...\n}\n```\n\n## Configuration\n\n###### (Environment variables)\n\n* **ifto_start** or **IFTO_START**\n\nPlease set the ```ifto_start``` as ```true``` in environment variables to allow the monitoring. (Monitoring will not start without setting the value as mentioned.)\n\n* **ifto_flush_when** or **IFTO_FLUSH_WHEN**\n\nThis indicates the ```minimum``` number of ```milliseconds``` remaining in ```context.getRemainingTimeInMillis()```, before flushing the logs to the output. (default setting is to write using ```console.log```). See the below.\n\n   (Default value is ***50***)\n```\nif context.getRemainingTimeInMillis() \u003c= ifto_flush_when then\n  flush the logs to standard output\nend if\n```\n\n## How to use it?\nAssume that you lambda is structured as follows.\n\n\tindex.ts\n\tlib/handler.ts\n\n##### modifications in ```index.ts```\n\n```ts\n/**\n * Just import the Ifto module.\n * This will, create a Ifto in the \"global\" name space.\n * So other files can use Ifto module without creating a object\n */\nimport 'Ifto';\nimport { Context, SQSEvent } from 'aws-lambda';\nimport { handler } from './lib/handler';\n\nexport async function myLambda(event: SQSEvent, context: Context) {\n  Ifto\n    .addLambdaContext(context) // Add the context module\n    .init(process.env); // Add the process.env inorder to read the required configurations.\n\n  // Use Ifto.log() to add log entry\n  Ifto\n    .log('My lambda execution started.');\n\n\n  // Use Ifto.monitor() function to start monitoring, if configured properly.\n  return Ifto.monitor(\n    // Pass the handler function\n    handler(event)\n  );\n}\n```\n\n##### modifications in ``lib/handler.ts``\n\n```ts\n/**\n * below import statement will required, only if you write individual unit tests for this file\n */\nimport 'Ifto';\nimport { SQSEvent } from 'aws-lambda';\n\n/*\n * You don't have to pass the context object here\n */\nexport async function handler(event: SQSEvent) {\n  ...\n  Ifto.log('log entry 1');\n\n  ...\n  Ifto.log('log entry 2');\n\n  ...\n  Ifto.log('log entry 3');\n\n}\n```\n\n## Methods\n* **Ifto.addLambdaContext(```context```);**\n\nset ```Context``` object passed to the lambda function.\n* **Ifto.init(```process.env```);**\n\nPass the ```process.env``` to read the environment variables defined.\n* **Ifto. log(```logMessage```);**\n\n  Adds the log message, so in the case of ```timeout```, this message will be flushed to the standard output.\n* **Ifto.monitor(```Promise```);**\n\nAccepts the handler function to in order to monitoring. (This won't change the ```return values``` or ```errors thrown``` buy the handler function).\n\n\n## Final output - (in case of ```Timeout error```)\n```\n2019-01-20T11:27:26.578Z    ccbc1d49-3336-47c2-9d49-c1d47ffc23de\nExpecting a possible lambda timeout.\nOnly 50 milliseconds remaining.\n(If this is a false positive error, change value by setting up environment variable \"ifto_flush_when\").\nCurrent log:\n2019-0-0T11:27:24.629 0: My lambda execution started.\n2019-0-0T11:27:24.919 1: log entry 1\n2019-0-0T11:27:24.956 2: log entry 2\n2019-0-0T11:27:25.236 3: log entry 3\n```\n\n## Spy modules :alien:\nThese modules are used to spy on operations which can take more execution time.\n***(More modules will be added in the future :rocket: :rocket:).***\n### HTTP / HTTPS spy module :cop:\nThis module keeps track **(most recent 10)** and outputs the ```URL``` of an ```HTTP``` or ```HTTPS``` request which is not complete at the time of flushing the logs.\n\n### Output with spy modules :point_right:\n```\nSTART RequestId: 55ca052e-45ae-49c5-89ef-bd1e5bfb0abf Version: $LATEST\n2019-02-10T16:24:45.737Z\t55ca052e-45ae-49c5-89ef-bd1e5bfb0abf\nExpecting a possible lambda timeout.\nOnly 50 milliseconds remaining.\n(If this is a false positive error change the value by setting up the environment variable \"ifto_flush_when\").\nCurrent log:\n2019-02-10T17:24:43.41 0: Handler entry\n2019-02-10T17:24:43.53 1: Start operation 1\n2019-02-10T17:24:43.54 2: Start operation 2\n2019-02-10T17:24:45.35 3: Run HTTP requests\n\n******************\nSpied module logs:\n******************\n\nhttp\n\nPossible unfinished HTTP requests\n2019-02-10T17:24:45.53 https://ifto-spy-testing.free.beeceptor.com/a-request-which-takes-long-time-to-process-004\n2019-02-10T17:24:45.41 https://ifto-spy-testing.free.beeceptor.com/a-request-which-takes-long-time-to-process-003\n2019-02-10T17:24:45.40 https://ifto-spy-testing.free.beeceptor.com/a-request-which-takes-long-time-to-process-002\n2019-02-10T17:24:45.38 https://ifto-spy-testing.free.beeceptor.com/a-request-which-takes-long-time-to-process-001\n\nEND RequestId: 55ca052e-45ae-49c5-89ef-bd1e5bfb0abf\nREPORT RequestId: 55ca052e-45ae-49c5-89ef-bd1e5bfb0abf\tDuration: 3003.15 ms\tBilled Duration: 3000 ms \tMemory Size: 512 MB\tMax Memory Used: 226 MB\n2019-02-10T16:24:45.789Z 55ca052e-45ae-49c5-89ef-bd1e5bfb0abf Task timed out after 3.00 seconds\n```\n\n## Important note\n:bulb: The default of ```ifto_flush_when``` is ``50`` and it was decided by running couple of lambdas and printing ```context.getRemainingTimeInMillis()``` value. In case of ```false positive``` change this value.\n\n:bulb: I haven't run any analysis on ``memory usage``.\n\n:bulb: I would thankfull to hear ``thoughts`` and ``bugs``.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpgeeganage%2Fifto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frpgeeganage%2Fifto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpgeeganage%2Fifto/lists"}