Node.js and Jaeger
Tracetest is a testing tool based on OpenTelemetry that allows you to test your distributed application. It allows you to use data from distributed traces generated by OpenTelemetry to validate and assert if your application has the desired behavior defined by your test definitions.
Jaeger is an open-source, end-to-end distributed tracing solution. It allows you to monitor and troubleshoot transactions in complex distributed systems. It was developed and then open sourced by Uber Technologies. Jaeger provides a distributed tracing solution to enable transactions across multiple heterogeneous systems or microservices to be tracked and displayed as a cascading series of spans.
Node.js App with Jaeger, OpenTelemetry and Tracetest​
This is a simple quick start on how to configure a Node.js app to use OpenTelemetry instrumentation with traces and Tracetest for enhancing your E2E and integration tests with trace-based testing. The infrastructure will use Jaeger as the trace data store, and OpenTelemetry Collector to receive traces from the Node.js app and send them to Jaeger.
- Cloud-based Managed Tracetest
- Hobby Open-Source Tracetest Core
Prerequisites​
Tracetest Account:
- Sign up to
app.tracetest.io
or follow the get started docs. - Create an environment.
- Create an environment token.
- Have access to the environment's agent API key.
Docker: Have Docker and Docker Compose installed on your machine.
Run This Quckstart Example​
The example below is provided as part of the Tracetest project. You can download and run the example by following these steps:
Clone the Tracetest project and go to the Jaeger Node.js Quickstart:
git clone https://github.com/kubeshop/tracetest
cd tracetest/examples/quick-start-jaeger-nodejs
Follow these instructions to run the quick start:
- Copy the
.env.template
file to.env
. - Log into the Tracetest app.
- Fill out the token and API key details by editing your
.env
file. You can find these values in the Settings area for your environment. - Run
docker compose -f ./docker-compose.yaml -f ./docker-compose.agent.yaml up -d
. - This example is configured to use the Jaeger Tracing Backend. Ensure the environment you're using to run this example is configured to use the Jaeger Tracing Backend by clicking on Settings, Tracing Backend, Jaeger, Save. Or, use the CLI as explained below.
- Run tests from the Tracetest Web UI by accessing the app with the URL
http://app:8080/
.
Follow along with the sections below for an in detail breakdown of what the example you just ran did and how it works.
Project Structure​
The project contains Tracetest Agent, OpenTelemetry Collector, and a Node.js app.
The docker-compose.yaml
file in the root directory of the quick start runs the Node.js app and the Tracetest Agent setup.
Configuring the Node.js App​
The Node.js app is a simple Express app, contained in the app.js
file.
Configure the .env
like shown below.
TRACETEST_API_KEY="<YOUR_TRACETEST_API_KEY>"
The OpenTelemetry tracing is contained in the tracing.otel.grpc.js
or tracing.otel.http.js
files. Traces will be sent to Tracetest Agent.
Choosing the tracing.otel.grpc.js
file will send traces to OpenTelemetry Collector's GRPC
endpoint.
Enabling the tracer is done by preloading the trace file. As seen in the package.json
.
"scripts": {
"app-with-grpc-tracer": "node -r ./tracing.otel.grpc.js app.js",
},
Configuring Jaeger​
Configure Jaeger as a Tracing Backend:
---
type: DataStore
spec:
name: Jaeger
type: jaeger
default: true
jaeger:
endpoint: jaeger:16685
tls:
insecure: true
tracetest config -t <YOUR_API_TOKEN>
tracetest apply datastore -f ./tracetest-tracing-backend.yaml
Run the Node.js App, Jaeger and OpenTelemetry Collector with Docker Compose​
The docker-compose.yaml
file and Dockerfile
in the root directory are for the Node.js app. The docker-compose.yaml
contains one service for the Node.js app.
The docker-compose.agent.yaml
file is for the Tracetest Agent, Jaeger, and OpenTelemetry Collector.
The collector.config.yaml
configures the OpenTelemetry Collector. It receives traces via either grpc
or http
. Then, exports them to Jaeger via the OTLP exporter
.
To start it, run this command:
docker compose -f ./docker-compose.yaml -f ./docker-compose.agent.yaml up -d
This will start the Node.js app the OpenTelemetry Collector and send the traces to Jaeger.
Run Tracetest Tests​
- Open Tracetest
- Configure Jaeger as a tracing backend if you have not already as explained above.
- Start creating tests! Make sure to use the
http://app:8080/
URL in your test creation. - To trigger tests in the CLI, first install the CLI, configure it, and run a test. From the root of the quick start directory, run:
tracetest configure -t <YOUR_API_TOKEN>
tracetest run test -f ./test-api.yaml
Prerequisites​
You will need Docker and Docker Compose installed on your machine to run this quick start app!
Project Structure​
The project is built with Docker Compose. It contains two distinct docker-compose.yaml
files.
1. Node.js App​
The docker-compose.yaml
file and Dockerfile
in the root directory are for the Node.js app.
2. Tracetest​
The docker-compose.yaml
file, collector.config.yaml
, tracetest-provision.yaml
, and tracetest-config.yaml
in the tracetest
directory are for the setting up Tracetest, Jaeger, and the OpenTelemetry Collector.
The tracetest
directory is self-contained and will run all the prerequisites for enabling OpenTelemetry traces and trace-based testing with Tracetest.
Docker Compose Network​
All services
in the docker-compose.yaml
are on the same network and will be reachable by hostname from within other services. For example, jaeger:4317
in the collector.config.yaml
will map to the jaeger
service, where the port 4317
is the port where Jaeger accepts traces. And, jaeger:16685
in the tracetest-provision.yaml
will map to the jaeger
service and port 16685
where Tracetest will fetch trace data from Jaeger.
Node.js App​
The Node.js app is a simple Express app contained in the app.js
file.
The OpenTelemetry tracing is contained in the tracing.otel.grpc.js
or tracing.otel.http.js
files.
Traces will be sent to the OpenTelemetry Collector.
Here's the content of the tracing.otel.grpc.js
file:
const opentelemetry = require("@opentelemetry/sdk-node");
const {
getNodeAutoInstrumentations,
} = require("@opentelemetry/auto-instrumentations-node");
const {
OTLPTraceExporter,
} = require("@opentelemetry/exporter-trace-otlp-grpc");
const sdk = new opentelemetry.NodeSDK({
traceExporter: new OTLPTraceExporter({ url: "http://otel-collector:4317" }),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
Depending on which of these you choose, traces will be sent to either the grpc
or http
endpoint.
The hostnames and ports for these are:
- GRPC:
http://otel-collector:4317
- HTTP:
http://otel-collector:4318/v1/traces
Enabling the tracer is done by preloading the trace file.
node -r ./tracing.otel.grpc.js app.js
In the package.json
you will see two npm scripts for running the respective tracers alongside the app.js
.
"scripts": {
"with-grpc-tracer":"node -r ./tracing.otel.grpc.js app.js",
"with-http-tracer":"node -r ./tracing.otel.http.js app.js"
},
To start the server, run this command:
npm run with-grpc-tracer
# or
npm run with-http-tracer
As you can see the Dockerfile
uses the command above.
FROM node:slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "with-grpc-tracer" ]
And, the docker-compose.yaml
contains just one service for the Node.js app.
version: "3"
services:
app:
image: quick-start-nodejs
build: .
ports:
- "8080:8080"
To start it, run this command:
docker compose build # optional if you haven't already built the image
docker compose up
This will start the Node.js app. But, you're not sending the traces anywhere.
Let's fix this by configuring Tracetest and OpenTelemetry Collector.
Tracetest​
The docker-compose.yaml
in the tracetest
directory is configured with four services.
- Postgres - Postgres is a prerequisite for Tracetest to work. It stores trace data when running the trace-based tests.
- OpenTelemetry Collector - A vendor-agnostic implementation of how to receive, process and export telemetry data.
- Jaeger - A trace data store.
- Tracetest - Trace-based testing that generates end-to-end tests automatically from traces.
version: "3"
services:
tracetest:
image: kubeshop/tracetest:${TAG:-latest}
platform: linux/amd64
volumes:
- type: bind
source: ./tracetest/tracetest.config.yaml
target: /app/tracetest.yaml
- type: bind
source: tracetest/tracetest-provision.yaml
target: /app/provision.yaml
command: --provisioning-file /app/provision.yaml
ports:
- 11633:11633
depends_on:
postgres:
condition: service_healthy
jaeger:
condition: service_started
otel-collector:
condition: service_started
healthcheck:
test: ["CMD", "wget", "--spider", "localhost:11633"]
interval: 1s
timeout: 3s
retries: 60
environment:
TRACETEST_DEV: ${TRACETEST_DEV}
postgres:
image: postgres:14
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
healthcheck:
test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
interval: 1s
timeout: 5s
retries: 60
otel-collector:
image: otel/opentelemetry-collector-contrib:0.59.0
command:
- "--config"
- "/otel-local-config.yaml"
volumes:
- ./tracetest/collector.config.yaml:/otel-local-config.yaml
jaeger:
image: jaegertracing/all-in-one:latest
restart: unless-stopped
ports:
- "16686:16686"
healthcheck:
test: ["CMD", "wget", "--spider", "localhost:16686"]
interval: 1s
timeout: 3s
retries: 60
Tracetest depends on Postgres, Jaeger and the OpenTelemetry Collector. Both Tracetest and the OpenTelemetry Collector require config files to be loaded via a volume. The volumes are mapped from the root directory into the tracetest
directory and the respective config files.
To start both the Node.js app and Tracetest we will run this command:
docker-compose -f docker-compose.yaml -f tracetest/docker-compose.yaml up # add --build if the images are not built already
The tracetest-config.yaml
file contains the basic setup of connecting Tracetest to the Postgres instance.
postgres:
host: postgres
user: postgres
password: postgres
port: 5432
dbname: postgres
params: sslmode=disable
telemetry:
exporters:
collector:
serviceName: tracetest
sampling: 100 # 100%
exporter:
type: collector
collector:
endpoint: otel-collector:4317
server:
telemetry:
exporter: collector
The tracetest.provision.yaml
file defines the trace data store, set to Jaeger, meaning the traces will be stored in Jaeger and Tracetest will fetch them from Jaeger when running tests.
But how does Tracetest fetch traces?
Tracetest uses jaeger:16685
to connect to Jaeger and fetch trace data.
---
type: PollingProfile
spec:
name: Default
strategy: periodic
default: true
periodic:
retryDelay: 5s
timeout: 10m
---
type: DataStore
spec:
name: Jaeger
type: jaeger
default: true
jaeger:
endpoint: jaeger:16685
tls:
insecure: true
How do traces reach Jaeger?
The collector.config.yaml
explains that. It receives traces via either grpc
or http
. Then, exports them to Jaegers's OTLP endpoint jaeger:4317
.
receivers:
otlp:
protocols:
grpc:
http:
processors:
batch:
timeout: 100ms
exporters:
logging:
loglevel: debug
otlp:
endpoint: jaeger:4317
tls:
insecure: true
service:
pipelines:
traces/1:
receivers: [otlp]
processors: [batch]
exporters: [logging, otlp]
Run Both the Node.js App and Tracetest​
To start both the Node.js app and Tracetest, we will run this command:
docker-compose -f docker-compose.yaml -f tracetest/docker-compose.yaml up # add --build if the images are not built already
This will start your Tracetest instance on http://localhost:11633/
.
Open the URL and start creating tests! Make sure to use the http://app:8080/
URL in your test creation, because your Node.js app and Tracetest are in the same network.
Learn More​
Feel free to check out our examples in GitHub and join our Slack Community for more info!