1.32.0 • Published 2 years ago

epsagon-frameworks v1.32.0

Weekly downloads
2,843
License
MIT
Repository
github
Last release
2 years ago

Build Status npm version semantic-release

Epsagon Tracing for Node.js frameworks

Trace

This package provides tracing to Node.js applications for the collection of distributed tracing and performance metrics in Epsagon.

Contents

Installation

To install Epsagon, simply run:

npm install epsagon-frameworks

Usage

Auto-tracing

The simplest way to get started is to run your node command with the following environment variable:

export EPSAGON_TOKEN=<epsagon-token>
export EPSAGON_APP_NAME=<app-name-stage>
export EPSAGON_METADATA=FALSE
NODE_OPTIONS='-r epsagon-frameworks' <command>

For example:

export EPSAGON_TOKEN=<your-token>
export EPSAGON_APP_NAME=express-prod
export EPSAGON_METADATA=FALSE
NODE_OPTIONS='-r epsagon-frameworks' node app.js

You can see the list of supported frameworks

Calling the SDK

Another simple alternative is to copy the snippet into your code:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

To run on your framework please refer to supported frameworks

Tagging Traces

You can add custom tags to your traces, for easier filtering and aggregations.

Add the following call inside your code:

epsagon.label('key', 'value');
epsagon.label('userId', userId);

In some frameworks tagging can be done in different ways.

Custom Errors

You can set a trace as an error (although handled correctly) to get an alert or just follow it on the dashboard.

Add the following call inside your code:

try {
  // something bad happens
} catch (err) {
  epsagon.setError(err);
}

// Or manually specify Error object
epsagon.setError(Error('My custom error'));

Custom Warnings

This API allows you to flag the trace with a warning and also enables more flexible alerting

Add the following call inside your code:

try {
  // something bad happens
} catch (err) {
  epsagon.setWarning(err);
}

// Or manually specify Error object
epsagon.setWarning(Error('My custom error'));

In some frameworks custom errors can be declared in different ways.

Filter Sensitive Data

You can pass a list of sensitive properties and hostnames and they will be filtered out from the traces:

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
  ignoredKeys: ['password', /.*_token$/],
  urlPatternsToIgnore: ['example.com', 'auth.com'],
});

The ignoredKeys property can contain strings (will perform a loose match, so that First Name also matches first_name), regular expressions, and predicate functions. Also, you can set urlPatternsToIgnore to ignore HTTP calls to specific domains.

Ignore Endpoints

You can ignore certain incoming requests by specifying endpoints:

epsagon.ignoreEndpoints(['/healthcheck'])

Trace URL

You can get the Epsagon dashboard URL for the current trace, using the following:

# Inside some endpoint or function
console.log('Epsagon trace URL:', epsagon.getTraceUrl())

This can be useful to have an easy access the trace from different platforms.

Frameworks

The following frameworks are supported by Epsagon Frameworks.

FrameworkSupported VersionEpsagon LibraryAuto-tracing Supported
Express>=3.0.0epsagon-frameworks- x
Hapi>=17.0.0epsagon-frameworks- x
Koa>=1.1.0epsagon-frameworks- x
restify>=7.0.0epsagon-frameworks- x
fastify>=3.0.0epsagon-frameworks- x
KafkaJS>=1.2.0epsagon-frameworks- x
kafka-node>=3.0.0epsagon-frameworks- x
PubSub>=1.1.0epsagon-frameworks- x
SQS Consumer>=4.0.0epsagon-frameworks- x
amqplib>=0.5.0epsagon-frameworks- x
amqp>=0.2.0epsagon-frameworks- x
bunnybus>=7.0.0epsagon-frameworks- x
NATS>=1.4.0epsagon-frameworks- x
HTTP ServerAllepsagon-frameworks- x
WS (Websocket)>=7.3.1epsagon-frameworks- x
GenericAllepsagon-

Express

Tracing Express application can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the application is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

