Skip to content

romansavchenko/signalfx-nodejs-tracing

 
 

Repository files navigation

SignalFx Tracing Library for JavaScript

npm (tag) CircleCI

The SignalFx Tracing Library for JavaScript automatically instruments your Node.js application to capture and report distributed traces to SignalFx with an OpenTracing-compatible tracer.

The tracer has constant sampling (i.e., 100% chance of tracing) and reports every span. Where applicable, context propagation uses B3 headers.

For more information about configuring and using the agent, see the examples.

For advanced configuration information, see SignalFx Tracing Library for JavaScript - API.

Requirements and supported software

The library supports Node.js versions 4.7+, 6.9+, and 8+.

These are the supported libraries. Instrumentation for each library is in beta.

Library Versions supported Instrumentation name(s) Notes
adonis 4+ use('adonis')
amqp10 3+ use('amqp10')
amqplib 0.5+ use('amqplib')
Bluebird 2+ use('bluebird')
Bunyan 1+ use('bunyan')
cassandra-driver 3+ use('cassandra-driver')
DNS Supported Node use('dns')
elasticsearch 10+ use('elasticsearch')
Express 4+ use('express')
GraphQL 0.10+ use('graphql')
hapi 2+ use('hapi')
http/https Supported Node use('http'), use('https')
ioredis 2+ use('ioredis')
Koa 2+ use('koa')
Knex 0.10+ use('bluebird'); use('knex') Depends on bluebird instrumentation
Memcached 2.2+ use('memcached')
MongoDB-Core 2+ use('mongodb-core')
mysql 2+ use('mysql')
MySQL2 1+ use('mysql2')
Nest 4.x - 6.x use('nest')
Net Supported Node use('net')
node-postgres 4+ use('pg')
Pino 2+ use('pino')
Q 1+ use('q')
Redis 0.12+ use('redis')
restify 3+ use('restify')
Sails 1+ use('sails')
Socket.IO 1.2+ use('socket.io')
when.js 3+ use('when')
winston 1+ use('winston')

Configure the SignalFx Tracing Library for JavaScript

Send traces from your Java application to a local or remote Smart Agent, OpenTelemetry Collector, or SignalFx ingest endpoint.

Configuration values

Configure these options as parameters for the init() method or as environment variables.

Config Environment Variable Default Description
service SIGNALFX_SERVICE_NAME unnamed-nodejs-service The service name to be used for this program.
url SIGNALFX_ENDPOINT_URL http://localhost:9080/v1/trace The url of the Agent or Gateway to which the tracer will submit traces.
accessToken SIGNALFX_ACCESS_TOKEN The optional organization access token for trace submission requests
enabled SIGNALFX_TRACING_ENABLED true Whether to enable the tracer.
debug SIGNALFX_TRACING_DEBUG false Enable debug logging in the tracer.
logInjection SIGNALFX_LOGS_INJECTION false Enable automatic injection of trace IDs in logs for supported logging libraries.
tags SIGNALFX_SPAN_TAGS {} Set global tags that should be applied to all spans. Format for the environment variable is key1:val1,key2:val2.
flushInterval 2000 Interval in milliseconds at which the tracer will submit traces to the agent.
plugins true Whether or not to enable automatic instrumentation of external libraries using the built-in plugins.
recordedValueMaxLength SIGNALFX_RECORDED_VALUE_MAX_LENGTH 1200 Maximum length an attribute value can have. Values longer than this limit are truncated. Any negative value turns off truncation.

Steps

To set up the library, install it and add the OpenTracing-compatible tracer to your application.

  1. Install the latest release of the tracing library. You can install directly from npm or directly from the GitHub repository.

    npm:

      $ npm install signalfx-tracing

    GitHub:

      $ git clone https://github.com/signalfx/signalfx-nodejs-tracing.git
      $ npm install ./signalfx-nodejs-tracing
  2. Set the service name for your application if not set via tracer configuration code:

      $ SIGNALFX_SERVICE_NAME=your_app_name node your_app.js
  3. Configure the OpenTracing-compatible tracer to report traces to a Smart Agent or OpenTelemetry Collector. You have to include this before you import the target library.

    // init() invocation must occur before importing any traced library (e.g. Express)
    const tracer = require('signalfx-tracing').init({
      // Service name, also configurable via
      // SIGNALFX_SERVICE_NAME environment variable
      service: 'my-traced-service',
      // Smart Agent or Gateway endpoint, also configurable via
      // SIGNALFX_ENDPOINT_URL environment variable
      url: 'http://my_agent_or_gateway:9080/v1/trace', // http://localhost:9080/v1/trace by default
      // Optional organization access token, also configurable via
      // SIGNALFX_ACCESS_TOKEN environment variable
      accessToken: 'myOptionalOrganizationAccessToken',
      // Optional environment tag
      tags: {environment: 'myEnvironment'}
    })
    
    // auto-instrumented example Express application
    const express = require('express')
    const app = express()

Inject trace IDs in logs

Link individual log entries with trace IDs and span IDs associated with corresponding events. Inject trace context in logs with these loggers:

  • Bunyan
  • Pino
  • Winston

You can also enable trace ID log injection with a custom logger.

When you configure trace ID log injection, your logger receives this info for the span.context:

signalfx: {
  trace_id: <trace_id>,
  span_id: <span_id>      
}

Inject trace IDs with Bunyan, Pino, or Winston

To transfer trace context to logs with Bunyan, Pino, or Winston, enable trace ID log injection with this environment variable:

$ SIGNALFX_LOGS_INJECTION=true

Inject trace IDs with a custom logger

To transfer trace context with a custom logger, add tracer.inject to your custom logger class like this:

const tracer = require('signalfx-tracing').init()
const formats = require('signalfx-tracing/ext/formats');

class Logger {
    log(level, message) {
        const span = tracer.scope().active();
        const time = new Date().toISOString();
        const record = { time, level, message };

        if (span) {
            tracer.inject(span.context(), formats.LOG, yourRecordObject);
        }

        console.log(JSON.stringify(yourRecordObject));
    }
}

module.exports = Logger;

where yourRecordObject is the object you want to inject span.context in.

License and versioning

The SignalFx Tracing Library for JavaScript is released under the terms of the BSD 3-Clause License. See the the license file for more details.

The SignalFx-Tracing Library for JavaScript is a fork of the DataDog APM JavaScript Tracer that has been modified to provide Zipkin v2 JSON formatting, B3 trace propagation functionality, and properly annotated trace data for handling by SignalFx Microservices APM.

About

OpenTracing-compatible Auto-Instrumentor for Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 95.4%
  • C++ 4.5%
  • Python 0.1%