Skip to content

Commit 6788d36

Browse files
committed
Simplify test environment creation and add option to run tests against local chatbot-rag-app
1 parent 64ecd7e commit 6788d36

File tree

7 files changed

+123
-104
lines changed

7 files changed

+123
-104
lines changed

example-apps/chatbot-rag-app/test/tracetest/README.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ We plan to add tests for additional model configurations in the future.*
55

66
## Introduction to Trace Testing
77

8-
Trace testing is a modern approach to testing distributed systems by leveraging the distributed traces that flow through your applications. In a complex system like the Chatbot RAG application, traditional testing approaches may fall short because they can't effectively monitor the interactions between microservices, databases, and external APIs.
8+
Trace testing is a modern approach to testing distributed systems by leveraging the distributed traces that flow through your applications.
99

10-
Tracetest is an open-source tool that enables you to create, run, and maintain integration tests using distributed traces with support for OpenTelemetry and observability backends such as Elastic APM. It allows you to:
10+
Tracetest is an open-source tool that enables you to create, run, and maintain tests using distributed traces with support for OpenTelemetry and observability backends such as Elastic APM. It allows you to:
1111

1212
- Validate the flow of requests through your entire system
1313
- Assert on specific spans within a trace
@@ -17,27 +17,18 @@ For more information about Tracetest, visit the [official documentation](https:/
1717

1818
## Setup
1919

20-
Chatbot RAG application setup uses Docker to create a testing environment that includes:
20+
Our setup uses Docker to create a testing environment that includes:
2121

2222
1. A Tracetest server for executing and managing tests
23-
2. An Elasticsearch cluster for storing traces, logs, and application data
24-
3. An OpenTelemetry collector for processing and routing telemetry data
25-
4. The chatbot RAG application itself
26-
27-
The setup leverages several Docker Compose files to combine the test environment with the local Elastic Stack (from [docker/docker-compose-elastic.yml](../../../../docker/docker-compose-elastic.yml)) and Chatbot RAG application (from [example-apps/chatbot-rag-app/docker-compose.yml](../../docker-compose.yml)). In order to spin up the up-to-date versions of all moving parts, we leverage overrides maintained within this directory. We use:
28-
29-
- `docker-compose.test.yml` - for Tracetest configuration
30-
- `docker-compose.test.override.yml` - Test-specific Tracetest configuration
31-
- `elastic-stack.override.yml` - for test-specific configuration for Elasticsearch and OpenTelemetry Collector
32-
- `chatbot-rag.override.yml` - for configuration of the chatbot application in test mode.
33-
34-
All services are connected through a shared Docker network to enable communication between components.
23+
2. An OpenTelemetry collector for processing and routing telemetry data
24+
3. Tracetest test running that uses Tracetest CLI to communicate with Tracetest server and execute tests based on the specified configuration.
25+
4. Optionally, you can also setup the Elastic Stack and the chatbot-rag-app itself if you don't have them running in your environment.
3526

3627
## Environment Configuration
3728

38-
Before running tests, you need to prepare a `.env.test` file with the necessary environment variables. This file configures the behavior of the chatbot application during testing (same configuration as described in [the applications's directory](../../README.md)).
29+
The test environment expects the Elastic Stack and chatbot-rag-app to run locally. If you already have these services running, you're ready to execute the tests and skip to the next section.
3930

40-
Create a `.env.test` file in the `test/tracetest` directory with the following content to reproduce the environment we're testing with:
31+
If you are starting without from scratch, you will need to set up the necessary environment variables for the chatbot-rag-app. For this you can follow setup the instructions in [the applications's directory](../../README.md#make-your-env-file) or copy our example to `.env` file in `example-apps/chatbot-rag-app`:
4132

4233
```bash
4334
# Location of the application routes
@@ -46,7 +37,7 @@ FLASK_APP=api/app.py
4637
PYTHONUNBUFFERED=1
4738

4839
# How you connect to Elasticsearch: change details to your instance
49-
ELASTICSEARCH_URL=http://elasticsearch:9200
40+
ELASTICSEARCH_URL=http://localhost:9200
5041
ELASTICSEARCH_USER=elastic
5142
ELASTICSEARCH_PASSWORD=elastic
5243

@@ -66,7 +57,7 @@ OTEL_SDK_DISABLED=false
6657
OTEL_SERVICE_NAME=chatbot-rag-app
6758

6859
# OpenTelemetry configuration
69-
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
60+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
7061
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
7162
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
7263

@@ -94,6 +85,12 @@ To run the trace-based tests for the chatbot RAG application, follow these steps
9485
./run-tests.sh
9586
```
9687

88+
If you already have Elastic stack and chatbot-rag-app running locally, you use the `--skip-stack-setup` and only spin up test resources.
89+
90+
```bash
91+
./run-tests.sh --skip-stack-setup
92+
```
93+
9794
To automatically clean up resources after the tests complete (or if they fail), you can use the --with-cleanup flag:
9895

9996
```bash
@@ -102,12 +99,13 @@ To run the trace-based tests for the chatbot RAG application, follow these steps
10299

103100
The script performs the following operations:
104101

105-
- Creates a shared Docker network for all services
106102
- Sets up the Tracetest server
107-
- Starts the Elastic stack (Elasticsearch and OpenTelemetry Collector)
108-
- Builds and starts the chatbot RAG application
103+
- If not using `--skip-stack-setup`:
104+
- Starts the Elastic stack (Elasticsearch and Kibana)
105+
- Builds and starts the chatbot-rag-app
106+
- Configures the OpenTelemetry Collector to send data to Tracetest
109107
- Executes the trace tests defined in `resources/openai-chatbot-test.yaml`
110-
- If `--with-cleanup` is provided, automatically cleans up all resources when the script exits (normally or due to an error)
108+
- If `--with-cleanup` is provided, automatically cleans up all resources when the script exits
111109

112110
The example test sends a question about working from home policy to the LLM via API and validates several aspects of the application:
113111

example-apps/chatbot-rag-app/test/tracetest/chatbot-rag.override.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

example-apps/chatbot-rag-app/test/tracetest/docker-compose.test.override.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

example-apps/chatbot-rag-app/test/tracetest/docker-compose.test.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ services:
2121
environment:
2222
TRACETEST_DEV: ${TRACETEST_DEV}
2323
extra_hosts:
24-
- host.docker.internal:host-gateway
24+
- localhost:host-gateway
2525
healthcheck:
2626
test:
2727
- CMD
@@ -37,6 +37,11 @@ services:
3737
target: 11633
3838
published: 11633
3939
protocol: tcp
40+
# Exposing the port for the tracetest otlp receiver that won't overlap with otel-collector.
41+
- mode: ingress
42+
target: 4317
43+
published: 11634
44+
protocol: tcp
4045
volumes:
4146
- type: bind
4247
source: ./resources/tracetest.yaml

example-apps/chatbot-rag-app/test/tracetest/elastic-stack.override.yml renamed to example-apps/chatbot-rag-app/test/tracetest/otel-collector.override.yml

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
networks:
2-
shared-test-network:
3-
external: true # Reference the network created above
4-
5-
services:
6-
elasticsearch:
7-
networks:
8-
- shared-test-network
9-
elasticsearch_settings:
10-
networks:
11-
- shared-test-network
12-
kibana:
13-
networks:
14-
- shared-test-network
15-
otel-collector:
16-
networks:
17-
- shared-test-network
181
configs:
19-
# This is the minimal yaml configuration needed to listen on all interfaces
20-
# for OTLP logs, metrics and traces, exporting to Elasticsearch.
21-
# Overriding for test purposes in order to add tracetest exporter
2+
# Overriding the config from docker/docker-compose-elastic.yml for test purposes in order to add tracetest exporter
223
edot-collector-config:
234
content: |
245
receivers:
@@ -52,7 +33,7 @@ configs:
5233
flush:
5334
interval: 1s # improve responsiveness in example apps (default 30s)
5435
otlp/tracetest:
55-
endpoint: http://tracetest:4317
36+
endpoint: http://localhost:11634
5637
tls:
5738
insecure: true
5839
@@ -76,4 +57,9 @@ configs:
7657
logs:
7758
receivers: [otlp]
7859
processors: []
79-
exporters: [elasticapm, elasticsearch]
60+
exporters: [elasticapm, elasticsearch]
61+
62+
services:
63+
otel-collector:
64+
extra_hosts:
65+
- localhost:host-gateway

example-apps/chatbot-rag-app/test/tracetest/resources/openai-chatbot-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spec:
77
type: http
88
httpRequest:
99
method: POST
10-
url: http://api-frontend:4000/api/chat
10+
url: http://localhost:4000/api/chat
1111
body: "{\"question\": \"What is the work from home policy?\"}"
1212
headers:
1313
- key: Content-Type

example-apps/chatbot-rag-app/test/tracetest/run-tests.sh

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,124 @@
22
set -e
33

44
CLEANUP=false
5+
SKIP_STACK_SETUP=false
6+
7+
show_help() {
8+
cat << EOF
9+
Usage: ./run-tests.sh [OPTIONS]
10+
11+
Run trace tests for the chatbot RAG application using Tracetest.
12+
13+
Options:
14+
--skip-stack-setup Assume Elastic stack and chatbot RAG are already running
15+
--with-cleanup Clean up resources after test execution
16+
--help Display this help message and exit
17+
18+
Examples:
19+
# Set up complete environment and run tests
20+
./run-tests.sh
21+
22+
# Run tests with existing, local Elastic stack and chatbot RAG
23+
./run-tests.sh --skip-stack-setup
24+
25+
# Set up everything, run tests, and clean up afterward
26+
./run-tests.sh --with-cleanup
27+
28+
# Run tests with existing services and clean up test components afterward
29+
./run-tests.sh --skip-stack-setup --with-cleanup
30+
EOF
31+
}
32+
533
for arg in "$@"; do
634
case $arg in
735
--with-cleanup)
836
CLEANUP=true
937
shift
1038
;;
39+
--skip-stack-setup)
40+
SKIP_STACK_SETUP=true
41+
shift
42+
;;
43+
--help)
44+
show_help
45+
exit 0
46+
;;
1147
esac
1248
done
1349

14-
NETWORK_NAME="shared-test-network"
15-
16-
ELASTIC_COMPOSE="../../../../docker/docker-compose-elastic.yml"
17-
ELASTIC_STACK_OVERRIDES="elastic-stack.override.yml"
1850

51+
ELASTIC_STACK_COMPOSE="../../../../docker/docker-compose-elastic.yml"
52+
# Docker compose override that configures the otel-collector to send traces to Tracetest
53+
OTEL_COLLECTOR_OVERRIDE="otel-collector.override.yml"
1954
TRACETEST_COMPOSE="docker-compose.test.yml"
20-
TRACETEST_COMPOSE_OVERRIDES="docker-compose.test.override.yml"
21-
2255
CHATBOT_RAG_COMPOSE="../../docker-compose.yml"
23-
CHATBOT_RAG_COMPOSE_OVERRIDES="chatbot-rag.override.yml"
2456

2557
cleanup() {
2658
echo "Cleaning up resources..."
27-
docker compose -f $CHATBOT_RAG_COMPOSE -f $CHATBOT_RAG_COMPOSE_OVERRIDES down
28-
docker compose -f $ELASTIC_COMPOSE -f $ELASTIC_STACK_OVERRIDES down
29-
docker compose -f $TRACETEST_COMPOSE -f $TRACETEST_COMPOSE_OVERRIDES down
30-
docker network rm $NETWORK_NAME || true
59+
60+
if [ "$SKIP_STACK_SETUP" = false ]; then
61+
# Clean up everything if not running in local mode
62+
echo "Cleaning up chatbot-rag-app..."
63+
docker compose -f $CHATBOT_RAG_COMPOSE down
64+
echo "Cleaning up Elastic stack..."
65+
docker compose -f $ELASTIC_STACK_COMPOSE down
66+
else
67+
# If running locally restore otel-collector to pre-test state
68+
echo "Stopping and removing otel-collector..."
69+
docker compose \
70+
-f $ELASTIC_STACK_COMPOSE \
71+
-f $OTEL_COLLECTOR_OVERRIDE \
72+
stop otel-collector
73+
docker compose \
74+
-f $ELASTIC_STACK_COMPOSE \
75+
-f $OTEL_COLLECTOR_OVERRIDE \
76+
rm -f otel-collector
77+
echo "Restarting otel-collector with original configuration..."
78+
docker compose \
79+
-f $ELASTIC_STACK_COMPOSE \
80+
up -d --wait otel-collector
81+
fi
82+
83+
# Always clean up Tracetest
84+
echo "Cleaning up Tracetest..."
85+
docker compose -f $TRACETEST_COMPOSE down
86+
87+
echo "Cleanup complete."
3188
}
3289

3390
# Set up cleanup trap if --with-cleanup was provided
3491
if [ "$CLEANUP" = true ]; then
3592
trap cleanup EXIT
3693
fi
3794

38-
# Create the shared network if it doesn't exist
39-
echo "Creating shared Docker network: $NETWORK_NAME"
40-
docker network create $NETWORK_NAME || echo "Shared network already exists"
95+
if [ "$SKIP_STACK_SETUP" = false ]; then
96+
echo "Setting up Elastic stack"
97+
docker compose \
98+
-f $ELASTIC_STACK_COMPOSE \
99+
up -d --wait elasticsearch kibana elasticsearch_settings
100+
fi
101+
102+
echo "Setting up Tracetest server"
103+
docker compose -f $TRACETEST_COMPOSE up --force-recreate -d --wait
41104

42-
echo "Setting up Tracetest tracetest server"
43-
docker compose -f $TRACETEST_COMPOSE -f $TRACETEST_COMPOSE_OVERRIDES up --force-recreate -d --wait
44105

45-
echo "Setting up Elastic stack"
106+
echo "Adding Tracetest exporter to otel-collector"
46107
docker compose \
47-
-f $ELASTIC_COMPOSE \
48-
-f $ELASTIC_STACK_OVERRIDES \
49-
up -d --wait
108+
-f $ELASTIC_STACK_COMPOSE \
109+
-f $OTEL_COLLECTOR_OVERRIDE \
110+
up -d --wait --force-recreate otel-collector
50111

51-
echo "Building and starting test environment"
52-
# Rebuilding the chatbot-rag app in order to test locally made changes
53-
docker compose -f $CHATBOT_RAG_COMPOSE -f $CHATBOT_RAG_COMPOSE_OVERRIDES build api-frontend
54-
docker compose -f $CHATBOT_RAG_COMPOSE -f $CHATBOT_RAG_COMPOSE_OVERRIDES up -d --wait
112+
if [ "$SKIP_STACK_SETUP" = false ]; then
113+
echo "Building and starting chatbot-rag-app"
114+
docker compose -f $CHATBOT_RAG_COMPOSE up --build -d --wait
115+
fi
55116

56117
echo "Executing trace tests"
57118

58119
docker run --rm \
59120
-v $(pwd)/resources:/resources \
60-
--network=$NETWORK_NAME \
121+
--add-host=localhost:host-gateway \
61122
--entrypoint tracetest \
62123
kubeshop/tracetest:v1.7.1 \
63-
-s http://tracetest:11633 \
124+
-s http://localhost:11633 \
64125
run test --file /resources/openai-chatbot-test.yaml

0 commit comments

Comments
 (0)