Node.js and Azure Application Insights (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.
Azure Application Insights is an extension of Azure Monitor and provides application performance monitoring (APM) features. APM tools are useful to monitor applications from development, through test, and into production in the following ways:
- Proactively understand how an application is performing.
- Reactively review application execution data to determine the cause of an incident.
Node.js App with Azure Application Insights 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 Azure App Insights 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.
Azure Account:
- Sign up to Azure.
- Install Azure CLI.
- Create an Application Insights app and get a Connection String.
- Get the Azure Resource ARM id for the Application Insights instance and generate an access token.
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 Azure Node.js Quickstart:
git clone https://github.com/kubeshop/tracetest
cd tracetest/examples/tracetest-azure-app-insights
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 up -d
. - Fill out the
resourceArmId: <your-arm-id>
andaccessToken: <your-access-token>
in thetracetest-tracing-backend.yaml
. - This example is configured to use the Azure Application Insights Tracing Backend. Ensure the environment you're using to run this example is configured to use the Application Insights Tracing Backend by clicking on Settings, Tracing Backend, Azure Application Insights, 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, and a Node.js app.
The docker-compose.yaml
file in the root directory of the quick start runs the Node.js app, Elasticsearch, Elastic APM server, OpenTelemetry Collector, 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.
CONNECTION_STRING="<YOUR_APP_INSIGHTS_CONNECTION_STRING>"
TRACETEST_API_KEY="<YOUR_TRACETEST_API_KEY>"
The OpenTelemetry tracing is contained in the tracing.js
file. Traces will be sent to Azure Application Insights.
The tracing.js
file will send traces to the Azure Application Insights endpoint.
Enabling the tracer is done by preloading the trace file. As seen in the package.json
.
"scripts": {
"start": "node -r ./tracing.js app.js"
},
Configuring Azure Application Insights​
Create an Application Insights resource in your Azure account. The Resource ID is the resourceArmId
you need.
Get the accessToken
by following this guide. Or if you are running Tracetest from a authorized container, you can use the default Active Directory Authentication.
Configure Azure Application Insights as a Tracing Backend in Tracetest:
---
type: DataStore
spec:
name: azureappinsights
type: azureappinsights
azureappinsights:
connectionType: direct
resourceArmId: <your-arm-id>
accessToken: <your-access-token>
useAzureActiveDirectoryAuth: false
tracetest config -t <YOUR_API_TOKEN>
tracetest apply datastore -f ./tracetest-tracing-backend.yaml
Run the Node.js App and OpenTelemetry Collector with Docker Compose​
The docker-compose.yaml
file and Dockerfile
in the root directory contain the Node.js app.
The docker-compose.yaml
file also contains the Tracetest Agent.
To start it, run this command:
docker compose up -d
This will start the Node.js app and Tracetest Agent.
Run Tracetest Tests​
- Open Tracetest.
- Configure Azure Application Insights 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!
You will also need an App Insights API Access Key and the resource ARM ID for your App Insights instance.
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 and 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.
Node.js App​
The Node.js app is a simple Express app, contained in the src/index.js
file.
It is instrumented using the applicationinsights SDK wrapping the application code to send telemetry data directly to the Azure cloud.
The following is the code instrumentation section from the src/tracing.js
file.
const {
ApplicationInsightsClient,
ApplicationInsightsConfig,
} = require("applicationinsights");
const {
ExpressInstrumentation,
} = require("@opentelemetry/instrumentation-express");
const { HttpInstrumentation } = require("@opentelemetry/instrumentation-http");
const config = new ApplicationInsightsConfig();
config.azureMonitorExporterConfig.connectionString = process.env.CONNECTION_STRING;
const appInsights = new ApplicationInsightsClient(config);
const traceHandler = appInsights.getTraceHandler();
traceHandler.addInstrumentation(new ExpressInstrumentation());
traceHandler.addInstrumentation(new HttpInstrumentation());
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 two other services.
- Postgres - Postgres is a prerequisite for Tracetest to work. It stores trace data when running the trace-based tests.
- Tracetest - Trace-based testing that generates end-to-end tests automatically from traces.
services:
postgres:
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
healthcheck:
test:
- CMD-SHELL
- pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
timeout: 5s
interval: 1s
retries: 60
image: postgres:14
networks:
default: null
tracetest:
command: --provisioning-file /app/provision.yaml
platform: linux/amd64
depends_on:
postgres:
condition: service_healthy
environment:
TRACETEST_DEV: ${TRACETEST_DEV}
extra_hosts:
host.docker.internal: host-gateway
healthcheck:
test:
- CMD
- wget
- --spider
- localhost:11633
timeout: 3s
interval: 1s
retries: 60
image: kubeshop/tracetest:${TAG:-latest}
networks:
default: null
ports:
- mode: ingress
target: 11633
published: 11633
protocol: tcp
volumes:
- type: bind
source: tracetest/tracetest.yaml
target: /app/tracetest.yaml
- type: bind
source: tracetest/tracetest-provision.yaml
target: /app/provision.yaml
networks:
default:
name: _default
Tracetest depends on Postgres and 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 Azure App Insights, meaning the traces will be stored in App Insights and Tracetest will fetch them from when running tests.
But how does Tracetest fetch traces?
Tracetest uses the Golang Azure SDK library to pull to fetch trace data.
type: DataStore
spec:
name: AzureAppInsights
type: azureappinsights
default: true
azureappinsights:
connectionType: direct
resourceArmId: <your-arm-id>
accessToken: <your-access-token>
useAzureActiveDirectoryAuth: false
How do traces reach Azure App Insights?
The application code in the src/tracing.js
file uses the native App Insights library which sends telemetry straight to the Azure Cloud.
Run Both the Node.js App and Tracetest​
To start both the Node.js app and Tracetest, run this command:
docker compose -f ./docker-compose.yaml -f ./tracetest/docker-compose.yaml up -d
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!