Skip to content

Commit

Permalink
docs(saas-recipe): add tabs for saas recipe (#3273)
Browse files Browse the repository at this point in the history
* docs(saas-recipe): add tabs for saas recipe

* docs(saas-recipe): update saas recipe for node and otel
  • Loading branch information
adnanrahic committed Oct 17, 2023
1 parent 9ee7a08 commit c63c8cb
Show file tree
Hide file tree
Showing 10 changed files with 5,053 additions and 24 deletions.
Expand Up @@ -87,5 +87,5 @@ tracetest apply datastore -f my/data-store/file/location.yaml
```

:::tip
To learn more, [read the recipe on running a sample app with OpenTelemetry Collector and Tracetest](../../examples-tutorials/recipes/running-tracetest-without-a-trace-data-store.md).
To learn more, [read the recipe on running a sample app with OpenTelemetry Collector and Tracetest](../../examples-tutorials/recipes/running-tracetest-without-a-trace-data-store.mdx).
:::
2 changes: 1 addition & 1 deletion docs/docs/examples-tutorials/recipes.mdx
Expand Up @@ -29,7 +29,7 @@ These recipes show integrations with trace data stores and tracing vendors/provi

This integration point uses the OpenTelemetry Collector as a router to send trace data to Tracetest.

- [Sending traces directly to Tracetest from a Node.js app using OpenTelemetry Collector](./recipes/running-tracetest-without-a-trace-data-store.md)
- [Sending traces directly to Tracetest from a Node.js app using OpenTelemetry Collector](./recipes/running-tracetest-without-a-trace-data-store.mdx)
- [Sending traces with manual instrumentation directly to Tracetest from a Node.js app using OpenTelemetry Collector](./recipes/running-tracetest-without-a-trace-data-store-with-manual-instrumentation.md)
- [Sending traces with manual instrumentation directly to Tracetest from a Python app using OpenTelemetry Collector](./recipes/running-python-app-with-opentelemetry-collector-and-tracetest.md)

Expand Down
@@ -1,14 +1,173 @@
# Running Tracetest without a Trace Data Store
---
id: running-tracetest-without-a-trace-data-store
title: Running Tracetest without a Trace Data Store
description: Tracetest allows you to quickly build integration and end-to-end tests, powered by your OpenTelemetry traces.
hide_table_of_contents: true
keywords:
- tracetest
- trace-based testing
- observability
- distributed tracing
- testing
image: https://res.cloudinary.com/djwdcmwdz/image/upload/v1689693872/docs/Blog_Thumbnail_28_ugy2yy.png
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from '@theme/CodeBlock';


:::note
[Check out the source code on GitHub here.](https://github.com/kubeshop/tracetest/tree/main/examples/quick-start-nodejs)
[Check out the source code on GitHub here.](https://github.com/kubeshop/tracetest/tree/main/examples/quick-start-nodejs)
:::

[Tracetest](https://tracetest.io/) is a testing tool based on [OpenTelemetry](https://opentelemetry.io/) 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.
Build integration and end-to-end tests in minutes, instead of days, using [OpenTelemetry](https://opentelemetry.io/) and [Tracetest](https://tracetest.io/).

## Node.js App with Tracetest

## Sample Node.js app with OpenTelemetry Collector and Tracetest
This is a quick start on how to configure a Node.js app to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your E2E and integration tests with trace-based testing.

This is a simple quick start on how to configure a Node.js app to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your E2E and integration tests with trace-based testing.
```mdx-code-block
<Tabs groupId="running-tracetest-without-a-trace-data-store">
<TabItem value="Tracetest" label="Tracetest" default>
```

## Prerequisites

You can run this example with [Docker](https://docs.docker.com/get-docker/), or locally with Node.js installed on your machine.

## Project Structure

The project contains Tracetest Agent and a Node.js app.

### 1. Tracetest Agent

Install and run Tracetest Agent locally.

```bash title=Terminal
tracetest start
```

Once started, Tracetest Agent will:

- Expose OTLP ports `4317` (gRPC) and `4318` (HTTP) for trace ingestion.
- Be able to trigger test runs in the environment where it is running.
- Be able to connect to a trace data store that is not accessible outside of your environment.

### 2. Node.js App

The Node.js app is a simple Express app, contained in the `app.js` file.

The OpenTelemetry tracing is contained in the `tracing.otel.grpc.js` or `tracing.otel.http.js` files.
Traces will be sent to Tracetest Agent.

Here's the content of the `tracing.otel.grpc.js` file:

```js
const opentelemetry = require('@opentelemetry/sdk-node')
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node')
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');

const sdk = new opentelemetry.NodeSDK({
// OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is passed into "new OTLPTraceExporter" automatically
traceExporter: new OTLPTraceExporter(),
instrumentations: [getNodeAutoInstrumentations()],
})
sdk.start()
```

Choosing the `tracing.otel.grpc.js` file will send traces to Tracetest Agent's `GRPC`.

Configure it an environment variable:
- Running in Docker: `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://host.docker.internal:4317`
- Running locally: `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317`

Enabling the tracer is done by preloading the trace file.

```bash
node -r ./tracing.otel.grpc.js app.js
```

In the `package.json` you will see two npm scripts for running the respective tracers alongside the `app.js`.

```json
"scripts": {
"with-grpc-tracer":"node -r ./tracing.otel.grpc.js app.js",
"with-http-tracer":"node -r ./tracing.otel.http.js app.js"
},
```

To start the server, run this command:

```bash
npm run with-grpc-tracer
# or
npm run with-http-tracer
```

## Run the Node.js App with Docker Compose

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

As you can see, the `Dockerfile` uses the command above.

```Dockerfile
FROM node:slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "with-grpc-tracer" ]
```

And, the `docker-compose.yaml` contains just one service for the Node.js app.

```yaml
version: '3'
services:
app:
image: quick-start-nodejs
extra_hosts:
- "host.docker.internal:host-gateway"
build: .
ports:
- "8080:8080"
environment:
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://host.docker.internal:4317
```

To start it, run this command:

```bash
docker compose build # optional if you haven't already built the image
docker compose up
```

This will start the Node.js app and send the traces to Tracetest Agent.

## Run the Node.js App Locally

Install Node.js and npm in your local development environment.

Install the npm modules, export the OTLP endpoint, and run the Node.js app.

```bash
npm i
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317
npm run with-grpc-tracer
```

This will start the Node.js app and send the traces to Tracetest Agent.

## Run Tracetest Tests

Open [Tracetest](https://app.tracetest.io/) and start creating tests! Make sure to use the `http://localhost:8080/` URL in your test creation.

```mdx-code-block
</TabItem>
<TabItem value="Tracetest Core" label="Tracetest Core">
```

## Prerequisites

Expand All @@ -21,7 +180,7 @@ The project is built with Docker Compose. It contains two distinct `docker-compo
### 1. Node.js App
The `docker-compose.yaml` file and `Dockerfile` in the root directory are for the Node.js app.

### 2. Tracetest
### 2. Tracetest Core
The `docker-compose.yaml` file, `collector.config.yaml`, `tracetest-provision.yaml`, and `tracetest.config.yaml` in the `tracetest` directory are for the setting up Tracetest and the OpenTelemetry Collector.

The `tracetest` directory is self-contained and will run all the prerequisites for enabling OpenTelemetry traces and trace-based testing with Tracetest.
Expand Down Expand Up @@ -50,7 +209,7 @@ const sdk = new opentelemetry.NodeSDK({
sdk.start()
```

Depending on which of these you choose, traces will be sent to either the `grpc` or `http` endpoint.
Depending on which of these you choose, traces will be sent to either the `GRPC` or `HTTP` endpoint.

The hostnames and ports for these are:

Expand Down Expand Up @@ -115,13 +274,13 @@ This will start the Node.js app. But, you're not sending the traces anywhere.

Let's fix this by configuring Tracetest and OpenTelemetry Collector.

## Tracetest
## Tracetest Core

The `docker-compose.yaml` in the `tracetest` directory is configured with three services.

- **Postgres** - Postgres is a prerequisite for Tracetest to work. It stores trace data when running the trace-based tests.
- [**OpenTelemetry Collector**](https://opentelemetry.io/docs/collector/) - A vendor-agnostic implementation of how to receive, process and export telemetry data.
- [**Tracetest**](https://tracetest.io/) - Trace-based testing that generates end-to-end tests automatically from traces.
- [**Tracetest Core**](https://github.com/kubeshop/tracetest) - Trace-based testing that generates end-to-end tests automatically from traces.

```yaml
version: "3"
Expand Down Expand Up @@ -149,8 +308,6 @@ services:
interval: 1s
timeout: 3s
retries: 60
environment:
TRACETEST_DEV: ${TRACETEST_DEV}

postgres:
image: postgres:14
Expand All @@ -173,15 +330,15 @@ services:

```

Tracetest depends on both Postgres and the OpenTelemetry Collector. Both Tracetest and the OpenTelemetry Collector require config files to be loaded via a volume. The volumes are mapped from the root directory into the `tracetest` directory and the respective config files.
Tracetest Core depends on both Postgres and the OpenTelemetry Collector. Both Tracetest Core and the OpenTelemetry Collector require config files to be loaded via a volume. The volumes are mapped from the root directory into the `tracetest` directory and the respective config files.

To start both the Node.js app and Tracetest we will run this command:
To start both the Node.js app and Tracetest Core we will run this command:

```bash
docker-compose -f docker-compose.yaml -f tracetest/docker-compose.yaml up # add --build if the images are not built already
```

The `tracetest-config.yaml` file contains the basic setup of connecting Tracetest to the Postgres instance.
The `tracetest-config.yaml` file contains the basic setup of connecting Tracetest Core to the Postgres instance.

```yaml
postgres:
Expand All @@ -194,7 +351,7 @@ postgres:

```

The `tracetest-provision.yaml` file provisions the trace data store and polling to store in the Postgres database. The data store is set to OTLP, meaning the traces will be stored in Tracetest itself.
The `tracetest-provision.yaml` file provisions the trace data store and polling to store in the Postgres database. The data store is set to OTLP, meaning the traces will be sent to Tracetest Core.

But how are traces sent to Tracetest?

Expand Down Expand Up @@ -238,9 +395,14 @@ To start both the Node.js app and Tracetest, run this command:
docker-compose -f docker-compose.yaml -f tracetest/docker-compose.yaml up # add --build if the images are not built already
```

This will start your Tracetest instance on `http://localhost:11633/`.
This will start your Tracetest Core instance on `http://localhost:11633/`.

Open the URL and start creating tests! Make sure to use the `http://app:8080/` URL in your test creation, because your Node.js app and Tracetest Core are in the same network.

Open the URL and start creating tests! Make sure to use the `http://app:8080/` URL in your test creation, because your Node.js app and Tracetest are in the same network.
```mdx-code-block
</TabItem>
</Tabs>
```

## Learn More

Expand Down
4 changes: 2 additions & 2 deletions docs/sidebars.js
Expand Up @@ -141,12 +141,12 @@ const sidebars = {
},
{
type: "category",
label: "OpenTelemetry Collector",
label: "OpenTelemetry",
items: [
{
type: "doc",
id: "examples-tutorials/recipes/running-tracetest-without-a-trace-data-store",
label: "Node.js and OpenTelemetry Collector",
label: "Node.js and OpenTelemetry",
},
{
type: "doc",
Expand Down
9 changes: 9 additions & 0 deletions examples/quick-start-nodejs/docker-compose.yaml
Expand Up @@ -7,3 +7,12 @@ services:
build: .
ports:
- "8080:8080"
environment:
# Local Tracetest Agent: GRPC
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://host.docker.internal:4317
# Local Tracetest Agent: HTTP
# - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://host.docker.internal:4318/v1/traces
# Tracetest Core: GRPC
# - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://otel-collector:4317
# Tracetest Core: HTTP
# - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://otel-collector:4318/v1/traces

0 comments on commit c63c8cb

Please sign in to comment.