autotelic / fastify-opentelemetry

A Fastify plugin that utilizes the OpenTelemetry API to provide request tracing.
MIT License
76 stars 12 forks source link

Fastify OpenTelemetry

A Fastify plugin that uses the OpenTelemetry API to provide request tracing.

Usage

npm i @autotelic/fastify-opentelemetry

Examples

Plugin Usage

// index.js
// Load your OpenTelemetry/API configuration first. Now the configured SDK will be available
// to fastify-opentelemetry. (See the example configuration below.)
require('./openTelemetryConfig')
const openTelemetryPlugin = require('@autotelic/fastify-opentelemetry')
const fastify = require('fastify')();

(async () => {
  await fastify.register(openTelemetryPlugin, { wrapRoutes: true })

  fastify.get('/', async function (request, reply) {
    const {
      activeSpan,
      tracer,
      // context,
      // extract,
      // inject,
    } = request.openTelemetry()
    // Spans started in a wrapped route will automatically be children of the activeSpan.
    const childSpan = tracer.startSpan(`${activeSpan.name} - child process`)
    // doSomeWork()
    childSpan.end()
    return 'OK'
  })

  fastify.listen(3000, (err, address) => {
    if (err) {
      fastify.log.error(err)
      process.exit(1)
    }
  })
})()

OpenTelemetry API Configuration

// openTelemetryConfig.js
const {
  BatchSpanProcessor,
  ConsoleSpanExporter,
  TraceIdRatioBasedSampler
} = require('@opentelemetry/sdk-trace-base')

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node')

// Configure a tracer provider.
const provider = new NodeTracerProvider({
  sampler: new TraceIdRatioBasedSampler(0.5)
})

// Add a span exporter.
provider.addSpanProcessor(
  new BatchSpanProcessor(new ConsoleSpanExporter())
)

// Register a global tracer provider.
provider.register()

// Note: the above is just a basic example. fastify-opentelemetry is compatible with any
// @opentelemetry/api configuration.

See /example for a working example app. To run the example app locally, first clone this repo and then npm i and run:

npm run dev

API

Configuration

This plugin leaves all tracer configuration to the OpenTelemetry API. The tracer and propagation method are pulled in from the global tracer provider and global propagator, respectively. This allows the config for the plugin itself to be minimal.

The plugin accepts the the following configuration properties:

Request Decorator

This plugin decorates the request with an openTelemetry function that returns an object with the following properties:

Hooks

This plugin registers the following Fastify hooks:

OpenTelemetry Compatibility

As the OpenTelemetry API uses a variable on the global object to store the global API, care needs to be taken to ensure all modules are compatible.

Each version of the OpenTelemetry API will require a specific release of fastify-opentelemetry.

OpenTelemetry API Version Minimum Fastify OpenTelemetry Version
@opentelemetry/api@1.0.0 @autotelic/fastify-opentelemetry@0.14.0
@opentelemetry/api@0.20.0 @autotelic/fastify-opentelemetry@0.13.0
@opentelemetry/api@1.0.0-rc.0 @autotelic/fastify-opentelemetry@0.12.0
@opentelemetry/api@0.18.0 @autotelic/fastify-opentelemetry@0.10.0
@opentelemetry/api@0.17.0 @autotelic/fastify-opentelemetry@0.9.0
@opentelemetry/api@0.15.0 @autotelic/fastify-opentelemetry@0.8.0
@opentelemetry/api@0.14.0 @autotelic/fastify-opentelemetry@0.7.0
@opentelemetry/api@0.13.0 @autotelic/fastify-opentelemetry@0.5.0
@opentelemetry/api@0.12.0 @autotelic/fastify-opentelemetry@0.4.0
@opentelemetry/api@0.10.0 @autotelic/fastify-opentelemetry@0.2.4
@opentelemetry/api@0.9.0 @autotelic/fastify-opentelemetry@0.1.1