0.13.0 • Published 3 years ago

signalfx-tracing v0.13.0

Weekly downloads
4,505
License
BSD-3-Clause
Repository
github
Last release
3 years ago

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.

LibraryVersions supportedInstrumentation name(s)Notes
adonis4+use('adonis')
amqp103+use('amqp10')
amqplib0.5+use('amqplib')
Bluebird2+use('bluebird')
Bunyan1+use('bunyan')
cassandra-driver3+use('cassandra-driver')
DNSSupported Nodeuse('dns')
elasticsearch10+use('elasticsearch')
Express4+use('express')
GraphQL0.10+use('graphql')
hapi2+use('hapi')
http/httpsSupported Nodeuse('http'), use('https')
ioredis2+use('ioredis')
Koa2+use('koa')
Knex0.10+use('bluebird'); use('knex')Depends on bluebird instrumentation
Memcached2.2+use('memcached')
MongoDB-Core2+use('mongodb-core')
mysql2+use('mysql')
MySQL21+use('mysql2')
Nest4.x - 6.xuse('nest')
NetSupported Nodeuse('net')
node-postgres4+use('pg')
Pino2+use('pino')
Q1+use('q')
Redis0.12+use('redis')
restify3+use('restify')
Sails1+use('sails')
Socket.IO1.2+use('socket.io')
when.js3+use('when')
winston1+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.

ConfigEnvironment VariableDefaultDescription
serviceSIGNALFX_SERVICE_NAMEunnamed-nodejs-serviceThe service name to be used for this program.
urlSIGNALFX_ENDPOINT_URLhttp://localhost:9080/v1/traceThe url of the Agent or Gateway to which the tracer will submit traces.
accessTokenSIGNALFX_ACCESS_TOKENThe optional organization access token for trace submission requests
enabledSIGNALFX_TRACING_ENABLEDtrueWhether to enable the tracer.
debugSIGNALFX_TRACING_DEBUGfalseEnable debug logging in the tracer.
tagsSIGNALFX_SPAN_TAGS{}Set global tags that should be applied to all spans. Format for the environment variable is key1:val1,key2:val2.
logInjectionSIGNALFX_LOGS_INJECTIONfalseEnable automatic injection of trace IDs in logs for supported logging libraries.
logInjectionTagsSIGNALFX_LOGS_INJECTION_TAGS'environment'If log injection is on, these tags will be injected to log context.
flushInterval2000Interval in milliseconds at which the tracer will submit traces to the agent.
pluginstrueWhether or not to enable automatic instrumentation of external libraries using the built-in plugins.
SIGNALFX_ENABLED_PLUGINSA comma separated list of plugins that should be used. Only the plugins specified will be used and all other plugins will be ignore. All plugins will be used when no value is set.
recordedValueMaxLengthSIGNALFX_RECORDED_VALUE_MAX_LENGTH1200Maximum length an attribute value can have. Values longer than this limit are truncated. Any negative value turns off truncation.
enableServerTimingSIGNALFX_SERVER_TIMING_CONTEXTtrueEnable injection of Server-Timing response header containing the current trace (e.g. Server-Timing: traceparent;desc="00-traceId-spanId-01").

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:
      ```bash
        $ npm install signalfx-tracing
      ```
      GitHub:
      ```bash
        $ 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.

      ```javascript
      // 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>,
  service: <service name>,
  environment: <environment tag>
}

Inject trace information with Bunyan, Pino, or Winston

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

$ SIGNALFX_LOGS_INJECTION=true

Inject additional span tags into log context

If log injection is enabled, in addition to trace ID and span ID, span tags can be injected to logs. To select which tags to inject, add logInjectionTags to init config, by default only environment is injected:

const tracer = require('signalfx-tracing').init({
  tags: {environment: 'production', region: 'eu'},
  logInjectionTags: ['environment', 'region']
})

After which your logger will receive:

signalfx: {
  trace_id: <trace_id>,
  span_id: <span_id>,
  service: <service name>
  environment: 'production',
  region: 'eu'
}

Or set the same injected tag via an environment var (with environment vars taking precendence):

$ SIGNALFX_LOGS_INJECTION_TAGS=environment,region

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) {
            // inject trace and span ID into `record` object
            tracer.inject(span.context(), formats.LOG, record);
        }

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

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.