app.get('/', (req, res) => {
  req.epsagon.label('key', 'value');
  req.epsagon.setError(Error('My custom error'));
}

Hapi

Tracing Hapi application can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the application is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

server.route({
  method: 'GET',
  path:'/',
  handler: (request, h) => {
      request.epsagon.label('key', 'value');
      request.epsagon.setError(Error('My custom error'));
  }
});

Koa

Tracing Koa application can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the application is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

app.use(async ctx => {
  ctx.epsagon.label('key', 'value');
  ctx.epsagon.setError(Error('My custom error'));
});

restify

Tracing restify application can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the application is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

function respond(req, res, next) {
  req.epsagon.label('key', 'value');
  req.epsagon.setError(Error('My custom error'));
}

fastify

Tracing fastify application can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the application is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

fastify.get('/', (request, reply) => {
  request.epsagon.label('key', 'value');
  request.epsagon.setError(Error('My custom error'));
  reply.send({ hello: 'world' })
})

KafkaJS

Tracing kafkajs consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

await consumer.run({
  eachMessage: async ({ topic, partition, message }) => {
    message.epsagon.label('key', 'value');
    message.epsagon.setError(Error('My custom error'));
  },
})

kafka-node

Tracing kafka0node consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

consumer.on('message', function (message) {
  message.epsagon.label('key', 'value');
  message.epsagon.setError(Error('My custom error'));
})

PubSub

Tracing @google-cloud/pubsub consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

await consumer.run({
  eachMessage: async ({ topic, partition, message }) => {
    message.epsagon.label('key', 'value');
    message.epsagon.setError(Error('My custom error'));
  },
})

SQS Consumer

Tracing sqs-consumer consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

const messageHandler = message => {
  message.epsagon.label('key', 'value');
  message.epsagon.setError(Error('My custom error'));
};

Or in batch message handler:

const batchMessageHandler = messages => {
  messages[0].epsagon.label('key', 'value');
  messages[0].epsagon.setError(Error('My custom error'));
};

amqplib

Tracing amqplib consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

ch.consume(q, function cons(msg) {
  if (msg !== null) {
    msg.epsagon.label('key', 'value');
    msg.epsagon.setError(Error('My custom error'));
    ch.ack(msg);
  }
});

amqp

Tracing amqp consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

q.subscribe(function (message) {
  message.epsagon.label('key', 'value');
  message.epsagon.setError(Error('My custom error'));
  console.log(message);
});

bunnybus

Tracing bunnybus consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

// epsagon is added as an argument to the handler
handler: async ({message, metaData, ack, rej, requeue, epsagon}) => {
    epsagon.label('key', 'value');
    epsagon.setError(Error('My custom error'));
    await ack();
}

NATS

Tracing nats consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

HTTP Server

Tracing HTTP Server application can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces or setting custom errors can be by:

const server = http.createServer((req, res) => {
  epsagon.label('key', 'value');
  epsagon.setError(Error('My custom error'));
});

WS (Websocket)

Tracing ws consumers can be done in two methods: 1. Auto-tracing using the environment variable. 2. Calling the SDK.

Calling the SDK is simple, and should be done in your main js file where the consumer is being initialized:

const epsagon = require('epsagon-frameworks');

epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});

Tagging traces, setting custom errors/warnings or get current trace url can be by:

socket.on('message', (message, epsagonSdk) => {
    epsagonSdk.label('key', 'value');
    epsagonSdk.setError(Error('My custom error'));
    epsagonSdk.setWarning(Error('My custom warning'));
    console.log('Epsagon trace URL:', epsagonSdk.getTraceUrl())
}) 

Generic

For any tracing, you can simply use the generic Epsagon wrapper using the following example:

const epsagon = require('epsagon');
epsagon.init({
  token: 'epsagon-token',
  appName: 'app-name-stage',
  metadataOnly: false,
});


function main(params) {
  // Your code is here
}

const wrappedMain = epsagon.nodeWrapper(main);

Integrations

Epsagon provides out-of-the-box instrumentation (tracing) for many popular frameworks and libraries.

LibrarySupported Version
httpFully supported
httpsFully supported
http2Fully supported
dnsFully supported
aws-sdk>=2.2.0
amazon-dax-client>=1.0.2
@google-cloud>=2.0.0
@google-cloud/pubsub>=1.1.0
mysql>=2
mysql2>=1
pg>=4
mongodb>=2.2.12
kafkajs>=1.2.0
kafka-node>=3.0.0
amqplib>=0.5.0
amqp>=0.2.0
redis>=0.12.1
ioredis>=4.0.0
mqtt>=2.13.1
nats>=1.4.0
openwhisk>=3.0.0
@azure/cosmos>=3.7.5
@azure/storage-blob>=12.2.0
ldapjs>=2.1.0
ws>=7.3.1

Configuration

Advanced options can be configured as a parameter to the init() method or as environment variables.

