Skip to content

Latest commit

 

History

History

distributed-tracing

Sample distributed tracing application

This example provides both a BullMQ producer and consumer with a redis instance.

The producer starts a transaction, adds headers into the transaction and then adds those headers as part of the job data to be added to the queue. The producer and the new relic agent will shutdown after 10 seconds.

The consumer starts a transaction, processes the jobs from the queue and links the transaction from the producer by accepting its headers that were added as part of the job data.

Getting started

Note: This application requires the use of Node.js v20+ and docker.

  1. Clone or fork this repository.

  2. Setup the redis container

    docker compose up -d 
  3. Install dependencies and run application

    npm install
    cp env.sample .env
    # Fill out `NEW_RELIC_LICENSE_KEY` in .env and save 
    # Start the consumer
    npm run start:consumer
    # Start the producer in a different shell
    npm run start:producer

You can send more messages to the consumer by rerunning the producer with "npm run start:producer"

Exploring Telemetry

After the producer sends a few messages and the consumer processes them, navigate to your application in APM & Services. Select Distributed Tracing. Transactions will be created for the messages sent and processed. Since the consumer is running and handling message consumption, Distributed Tracing will link the two entities.

Producer distributed tracing Producer distributed trace

The producer service map shows two entities: the producer and consumer. Producer service map

You will see a distributed trace and a service map for the consumer as well.

Consumer distributed tracing

The consumer service map shows two entities (producer and consumer) and redis. Consumer service map

There are transactions created for every message consumption. Consumer Transactions

About insertDistributedTraceHeaders and acceptDistributedTraceHeaders

For context on how to use acceptDistributedTraceHeaders and insertDistributedTraceHeaders, first read Enable distributed tracing with agent APIs.

You can use insertDistributedTraceHeaders and acceptDistributedTraceHeaders to link different transactions together. In this example, one background transaction is linked to another background transaction.

insertDistributedTraceHeaders modifies the headers map that is passed in by adding W3C Trace Context headers and New Relic Distributed Trace headers. The New Relic headers can be disabled with distributed_tracing.exclude_newrelic_header: true in the config.

acceptDistributedTraceHeaders is used to instrument the called service for inclusion in a distributed trace. It links the spans in a trace by accepting a payload generated by insertDistributedTraceHeaders or generated by some other W3C Trace Context compliant tracer. This method accepts the headers of an incoming request, looks for W3C Trace Context headers, and if not found, falls back to New Relic distributed trace headers.