Thundra APM
Search…
Deployment Integrations
Powered By GitBook
Sampling in Node.js SDK
You can reduce the amount of data that the Node.js agent sends to the Thundra Web Console by enabling sampling. You can either add built-in sampling rules to the Metric and Trace plugins, or you can provide a custom sampler and implement the sampling logic by yourself. Do note, however, that you can only sample timed-out invocations. To see an example, take a look at the project on GitHub for more examples.

Timed-Out Invocation Sampling

You can only send data to Thundra when a Lambda time-out happens. With timed-out sampling, you can focus on your timed-out invocations. The code below shows how to enable timed-out invocations programmatically:
Sampling timed-out invocations
1
const config = {
2
sampleTimedOutInvocations: true
3
};
4
5
const thundra = require("@thundra/core");
6
7
exports.handler = thundra(config)((event, context, callback) => {
8
callback(null, {msg : 'hello world'});
9
});
Copied!
You can also enable timed-out sampling by setting thundra_agent_lambda_sample_timed_out_invocations to true without any code change.

Masking Stack Traces

To reduce your data usage, you can mask the stack traces of errors that the Thundra Node.js agent collects. You can enable masking of stack traces by setting the environment variable thundra_agent_lambda_error_stacktrace_mask to “true” without any code change.

Composite Sampler

You can combine one or more samplers with the help of Composite Sampler, which can be used in any plugin. You are also to decide which logical operator (AND, OR) will be used when combining samplers. All you need to do is give the Composite Sampler to the sampler configuration of one of the Thundra agents’ plugin configurations. The example below shows how to configure a CompositeSampler, which combines the CountAwareSampler and TimeAwareSampler with an AND operation. The CompositeSampler is given to the trace plugin.
Composite sampler example
1
const thundra = require("@thundra/core");
2
3
// Imports
4
const CompositeSampler = thundra.samplers.CompositeSampler;
5
const CountAwareSampler = thundra.samplers.CountAwareSampler;
6
const TimeAwareSampler = thundra.samplers.TimeAwareSampler;
7
const SamplerCompositionOperator = thundra.samplers.SamplerCompositionOperator;
8
9
// Init two separate samplers
10
const sampler1 = new CountAwareSampler(2); // Samples every 2th.
11
const sampler2 = new TimeAwareSampler(3000); // Samples every 3 seconds.
12
13
// Create a composite sampler with count and time aware and combine them with AND // operator, default is OR
14
15
const sampler = new CompositeSampler([sampler1, sampler2], SamplerCompositionOperator.AND)
16
17
const compositeConfig = {
18
traceConfig: {
19
sampler
20
},
21
};
22
23
exports.handler = thundra(compositeConfig)((event, context, callback) => {
24
callback(null, {msg: event.msg});
25
});
Copied!

Metric Sampling

The Metric plugin provides three built-in samplers, and you can also use a custom or composite sampler. These samplers include:
    Count Aware Sampler
    Time Aware Sampler
    Error Aware Sampler
    Custom Sampler

Count Aware Sampler

The Count Aware Sampler enables you to sample metric data with a count frequency. For example, if the count frequency is 50, metric data will be sampled with every 50 Lambda invocations. Here is the configuration:
Count Aware Metric Sampler example
1
const thundra = require("@thundra/core");
2
3
const CountAwareSampler = thundra.samplers.CountAwareSampler;
4
5
const config = {
6
metricConfig: {
7
sampler: new CountAwareSampler(50) // Sample metrics every 50th invocation
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!
You can update count frequency without re-deploying Lambda functions. After enabling Count Aware Sampler with a programmatic configuration, change the count frequency by setting the thundra_agent_lambda_sampler_countAware_countFreq environment variable in AWS Lambda.

Time Aware Sampler

The Time Aware Sampler enables you to sample metric data with a time frequency. For example, if the time frequency is 300,000, metric data will be sampled every 300 seconds within consecutive Lambda invocations. Here is the configuration:
Time Aware Metric Sampler example
1
const thundra = require("@thundra/core");
2
3
const TimeAwareSampler = thundra.samplers.TimeAwareSampler;
4
5
const config = {
6
metricConfig: {
7
sampler: new TimeAwareSampler(300000) // Sample metrics every 300s
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!
You can update time frequency without re-deploying Lambda functions. After enabling Time Aware Sampler with a programmatic configuration, change the time frequency by setting the thundra_agent_lambda_sampler_timeAware_timeFreq environment variable in AWS Lambda.

Error Aware Sampler

The Error Aware Sampler enables you to sample metric data according to erroneous invocations of a Lambda function. For example, you can send metric data from only the Lambda execution that results in an error.
Error Aware Metric Sampler example
1
const thundra = require("@thundra/core");
2
3
const ErrorAwareSampler = thundra.samplers.ErrorAwareSampler;
4
5
const config = {
6
metricConfig: {
7
sampler: new ErrorAwareSampler() // Sample metrics when Lambda fails
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!

Custom Sampler

You also have the ability to implement a custom sampler. A custom sampler is an Object with the isSampled function. The isSampled function should return “true” if you want to send metric data to Thundra, and should return “false” if you do not want to send the data.
Custom metric sampler example
1
const customSamplerConfig = {
2
metricConfig: {
3
sampler: {
4
isSampled: () => {
5
// Decide what to do yourself here. return true/false.
6
return true;
7
}
8
}
9
},
10
};
11
12
const thundra = require("@thundra/core");
13
14
exports.handler = thundra(customSamplerConfig)((event, context, callback) => {
15
callback(null, {msg : 'hello world'});
16
});
Copied!

Trace Sampling

The Trace plugin provides four built-in samplers, and you can also use a custom or composite sampler. These samplers include:
    Duration Aware Sampler
    Error Aware Sampler
    Count Aware Sampler
    Time Aware Sampler
    Custom Sampler

Duration Aware Sampler

The Duration Aware Sampler enables you to sample trace data according to the duration of a Lambda function. For example, you can send trace data only if the duration of the Lambda function is longer than 500 milliseconds. Here is the configuration:
Duration Aware Trace Sampler example
1
const thundra = require("@thundra/core");
2
3
const DurationAwareSampler = thundra.samplers.DurationAwareSampler;
4
5
const config = {
6
traceConfig: {
7
sampler: new DurationAwareSampler() // Sample traces when Lambda fails
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!

Error Aware Sampler

The Error Aware Sampler enables you to sample trace data according to erroneous invocations of the Lambda function. For example, you can send trace data from only the Lambda execution that results in an error.
Error Aware Trace Sampler example
1
const thundra = require("@thundra/core");
2
3
const ErrorAwareSampler = thundra.samplers.ErrorAwareSampler;
4
5
const config = {
6
traceConfig: {
7
sampler: new ErrorAwareSampler() // Sample traces when Lambda fails
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!

Count Aware Sampler

The Count Aware Sampler enables you to sample trace data with a count frequency. For example, if the count frequency is 50, trace data will be sampled with every 50 Lambda invocations. Here is the configuration:
Count Aware Trace Sampler
1
const thundra = require("@thundra/core");
2
3
const CountAwareSampler = thundra.samplers.CountAwareSampler;
4
5
const config = {
6
traceConfig: {
7
sampler: new CountAwareSampler(50) // Sample traces every 50th invocation
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!
You can update count frequency without re-deploying Lambda functions. After enabling Count Aware Sampler with a programmatic configuration, change the count frequency by setting the thundra_agent_lambda_sampler_countAware_countFreq environment variable in AWS Lambda.

Time Aware Sampler

The Time Aware Sampler enables you to sample trace data with a time frequency. For example, if the time frequency is 300,000, trace data will be sampled every 300 seconds within consecutive Lambda invocations. Here is the configuration:
Time Aware Trace Sampler
1
const thundra = require("@thundra/core");
2
3
const TimeAwareSampler = thundra.samplers.TimeAwareSampler;
4
5
const config = {
6
traceConfig: {
7
sampler: new TimeAwareSampler(300000) // Sample traces every 300s
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!
You can update time frequency without re-deploying Lambda functions. After enabling Time Aware Sampler with a programmatic configuration, change the time frequency by setting the thundra_agent_lambda_sampler_timeAware_timeFreq' environment variable in AWS Lambda.

Root Aware Sampler

The Root Aware Sampler enables you to sample trace data with a type of span. In this case, every span will be sampled except root span. Here is the configuration:
Root Aware Sampler
1
const thundra = require("@thundra/core");
2
3
const RootAwareSampler = thundra.samplers.RootAwareSampler;
4
5
const config = {
6
traceConfig: {
7
sampler: new RootAwareSampler() // Sample spans except root span
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!

Custom sampler

You also have the ability to implement a custom sampler. A custom sampler is an Object with the isSampled function. The isSampled function should return “true” if you want to send trace data to Thundra, and should return “false” if you do not want to send the data.
Custom trace sampler example
1
const customSamplerConfig = {
2
traceConfig: {
3
sampler: {
4
isSampled: (span) => {
5
// Decide what to do yourself here. return true/false.
6
return true;
7
}
8
}
9
},
10
};
11
12
const thundra = require("@thundra/core");
13
14
exports.handler = thundra(customSamplerConfig)((event, context, callback) => {
15
callback(null, {msg : 'hello world'});
16
});
Copied!

Log Sampling

The Log plugin provides three built-in samplers, and you can also use a custom or composite sampler. These samplers include:
    Error Aware Sampler
    Count Aware Sampler
    Time Aware Sampler
    Custom Sampler
    Log Level Filtering

Error Aware Sampler

The Error Aware Sampler enables you to sample log data according to erroneous invocations of the Lambda function. For example, you can send log data from only the Lambda execution that results in an error.
Error Aware Log Sampler example
1
const thundra = require("@thundra/core");
2
3
const ErrorAwareSampler = thundra.samplers.ErrorAwareSampler;
4
5
const config = {
6
logConfig: {
7
sampler: new ErrorAwareSampler() // Sample logs when Lambda fails
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!

Count Aware Sampler

The Count Aware Sampler enables you to sample log data with a count frequency. For example, if the count frequency is 50, log data will be sampled with every 50 Lambda invocations. Here is the configuration:
Count Aware Log Sampler example
1
const thundra = require("@thundra/core");
2
3
const CountAwareSampler = thundra.samplers.CountAwareSampler;
4
5
const config = {
6
logConfig: {
7
sampler: new CountAwareSampler(50) // Sample logs every 50th invocation
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!
You can update count frequency without re-deploying Lambda functions. After enabling Count Aware Sampler with a programmatic configuration, you can change the count frequency by setting the thundra_agent_lambda_sampler_countAware_countFreq environment variable in AWS Lambda.

Time Aware Sampler

The Time Aware Sampler enables you to sample log data with a time frequency. For example, if the time frequency is 300,000, log data will be sampled every 300 seconds within consecutive Lambda invocations. Here is the configuration:
Time Aware Log Sampler example
1
const thundra = require("@thundra/core");
2
3
const TimeAwareSampler = thundra.samplers.TimeAwareSampler;
4
5
const config = {
6
logConfig: {
7
sampler: new TimeAwareSampler(300000) // Sample logs every 300s
8
}
9
};
10
11
exports.handler = thundra(config)((event, context, callback) => {
12
callback(null, {msg : 'hello world'});
13
});
Copied!
You can update time frequency without re-deploying Lambda functions. After enabling Time Aware Sampler with a programmatic configuration, change the time frequency by setting the thundra_agent_lambda_sampler_timeAware_timeFreq' environment variable in AWS Lambda.

Custom sampler

You also have the ability to implement a custom sampler. A custom sampler is an Object with the isSampled function. The isSampled function should return “true” if you want to send trace data to Thundra, and should return “false” if you do not want to send the data.
Custom Log sampler example
1
const customSamplerConfig = {
2
logConfig: {
3
sampler: {
4
isSampled: () => {
5
// Decide what to do yourself here. return true/false.
6
return true;
7
}
8
}
9
},
10
};
11
12
const thundra = require("@thundra/core");
13
14
exports.handler = thundra(customSamplerConfig)((event, context, callback) => {
15
callback(null, {msg : 'hello world'});
16
});
Copied!

Log Level Filtering

The following are Thundra Log Levels in increasing precedence: Trace, debug, info, warn, error, and fatal. You can set the log level by setting the environment variable thundra_agent_lambda_log_loglevel to one of the following:
    Trace
    debug
    info
    warn
    error
    fatal
    none
For instance, if thundra_agent_lambda_log_loglevel is debug, only debug and higher precedence logs will be reported. None of the logs lower in precedence will be reported, such as Trace.
Last modified 3mo ago