Node.js and AWS X-Ray (Node.js SDK)
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.
AWS X-Ray provides a complete view of requests as they travel through your application and filters visual data across payloads, functions, traces, services, APIs and more with no-code and low-code motions.
Node.js App with AWS X-Ray (Node.js SDK) and Tracetest​
This is a simple quick start guide on how to configure a Node.js app to use instrumentation with traces and Tracetest for enhancing your E2E and integration tests with trace-based testing. The infrastructure will use AWS X-Ray as the trace data store and a Node.js app to generate the telemetry data.
- 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.
AWS Account:
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 AWS X-Ray Node.js Quickstart:
git clone https://github.com/kubeshop/tracetest
cd tracetest/examples/tracetest-amazon-x-ray
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. - Fill out the AWS credentials in the
.env
file. You can create credentials by following this guide. - Run
docker compose -f ./docker-compose.agent.yaml up -d
. - This example is configured to use the AWS X-Ray Tracing Backend. Ensure the environment you're using to run this example is configured to use the AWS X-Ray Tracing Backend by clicking on Settings, Tracing Backend, AWS X-Ray, Save. Or, use the CLI as explained below.
- Run tests from the Tracetest Web UI by accessing the app with the URL
http://app:3000/http-request/
.
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, and a Node.js app.
The docker-compose.agent.yaml
file in the root directory of the quick start runs the Node.js app, X-Ray Daemon, and the Tracetest Agent setup.
Configuring the Node.js App​
The Node.js app is a simple Express app, contained in the index.js
file.
Configure the .env
like shown below.
AWS_ACCESS_KEY_ID="<YOUR_AWS_ACCESS_KEY_ID>"
AWS_SECRET_ACCESS_KEY="<YOUR_AWS_SECRET_ACCESS_KEY>"
AWS_SESSION_TOKEN="<YOURAWS_SESSION_TOKEN>"
AWS_REGION="<YOUR_AWS_REGION>"
TRACETEST_API_KEY="<YOUR_TRACETEST_API_KEY>"
TRACETEST_API_TOKEN="<YOUR_TRACETEST_TOKEN>"
The X-Ray tracing is contained in the index.js
file. Traces will be sent to AWS X-Ray via the X-Ray Daemon.
Configuring AWS X-Ray​
Configure AWS X-Ray as a Tracing Backend in Tracetest.
Get temporary credentials.
aws sts get-session-token
{
"Credentials": {
"AccessKeyId": "<yourkeyid>",
"SecretAccessKey": "<youraccesskey>",
"SessionToken": "<yoursessiontoken>",
"Expiration": "2024-05-23T21:01:38+00:00"
}
}
Edit the tracetest-tracing-backend.yaml
.
---
type: DataStore
spec:
name: awsxray
type: awsxray
awsxray:
accessKeyId: <your-accessKeyId>
secretAccessKey: <your-secretAccessKey>
sessionToken: <your-session-token>
region: "<your-region>"
tracetest config -t <YOUR_API_TOKEN>
tracetest apply datastore -f ./tracetest-tracing-backend.yaml
Run the Node.js App, X-Ray Daemon, and Tracetest Agent with Docker Compose​
The docker-compose.agent.yaml
file and Dockerfile
in the root directory contain the Node.js app.
The docker-compose.agent.yaml
file also contains the Tracetest Agent and X-Ray Daemon.
To start it, run this command:
docker compose -f ./docker-compose.agent.yaml up -d
This will start the Node.js app and Tracetest Agent.
Run Tracetest Tests​
- Open Tracetest.
- Configure AWS X-Ray as a tracing backend if you have not already as explained above.
- Start creating tests! Make sure to use the
http://app:3000/http-request/
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!
And a set of AWS credentials to connect Tracetest to the cloud API.
Project Structure​
The project is built with Docker Compose.
1. Node.js App​
The Dockerfile
in the root directory is for the Node.js app.
2. Tracetest​
The docker-compose.yaml
file, tracetest.provision.yaml
, and tracetest-config.yaml
in the root
directory are for the setting up the Node.js App, Tracetest and X-Ray Daemon.
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, xray-daemon:2000
in the src/index.js
will map to the xray-daemon
service, where the port 2000
is the port where the X-Ray Daemon accepts telemetry data.
Node.js App​
The Node.js app is a simple Express app, contained in the src/index.js
file.
It is instrumented using AWS X-Ray SDK sending the initial data to the Daemon that will be pushing the telemetry data to the AWS service.
The key following is the instrumentation section from the src/index.js
file.
const AWSXRay = require("aws-xray-sdk");
const XRayExpress = AWSXRay.express;
const express = require("express");
AWSXRay.setDaemonAddress("xray-daemon:2000");
// Capture all AWS clients we create
const AWS = AWSXRay.captureAWS(require("aws-sdk"));
AWS.config.update({
region: process.env.AWS_REGION || "us-west-2",
});
// Capture all outgoing https requests
AWSXRay.captureHTTPsGlobal(require("https"));
const https = require("https");
const app = express();
const port = 3000;
app.use(XRayExpress.openSegment("Tracetest"));
To start the server, run this command:
npm start
As you can see the Dockerfile
uses the command above.
FROM node:slim
WORKDIR /usr/src/app
COPY ./src/package*.json ./
RUN npm install
COPY ./src .
EXPOSE 3000
CMD [ "npm", "start" ]
Tracetest​
The docker-compose.yaml
includes three other services.
- Postgres - Postgres is a prerequisite for Tracetest to work. It stores trace data when running the trace-based tests.
- AWS X-Ray Daemon - is a software application that listens for traffic on UDP port 2000, gathers raw segment data, and relays it to the AWS X-Ray API. The daemon works in conjunction with the AWS X-Ray SDKs and must be running so that data sent by the SDKs can reach the X-Ray service.
- 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-config.yaml
target: /app/tracetest.yaml
- type: bind
source: ./tracetest.provision.yaml
target: /app/provisioning.yaml
ports:
- 11633:11633
command: --provisioning-file /app/provisioning.yaml
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
postgres:
condition: service_healthy
xray-daemon:
condition: service_started
healthcheck:
test: ["CMD", "wget", "--spider", "localhost:11633"]
interval: 1s
timeout: 3s
retries: 60
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
ports:
- 5432:5432
xray-daemon:
image: amazon/aws-xray-daemon:latest
environment:
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_SESSION_TOKEN: ${AWS_SESSION_TOKEN}
AWS_REGION: ${AWS_REGION}
ports:
- 2000:2000
Tracetest depends on Postgres and the x-ray daemon. Tracetest requires config files to be loaded via a volume. The volumes are mapped from the root directory into the root
directory and the respective config files.
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
The tracetest.provision.yaml
file defines the trace data store, set to AWS X-Ray, meaning the traces will be stored in X-Ray and Tracetest will fetch them from X-Ray when running tests.
But how does Tracetest fetch traces?
Tracetest uses the Golang AWS-SDK library to pull to fetch trace data.
---
type: DataStore
spec:
name: awsxray
type: awsxray
awsxray:
accessKeyId: <your-accessKeyId>
secretAccessKey: <your-secretAccessKey>
sessionToken: <your-session-token>
region: "us-west-2"
How do traces reach AWX X-Ray?
The application code in the src/index.js
file uses the native AWS SDK X-Ray library which sends telemetry data to the X-Ray Daemon to be processed and then sent to the configured AWS X-Ray SaaS.
Run Both the Node.js App and Tracetest​
To start both the Node.js app and Tracetest, run this command:
docker-compose up
This will start your Tracetest instance on http://localhost:11633/
. Open it and start creating tests!
Make sure to use the http://app:3000/
URL in your test creation because your Node.js app and Tracetest are in the same network.
Learn More​
Please visit our examples in GitHub and join our Slack Community for more info!