UNPKG

9.18 kBMarkdownView Raw
1# Thundra Lambda Node.js Agent
2
3[![Coverage Status](https://coveralls.io/repos/github/thundra-io/thundra-lambda-agent-nodejs/badge.svg?branch=master)](https://coveralls.io/github/thundra-io/thundra-lambda-agent-nodejs?branch=master)
4[![CircleCI](https://circleci.com/gh/thundra-io/thundra-lambda-agent-nodejs.svg?style=svg)](https://circleci.com/gh/thundra-io/thundra-lambda-agent-nodejs)
5
6Instrument and profile your AWS lambda functions with zero overhead.
7
8Check out [example projects](https://github.com/thundra-io/thundra-examples-lambda-nodejs) for a quick start and [Thundra docs](https://docs.thundra.io/) for more information.
9
10## Installation
11
12```bash
13npm install @thundra/core --save
14```
15
16## Usage
17
18Just require this module, pass your api key to it and wrap your handler:
19
20```js
21const thundra = require("@thundra/core")({ apiKey: "MY_APIKEY" });
22
23exports.handler = thundra((event, context,callback) => {
24 callback(null, "Hello Thundra!");
25});
26```
27
28Thundra will monitor your AWS lambda function and report automatically!
29
30`context.done`, `context.succeed` and `context.fail` are also supported:
31
32```js
33const thundra = require("@thundra/core")({ apiKey: "MY_APIKEY" });
34
35exports.handler = thundra((event, context) => {
36 context.succeed("Hello Thundra!");
37});
38```
39
40## Configuration
41You can configure Thundra using **environment variables** or **module initialization parameters**.
42
43Environment variables have **higher precedence** over initialization parameters.
44
45Check out the [configuration part](https://thundra.readme.io/docs/nodejs-configuration) of our docs for more detailed information.
46
47#### 1. Environment variables
48
49| Name | Type | Default Value |
50|:------------------------------------------------------------------------ |:------:|:-------------:|
51| thundra_apiKey | string | - |
52| thundra_lambda_warmup_warmupAware | bool | false |
53| thundra_agent_lambda_application_stage | string | empty |
54| thundra_agent_lambda_application_domainName | string | API |
55| thundra_agent_lambda_application_className | string | AWS-Lambda |
56| thundra_agent_lambda_disable | bool | false |
57| thundra_agent_lambda_timeout_margin | number | 200 |
58| thundra_agent_lambda_report_rest_baseUrl | string | https<nolink>://api.thundra.io/v1 |
59| thundra_agent_lambda_report_cloudwatch_enable | bool | false |
60| thundra_agent_lambda_trace_disable | bool | false |
61| thundra_agent_lambda_metric_disable | bool | false |
62| thundra_agent_lambda_log_disable | bool | false |
63| thundra_agent_lambda_trace_request_skip | bool | false |
64| thundra_agent_lambda_trace_response_skip | bool | false |
65| thundra_agent_lambda_trace_instrument_disable | bool | false |
66| thundra_agent_lambda_trace_instrument_traceableConfig | string | empty |
67| thundra_agent_lambda_trace_instrument_file_prefix | string | empty |
68| thundra_agent_lambda_log_loglevel | string | TRACE |
69| thundra_agent_lambda_integrations | string | empty |
70| thundra_agent_lambda_publish_report_rest_trustAllCertificates | bool | false |
71| thundra_agent_lambda_debug_enable | bool | false |
72| thundra_agent_lambda_trace_instrument_integrations_disable | array | [] |
73| thundra_agent_lambda_metric_sample_sampler_timeAware_timeFreq | number | 300000 |
74| thundra_agent_lambda_metric_sample_sampler_countAware_countFreq | number | 10 |
75| thundra_agent_lambda_log_console_shim_disable | bool | false |
76| thundra_agent_trace_instrument_integrations_spanContext_disable | bool | false |
77| thundra_agent_lambda_xray_disable | bool | false |
78| thundra_agent_lambda_trace_span_listener | string | empty |
79| thundra_agent_lambda_sample_timed_out_invocations | bool | false |
80| thundra_agent_lambda_trace_integrations_redis_command_mask | bool | false |
81| thundra_agent_lambda_trace_integrations_rdb_statement_mask | bool | false |
82| thundra_agent_lambda_trace_integrations_aws_dynamodb_statement_mask | bool | false |
83| thundra_agent_lambda_trace_integrations_elastic_statement_mask | bool | false |
84
85#### 2. Module initialization parameters
86
87| Name | Type | Default Value |
88|:---------------|:------:|:-------------:|
89| apiKey | string | - |
90| disableThundra | bool | false |
91| plugins | array | [ ] |
92
93
94## Async Monitoring with Zero Overhead
95By default, Thundra agent reports by making an HTTPS request. This adds an overhead to your lambda function.
96
97Instead, you can [setup async monitoring](https://docs.thundra.io/docs/how-to-setup-async-monitoring) in **2 minutes** and monitor your lambda functions with **zero overhead**!
98
99Check out our async monitoring example at our [example projects](https://github.com/thundra-io/thundra-examples-lambda-nodejs) for a quick start.
100
101
102## Log Support
103You can monitor your logs using Thundra and enjoy the three pillars of observability in one place!
104
105```js
106const thundra = require("@thundra/core");
107
108const logger = thundra.createLogger();
109
110exports.handler = thundra({
111 apiKey: "MY_APIKEY",
112})((event, context, callback) => {
113 logger.info("Hello %s", "Thundra");
114 callback(null, "Hello Thundra!");
115});
116```
117
118You can also set the name of a logger while creating it (default name is `default`):
119
120```js
121const logger = thundra.createLogger({loggerName: "Bob"});
122```
123
124Logger's name will be visible in Thundra's trace chart.
125
126## How to use Thundra loggers
127
128You can log by two different ways.
129
130### 1. Using `trace`, `debug`, `info`, `warn`, `error`, `fatal` methods
131
132All these methods support `printf`-like format. Same as Node's [`util.format`](https://nodejs.org/api/util.html#util_util_format_format_args).
133```js
134const thundra = require("@thundra/core");
135const logger = thundra.createLogger();
136
137logger.trace("Hey, I %s things", "trace");
138logger.debug("Someone is %s %d"," debugging", 2);
139logger.info("Get some info","and more");
140logger.warn("I am warning you %s", "!!!");
141logger.error("Error Error Error...");
142logger.fatal("FATALITY");
143```
144
145### 2. Using `log` method
146
147Pass an object with `level` and `message` fields:
148```js
149const thundra = require("@thundra/core");
150const logger = thundra.createLogger();
151
152logger.log({
153 level: "trace",
154 message: "Hey, I am tracing."
155});
156```
157
158You can also pass `level` as a string, this way you can use `printf`-like formatting:
159
160```js
161logger.log("trace", "Hey, I am %s", "tracing.");
162```
163`level` can be one of the following: `"trace"`, `"debug"`, `"info"`, `"warn"`, `"error"`, `"fatal"`
164
165## Log Levels
166
167In increasing precedence: **`trace`**, **`debug`**, **`info`**, **`warn`**, **`error`**, **`fatal`**.
168
169You can set the log level by setting the environment variable `thundra_log_logLevel` to one of the following:
170* `trace`
171* `debug`
172* `info`
173* `warn`
174* `error`
175* `fatal`
176* `none`
177
178For instance, if `thundra_log_logLevel` is:
179* `debug`, only `debug` and higher precedence logs will be reported.
180* `none`, none of the logs will be reported.
181
182## Warmup Support
183You can cut down cold starts easily by deploying our lambda function [`thundra-lambda-warmup`](https://github.com/thundra-io/thundra-lambda-warmup).
184
185Our agent handles warmup requests automatically so you don't need to make any code changes.
186
187You just need to deploy `thundra-lambda-warmup` once, then you can enable warming up for your lambda by
188* setting its environment variable `thundra_lambda_warmup_warmupAware` **true** OR
189* adding its name to `thundra-lambda-warmup`'s environment variable `thundra_lambda_warmup_function`.
190
191Check out [this part](https://thundra.readme.io/docs/how-to-warmup) in our docs for more information.
192
193## How to build
194[Webpack](https://webpack.js.org/) is used as a module bundler.
195
196To build the project,
197 ```bash
198 npm install
199 npm run build
200 ```
201
202## How to test
203Tests are written using [Jest](https://facebook.github.io/jest/).
204
205To run tests,
206 ```bash
207 npm run test
208 ```
209
210## Changelog
211
212Please see the [CHANGELOG](https://github.com/thundra-io/thundra-lambda-agent-nodejs/blob/master/CHANGELOG.md) file.
213
\No newline at end of file