An Interest In:
Web News this Week
- February 28, 2024
- February 27, 2024
- February 26, 2024
- February 25, 2024
- February 24, 2024
- February 23, 2024
- February 22, 2024
Instrumenting Your Node.js Apps with OpenTelemetry
As systems become increasingly complex, its increasingly important to get visibility into the inner workings of systems to increase performance and reliability. Distributed tracing shows how each request passes through the application, giving developers context to resolve incidents, showing what parts of their system are slow or broken.
A single trace shows the path a request makes, from the browser or mobile device down to the database. By looking at traces as a whole, developers can quickly discover which parts of their application is having the biggest impact on performance as it affects your users experiences.
Thats pretty abstract, right? So lets zero in on a specific example to help clarify things. Well use OpenTelemetry to generate and view traces from a small sample application.
Spinning up our Movies App
We have written a simple application consisting of two microservices, movies and dashboard. The movies
service provides the name of movies and their genre in JSON format, while the dashboard
service returns the results from the movies service.
To spin up the app, run
$ npm i$ node dashboard.js$ node movies.js
Notice the variable delay
, built into the movies microservice that causes random delays returning the JSON.
const express = require('express')const app = express()const port = 3000app.get('/movies', async function (req, res) { res.type('json')+ var delay = Math.floor( ( Math.random() * 2000 ) + 100);+ setTimeout((() => { res.send(({movies: [ { name: 'Jaws', genre: 'Thriller'}, { name: 'Annie', genre: 'Family'}, { name: 'Jurassic Park', genre: 'Action'}, ]}))+ }), delay)})
Tracing HTTP Requests with Open Telemetry
OpenTelemetry traces incoming and outgoing HTTP requests by attaching IDs. To do this, we need to
- Instantiate a trace provider to get data flowing.
- Configure that trace provider with an exporter to send telemetry data to another system where you can view, store, and analyze it.
- Install OpenTelemetry plugins to instrument specific node module(s) to automatically instrument various frameworks
Step 1: Create our trace provider and configuring it with an exporter
Well start by creating our trace provider and configuring it with an exporter. To do this, well need to install
$ npm install @opentelemetry/node
OpenTelemetry auto instrumentation package for NodeJS
The @opentelemetry/node
module provides auto-instrumentation for Node.js applications, which automatically identifies frameworks (Express), common protocols (HTTP), databases, and other libraries within your application. This module uses other community-contributed plugins to automatically instrument your application to automatically produce spans and provide end-to-end tracing with just a few lines of code.
OpenTelemetry Plugins
$ npm install @opentelemetry/plugin-http$ npm install @opentelemetry/plugin-express
The @opentelemetry/plugin-http
plugin generates trace data from NodeJSs underlying HTTP handling APIs that both send, and handle requests. The @opentelemetry/plugin-express
plugin generates trace data from requests sent through the express framework.
Step 2: Adding the Trace Provider and the Span Processor
Add this code snippet to
- create a trace provider
- adds a span processor to the trace provider
This code gets data out of your local application and exports into your console!
const { NodeTracerProvider } = require('@opentelemetry/node');const { ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/tracing');const provider = new NodeTracerProvider();const consoleExporter = new ConsoleSpanExporter();const spanProcessor = new SimpleSpanProcessor(consoleExporter);provider.addSpanProcessor(spanProcessor);provider.register()
Once we add this code snippet, whenever we reload http://localhost:3001/dashboard
, we should get something like this - beautiful things on the console.
Step 3a: Spinning up Zipkin
Let's spin up a Zipkin instance with the Docker Hub Image
$ docker run -d -p 9411:9411 openzipkin/zipkin
and youll have a Zipkin instance up and running. Youll be able to load it by pointing your web browser to http://localhost:9411. Youll see something like this
Step 3: Exporting to Zipkin
While its neat, spans in a terminal window are a poor way to have visibility into a service. In our code above, the following lines are what added a console exporter to our system. Let's now ship this data to Zipkin.
In this code snippet, we are instantiating a Zipkin exporter, and then adding it to the trace provider. The great thing about OpenTelemetry is that it's backend agnostic, meaning you can have as many different exporters configured as you like,
const { NodeTracerProvider } = require('@opentelemetry/node')const { ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/tracing')+ const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin')const provider = new NodeTracerProvider()const consoleExporter = new ConsoleSpanExporter()const spanProcessor = new SimpleSpanProcessor(consoleExporter)provider.addSpanProcessor(spanProcessor)provider.register()+ const zipkinExporter = new ZipkinExporter({+ url: 'http://localhost:9411/api/v2/spans',+ serviceName: 'movies-service'})+ const zipkinProcessor = new SimpleSpanProcessor(zipkinExporter)+ provider.addSpanProcessor(zipkinProcessor)
After you make these changes, let's visit our Zipkin instance at localhost:9411, start our application back up and request some URLs.
Step 4: Using the OpenTelemetry Collector to export the data into New Relic
What happens if we want to send the OpenTelemetry data to another backend where you didn't have to manage all of your own telemetry data?
Well, the amazing contributors to OpenTelemetry have come up with a solution to fix this!
The OpenTelemetry Collector is a way for developers to receive, process and export telemetry data to multiple backends. It supports multiple open-source observability data formats like Zipkin, Jaeger, Prometheus, Fluent Bit sending it to one or more open-source or commercial back-ends.
New Relic
New Relic is a platform for you to analyze, store, and use your telemetry data for Free, forever. Sign up now!
Configuring the OpenTelemetry Collector with New Relic
Clone the OpenTelemetry Collector with New Relic Exporter and spin up the docker container, making sure to export the New Relic API key.
export NEW_RELIC_API_KEY=<INSERT-API-KEY-HERE>docker-compose -f docker-compose.yaml up
Make sure to change the reporting URL from
http://localhost:9411/api/v2/spans
tohttp://localhost:9411/
in bothdashboard.js
andmovies.js
const zipkinExporter = new ZipkinExporter({- url: 'http://localhost:9411/api/v2/spans',+ url: 'http://localhost:9411', serviceName: 'movies-service'})
Step 5: Look at your beautiful data
Navigate to the "Explorer" tab on New Relic One.
When you click on the service, you should be able to see some beautiful traces!
Final Thoughts
Instrumenting your app with Open Telemetry makes it easy to figure out what is going wrong when parts of your application is slow, broken, or both. With the collector, you can forward your data anywhere, so you are never locked into a vendor. You can choose to spin up an open source backend, use a proprietary backend like New Relic, or just roll your own backend! Whatever you choose, I wish you well you in journey to instrument EVERYTHING!
Original Link: https://dev.to/newrelic/instrumenting-your-node-js-apps-with-opentelemetry-5flb
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To