Skip to content

Latest commit

History

History
213 lines (162 loc) 路 7.3 KB

running-tracetest-with-azure-app-insights.mdx

File metadata and controls

213 lines (162 loc) 路 7.3 KB
id title description hide_table_of_contents keywords image
running-tracetest-with-azure-app-insights
Node.js and Azure Application Insights (Node.js SDK)
Quick start on how to configure a Node.js app with OpenTelemetry traces, Azure Application Insights as a trace data store, and Tracetest for enhancing your E2E and integration tests with trace-based testing.
false
tracetest
trace-based testing
observability
distributed tracing
testing

:::note Check out the source code on GitHub here. :::

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.

Sample 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

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!