This page describes how to use the Node.js Serverless Tracer API to fine-tune your configuration.

Create or Modify Exit Calls

You need to create an exit call if your function does not use HTTP or inter-AWS Lambda exit calls.

The code snippet below demonstrates how you might create an exit call for an AWS Lambda function that calls a MySQL database: 

// Use the exit call API in your AWS Lambda handler function
 
module.exports.myLambdaHandler = async function () {
  const queryPromise = new Promise(function (resolve, reject) {
    const dbConfig = {
      host: '127.0.0.1', // The host where MySQL server runs
      port: 3306,
      user: 'root',
      database: 'movies'
    };
    const mysql = require('mysql');
    const mysqlClient = mysql.createPool(dbConfig);
    mysqlClient.getConnection(function (err, connection) {
      if (err) {
        reject({
          statusCode: 500,
          body: 'Couldn\'t make the connection to mysql client.'
        });
        return;
      }
      // Create an Exit Call
      var exitCall = tracer.startExitCall({
        exitType: 'DB',
        exitSubType: 'DB',
        identifyingProperties: {
          'HOST': '127.0.0.1', // The host where MySQL server runs
          'PORT': '3306',
          'DATABASE': 'movies',
          'VENDOR': 'MYSQL'
        }
      });
      connection.query('SELECT * FROM ClassicHits', function (err, results) {
        connection.release();
        if (err) {
          reject({
            statusCode: 500,
            body: 'Mysql query failed'
          });
          return;
        }
 
		//Stop the exit call
        tracer.stopExitCall(exitCall);
        resolve({
          statusCode: 200,
          body: JSON.stringify(results)
        });
      });
    });
  });
  return queryPromise;
}
JS

Exit Call Types and Identifying Properties

In the Controller, each exit call has a distinct type, determined by a set of identifying properties. The exitCall types, subtypes, and identifying properties are listed below. For exit call subtypes, you can use the examples listed in the table or use an empty string.  


Exit Call Type

Exit Call Subtype

Identifying Properties

HTTPHTTP

HOST

PORT

URL

QUERY STRING

DB

DB


URL

HOST

PORT

DATABASE

VERSION

VENDOR

CACHE

CACHE


VENDOR

SERVER POOL 

CUSTOM

CASSANDRA CQL

COUCHBASE

AMAZON WEB SERVICES

MONGODB


Any user-defined set of properties


Transaction Error Reports

By default, exceptions are detected by the tracer automatically. If your business logic defines the transaction as in error for any other reason you can customize the error report method to mark the transaction in error. 

The code snippet below demonstrates how you might create a transaction error report:


// Transaction Error Report
module.exports.apiTransactionErrorReport = function(event, context, callback) {
  setTimeout(function () {
    tracer.reportTransactionError(new Error('API Error'));
    callback(null, {
      statusCode: 200,
      body: 'Transaction Error Reported'
    });
  }, 100);
}

JS


Exit Call Error Reports

By default, the Node.js Serverless Tracer automatically discovers exit calls passed over HTTP or inter-AWS Lambda calls. Use the reportExitCallError method to report errors and exceptions that occur during exit call execution.

The code snippet below demonstrates how you might create an exit call error report:


// Use the exit call API in your AWS Lambda handler function
  
module.exports.myLambdaHandler = async function () {
  const queryPromise = new Promise(function (resolve, reject) {
    const dbConfig = {
      host: '127.0.0.1', // The host where your MySQL server runs
      port: 3306,
      user: 'root',
      database: 'movies'
    };
    const mysql = require('mysql');
    const mysqlClient = mysql.createPool(dbConfig);
    mysqlClient.getConnection(function (err, connection) {
      if (err) {
        reject({
          statusCode: 500,
          body: 'Couldn\'t make the connection to mysql client.'
        });
        return;
      }
      // Create an Exit Call
      var exitCall = tracer.startExitCall({
        exitType: 'DB',
        exitSubType: 'DB',
        identifyingProperties: {
          'HOST': '127.0.0.1', // The host where your MySQL server runs
          'PORT': '3306',
          'DATABASE': 'movies',
          'VENDOR': 'MYSQL'
        }
      });
      connection.query('SELECT * FROM ClassicHits', function (err, results) {
        connection.release();
 
		//Create an exit call error report
        if (err) {
          tracer.reportExitCallError(exitCall, 'MYSql error', 'Failed in making the query');
          reject({
            statusCode: 500,
            body: 'Mysql query failed'
          });
          return;
        }
        resolve({
          statusCode: 200,
          body: JSON.stringify(results)
        });
      });
    });
  });
  return queryPromise;
}
JS


Override the Tracer's Default Behavior

If you prefer to customize the function and business transaction names, or keep the tracer settings in your source code, you can override the environment variables. This option allows you to retrieve values at runtime from any desired source.

The code snippet below demonstrates how to override the default behavior of the Node.js tracer: 

tracer.init({
  controllerHostName: 'controllerHost',
  controllerPort: 8080,
  accountAccessKey: 'controllerAccountAccessKey',
  accountName: 'controllerAccountName',
  applicationName: 'applicationName',
  tierName: 'tierName',
  serverlessApiEndpoint: 'serverlessApiEndpoint',
  proxyHost: 'proxyHost',
  proxyPort: '80',
  proxyUser: 'proxyUser',
  proxyPassword: 'proxyPassword',
  certificateFile: 'proxyCertFile',
  debug: true
});
JS


Amazon Web Services, the AWS logo, AWS, and any other AWS Marks used in these materials are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries.