Skip to main content

Node.js, Elasticsearch and Elastic APM

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.

Elastic is a search, observability, and security platform. The company is famous for developing the ELK stack - ElasticSearch, Kibana, Beats and Logstash that is used by thousands of developers. Elastic offers a cloud platform, Elastic Cloud, as its commercial offering to enable observability across logs, application performance monitoring (APM), and infrastructure monitoring, real user monitoring (RUM) and synthetic monitoring.

Node.js App with Elasticsearch, Elastic APM and Tracetest​

This is a simple quick start on how to configure a Node.js app to use Elastic APM Agent with traces and Tracetest for enhancing your E2E and integration tests with trace-based testing. The infrastructure will use Elastic APM and Elasticsearch as the trace data store and Elastic APM server to receive traces from the Node.js app and send them to Elasticsearch.

Prerequisites​

Tracetest 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 Elastic Node.js Quickstart:

git clone https://github.com/kubeshop/tracetest
cd tracetest/examples/tracetest-elasticapm-with-elastic-agent

Follow these instructions to run the quick start:

  1. Copy the .env.template file to .env.
  2. Log into the Tracetest app.
  3. 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.
  4. Run docker compose -f ./docker-compose.agent.yaml up -d.
  5. This example is configured to use the Elastic Tracing Backend. Ensure the environment you're using to run this example is configured to use the Elastic Tracing Backend by clicking on Settings, Tracing Backend, Elastic, Save. Or, use the CLI as explained below.
  6. 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, Elastic APM server, Elasticsearch, and a Node.js app.

The docker-compose.agent.yaml file in the root directory of the quick start runs the Node.js app, Elasticsearch, Elastic APM server, 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 Elastic APM tracing is contained in the elastic-apm-agent.js file. Traces will be sent to Elastic APM Server.

Enabling the tracer is done by preloading the trace file. As seen in the package.json.

"scripts": {
"with-elastic-apm-tracer":"node -r ./elastic-apm-agent.js app.js"
},

Configuring Elastic​

Configure Elastic as a Tracing Backend:

---
type: DataStore
spec:
id: current
name: elasticapm
type: elasticapm
elasticapm:
addresses:
- https://es01:9200
username: elastic
password: changeme
index: traces-apm-default
insecureSkipVerify: true
tracetest config -t <YOUR_API_TOKEN>
tracetest apply datastore -f ./tracetest-tracing-backend.yaml

Run the Node.js App, Elasticsearch and Elastic APM with Docker Compose​

The docker-compose.yaml file and Dockerfile in the root directory contain the Node.js app.

The docker-compose.agent.yaml file also contains the Tracetest Agent, Elasticsearch, Kibana, and Elastic APM server.

To start it, run this command:

docker compose -f ./docker-compose.agent.yaml up -d

This will start the Node.js app the Elastic APM server and send the traces to Elasticsearch.

Run Tracetest Tests​

  1. Open Tracetest.
  2. Configure Elastic as a tracing backend if you have not already as explained above.
  3. Start creating tests! Make sure to use the http://app:8080/ URL in your test creation.
  4. 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

Learn More​

Feel free to check out our examples in GitHub and join our Slack Community for more info!