ParameterEnvironment VariableTypeDefaultDescription
tokenEPSAGON_TOKENString-Epsagon account token
appNameEPSAGON_APP_NAMEStringApplicationApplication name that will be set for traces
metadataOnlyEPSAGON_METADATABooleantrueWhether to send only the metadata (true) or also the payloads (false)
useSSLEPSAGON_SSLBooleantrueWhether to send the traces over HTTPS SSL or not
traceCollectorURL-String-The address of the trace collector to send trace to
isEpsagonDisabledDISABLE_EPSAGONBooleanfalseA flag to completely disable Epsagon (can be used for tests or locally)
ignoredKeysEPSAGON_IGNORED_KEYSArray-Array of keys names (can be string or regex) to be removed from the trace
urlPatternsToIgnoreEPSAGON_URLS_TO_IGNOREArray[]Array of URL patterns to ignore the calls
sendTimeoutEPSAGON_SEND_TIMEOUT_SECFloat0.2The timeout duration in seconds to send the traces to the trace collector
decodeHTTPEPSAGON_DECODE_HTTPBooleantrueWhether to decode and decompress HTTP responses into the payload
httpErrorStatusCodeEPSAGON_HTTP_ERR_CODEInteger400The minimum number of an HTTP response status code to treat as an error
-EPSAGON_AUTO_ADD_NODE_PATHSBooleanfalseAuto add node_modules sub folders to look when patching libraries.
-DISABLE_EPSAGON_PATCHBooleanfalseDisable the library patching (instrumentation)
-EPSAGON_DEBUGBooleanfalseEnable debug prints for troubleshooting
-EPSAGON_PROPAGATE_NATS_IDBooleanfalseWhether to propagate a correlation ID in NATS.io calls for distributed tracing
-EPSAGON_ADD_NODE_PATHString-List of folders to looks for node_modules when patching libraries. Separated by :
-EPSAGON_DNS_INSTRUMENTATIONBooleanfalseWhether to capture dns calls into the trace
-EPSAGON_ALLOW_NO_ROUTEBooleanfalseWhether to capture non-matched route requests in Express.js
-EPSAGON_LOGGING_TRACING_ENABLEDBooleantruewhether to add an Epsagon ID to the logs in order to correlate traces to logs in the dashboard

Getting Help

If you have any issue around using the library or the product, please don't hesitate to:

  • Use the documentation.
  • Use the help widget inside the product.
  • Open an issue in GitHub.

Opening Issues

If you encounter a bug with the Epsagon library for Node.js, we want to hear about it.

When opening a new issue, please provide as much information about the environment:

  • Library version, Node.js runtime version, dependencies, etc.
  • Snippet of the usage.
  • A reproducible example can really help.

The GitHub issues are intended for bug reports and feature requests. For help and questions about Epsagon, use the help widget inside the product.

License

Provided under the MIT license. See LICENSE for details.

Copyright 2020, Epsagon

1.32.0

2 years ago

1.31.0

3 years ago

1.30.3

3 years ago

1.30.2

3 years ago

1.29.2

3 years ago

1.30.0

3 years ago

1.30.1

3 years ago

1.29.0

3 years ago

1.29.1

3 years ago

1.28.8

3 years ago

1.28.7

3 years ago

1.28.6

3 years ago

1.28.5

3 years ago

1.28.4

3 years ago

1.28.3

3 years ago

1.28.2

3 years ago

1.28.1

3 years ago

1.28.0

3 years ago

1.27.2

3 years ago

1.27.1

3 years ago

1.27.0

3 years ago

1.26.3

3 years ago

1.26.2

3 years ago

1.26.1

3 years ago

1.26.0

3 years ago

1.25.3

3 years ago

1.25.2

3 years ago

1.25.1

3 years ago

1.25.0

3 years ago

1.24.4

3 years ago

1.24.3

3 years ago

1.24.2

3 years ago

1.24.1

3 years ago

1.24.0

3 years ago

1.23.6

3 years ago

1.23.5

3 years ago

1.23.4

3 years ago

1.23.3

4 years ago

1.23.3-beta.3

4 years ago

1.23.3-beta.1

4 years ago

1.23.3-beta.0

4 years ago

1.23.2

4 years ago

1.23.1

4 years ago

1.23.0

4 years ago

1.22.2

4 years ago

1.22.1

4 years ago

1.22.0

4 years ago

1.21.0

4 years ago

1.20.2

4 years ago

1.20.3

4 years ago

1.20.1

4 years ago

1.20.0

4 years ago

1.19.2

4 years ago

1.19.1

4 years ago

1.18.1

4 years ago

1.19.0

4 years ago

1.18.0

4 years ago

1.17.6

4 years ago

1.17.5

4 years ago

1.17.4

4 years ago

1.17.2

4 years ago

1.17.3

4 years ago

1.17.1

4 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.15.2

4 years ago

1.15.1

4 years ago

1.15.0

4 years ago

1.14.1

4 years ago

1.14.0

4 years ago

1.13.2

4 years ago

1.13.1

4 years ago

1.13.0

4 years ago

1.12.0

4 years ago

1.11.2

4 years ago

1.11.1

4 years ago

1.11.0

4 years ago

1.10.1

4 years ago

1.10.0

4 years ago

1.9.0

4 years ago

1.8.2

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago