Thundra APM
Search…
Deployment Integrations
Enrich Tracing in Node.js

Manual Instrumentation with Open Tracing

Thundra's Node.js agent automatically traces a broad range of libraries by default when integration is complete. However, if you would like to fine-tune your traces and perform manual instrumentation, then you will need to get a tracer instance with the command thundra.tracer() in your Lambda function.

Using Manual Instrumentation

In your Lambda Handler code, add instrumentation to the operations you want to track. You can do that primarily by placing "spans" around operations of interest and adding log statements to capture useful data relevant to those operations. Below is a simple hello-world example:
Manual instrumentation example
1
const thundra = require("@thundra/core");
2
3
exports.handler = thundra()((event, context,callback) => {
4
// Get the tracer instance
5
const tracer = thundra.tracer();
6
// Manually create a new span to represent some group of operations
7
const parent = tracer.startSpan('parentSpan');
8
console.log('Doing some work in the parent span...');
9
// Create another span under the above span
10
const child = tracer.startSpan('childSpan');
11
console.log('In the child span...');
12
// Finish the innermost span(childSpan)
13
// You can use OpenTracing compatible span.close() method
14
child.close();
15
// Finish the parentSpan
16
// This time using the tracer.finishSpan() method provided by Thundra
17
tracer.finishSpan();
18
callback(null, 'Success');
19
});
Copied!

Thundra Specific Methods

Apart from being OpenTracing compatible, Thundra's Node.js agent also provides specific methods to allow easier configuration of Trace Support.
getActiveSpan/finishSpan
The Thundra tracer provides additional methods beyond those found in the OpenTracing spec.
Below you can see how to obtain an active span via the getActiveSpan method and finish the active span via the finishSpan method.
getActiveSpan() Example
1
// In your handler
2
3
const tracer = thundra.tracer();
4
5
const parent = tracer.startSpan('parent');
6
const child = tracer.startSpan('child', {childOf: parent});
7
8
const span = tracer.getActiveSpan(); //return child span as current active span
9
10
tracer.finishSpan(); // closes children and active span is parent
11
tracer.finishSpan(); // closes parent and active span is null
Copied!
Callback Wrapper
The Thundra tracer provides a wrapper that will wrap your asynchronous callback. The wrapper returns a closure that saves the current active span at the time of callback creation, and then uses the saved active span as a parent span when the callback occurs. In the example below, the Thundra tracer’s wrapper method wraps a callback function with a try/catch statement. It starts a span with the name callback at the beginning and closes the span either before returning or in the case of an error.
Callback Wrapper Example
1
// In your handler
2
3
const tracer = thundra.tracer();
4
5
const callback = function() {
6
console.log('callback called');
7
};
8
9
const parentSpan = tracer.startSpan('parent');
10
11
// Callback is wrapped with span whose parent will be parentSpan which is the current active span
12
const wrappedCallback = tracer.wrapper('callback', callback);
13
14
const doStuff = function(stuff, callback) {
15
const doStuffSpan = tracer.startSpan(‘doStuff’);
16
console.log(`Starting doing my ${stuff}`);
17
callback();
18
doStuffSpan.finish();
19
}
20
21
doStuff('yoga', wrappedCallback);
22
23
parentSpan.finish();
24
25
// Span tree right now :
26
// parentSpan
27
// / \
28
// callback doStuff
29
30
31
// Span tree without wrapper would be:
32
// parentSpan
33
// /
34
// doStuff
35
// /
36
// callback
Copied!