Skip to main content

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.

Prerequisites​

Tracetest Account:

Azure 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 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:

  1. Copy the .env.template file to .env.
  2. Fill out the TRACETEST_TOKEN and ENVIRONMENT_ID details by editing your .env file.
  3. Fill out the APP_INSIGHTS_ACCESS_TOKEN, APP_INSIGHTS_ARM_ID and APP_INSIGHTS_INSTRUMENTATION_STRING details by editing your .env file.
  4. Run docker compose run tracetest-run.
  5. Follow the links in the output to view the test results.

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, 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.

# Get the required information here: https://app.tracetest.io/retrieve-token

TRACETEST_TOKEN="<YOUR_TRACETEST_TOKEN>"
TRACETEST_ENVIRONMENT_ID="<YOUR_ENV_ID>"

# Azure
APP_INSIGHTS_ACCESS_TOKEN=""
APP_INSIGHTS_ARM_ID="/subscriptions/<id>/resourceGroups/app-insights-1/providers/microsoft.insights/components/<name>"
APP_INSIGHTS_INSTRUMENTATION_STRING=""

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"
},

Running the Node.js App and Tracetest​

The docker-compose.yaml file in the root directory contains the Node.js app and Tracetest Agent.

To run everything including Tracetest tests, run this command:

docker compose run tracetest-run

This will:

  1. Start the Node.js app and send the traces to Azure App Insights.
  2. Start the Tracetest Agent.
  3. Configure the Azure App Insights tracing backend and create tests in your environment.
  4. Run the tests.

The Test File​

Check out the resources/test.yaml file.

# resources/test.yaml
type: Test
spec:
id: W656Q0c4g
name: Test API
description: Test the App.
pollingProfile: azure
trigger:
type: http
httpRequest:
url: http://app:8080
method: GET
headers:
- key: Content-Type
value: application/json
specs:
- selector: span[tracetest.span.type="http" name="GET /"]
assertions:
- attr:http.response.status_code = 200
- attr:tracetest.span.duration < 500ms

Learn More​

Please visit our examples in GitHub and join our Slack Community for more info!