You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Check out the source code on GitHub here.](https://github.com/kubeshop/tracetest/tree/main/examples/quick-start-python)
26
22
:::
@@ -31,19 +27,12 @@ import CodeBlock from '@theme/CodeBlock';
31
27
32
28
This is a simple quick start on how to configure a Python app to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your e2e and integration tests with trace-based testing.
- Sign up to [`app.tracetest.io`](https://app.tracetest.io) or follow the [get started](/getting-started/installation) docs.
44
-
- Create an [environment](/concepts/environments).
45
-
- Create an [environment token](/concepts/environment-tokens).
46
-
- Have access to the environment's [agent API key](/configuration/agent).
35
+
- Have access to the environment's [agent API key](https://app.tracetest.io/retrieve-token).
47
36
48
37
**Docker**: Have [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed on your machine.
49
38
@@ -61,11 +50,9 @@ cd tracetest/examples/quick-start-python
61
50
Follow these instructions to run the quick start:
62
51
63
52
1. Copy the `.env.template` file to `.env`.
64
-
2. Log into the [Tracetest app](https://app.tracetest.io/).
65
-
3. This example is configured to use the OpenTelemetry Tracing Backend. Ensure the environment you will be utilizing to run this example is also configured to use the OpenTelemetry Tracing Backend by clicking on Settings, Tracing Backend, OpenTelemetry, Save.
66
-
4. Fill out the [token](https://docs.tracetest.io/concepts/environment-tokens) and [API key](https://docs.tracetest.io/concepts/agent) details by editing your `.env` file. You can find these values in the Settings area for your environment.
67
-
5. Run `docker compose up -d`.
68
-
6. Run tests from the Tracetest Web UI by accessing the app with the URL `http://app:8080/`.
53
+
2. Fill out the [TRACETEST_TOKEN and ENVIRONMENT_ID](https://app.tracetest.io/retrieve-token) details by editing your `.env` file.
54
+
3. Run `docker compose run tracetest-run`.
55
+
4. Follow the links in the output to view the test results.
69
56
70
57
Follow along with the sections below for a detailed breakdown of what the example you just ran did and how it works.
71
58
@@ -82,8 +69,15 @@ The Python app is a simple Flask app, contained in the `app.py` file.
82
69
Configure the `.env` like shown below.
83
70
84
71
```bash
72
+
# Get the required information here: https://app.tracetest.io/retrieve-token
The OpenTelemetry tracing is contained both as automatic and manual instrumentation. Traces will be sent to the Tracetest Agent. Here's the `requirements.txt` with OpenTelemetry libraries.
@@ -107,7 +101,6 @@ EXPOSE 8080
107
101
108
102
The `docker-compose.yaml` contains just one service for the Python app. The service is started with the `command` parameter.
@@ -139,16 +132,16 @@ To start the full setup, run the following command:
139
132
docker compose up -d
140
133
```
141
134
142
-
There are 3 endpoints in the Flask app. To see manual instrumentation, trigger the `"/manual"` endpoint. To see the automatic instrumentation, trigger the `"/automatic"` endpoint respectively.
135
+
There are 3 endpoints in the Flask app. To see manual instrumentation, trigger the `"/manual"` endpoint. To see the automatic instrumentation, trigger the `"/automatic"` endpoint respectively.
2. Start creating tests! Make sure to use the `http://app:8080/automatic` or `http://app:8080/manual` URLs in your test creation.
168
-
3. To trigger tests in the CLI, first [install the CLI](/cli/cli-installation-reference), [configure it](/cli/configuring-your-cli), and [run a test](/cli/running-tests). From the root of the quick start directory, run:
159
+
To execute the tests, run this command:
169
160
170
161
```bash
171
-
tracetest configure -t <YOUR_API_TOKEN>
172
-
tracetest run test -f ./test-api.yaml
162
+
docker compose run tracetest-run
173
163
```
174
164
165
+
This will:
166
+
1. Start the Node.js app, the OpenTelemetry Collector, and send the traces to the Tracetest Agent.
167
+
2. Start the Tracetest Agent.
168
+
3. Configure the tracing backend and create tests in your environment.
169
+
4. Run the tests.
170
+
175
171
Here's a sample of a failed test run, which happens if you use this selector and assertion pair.
You will need [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed on your machine to run this quick start app!
197
-
198
-
## Project structure
199
-
200
-
The project is built with Docker Compose. It contains two distinct `docker-compose.yaml` files.
201
-
202
-
### 1. Python app
203
-
204
-
The `docker-compose.yaml` file and `Dockerfile` in the root directory are for the Python app.
205
-
206
-
### 2. Tracetest
207
-
208
-
The `docker-compose.yaml` file, `collector.config.yaml`, `tracetest-provision.yaml`, and `tracetest-config.yaml` in the `tracetest` directory are for setting up Tracetest and the OpenTelemetry Collector.
209
-
210
-
The `tracetest` directory is self-contained and will run all the prerequisites for enabling OpenTelemetry traces and trace-based testing with Tracetest.
211
-
212
-
### Docker Compose Network
213
-
214
-
All `services` in the `docker-compose.yaml` are on the same network and will be reachable by hostname from within other services. E.g. `tracetest:4317` in the `collector.config.yaml` will map to the `tracetest` service, where the port `4317` is the port where Tracetest accepts traces.
215
-
216
-
## Python app
217
-
218
-
The Python app is a simple Flask app, contained in the `app.py` file.
219
-
220
-
The code below imports all the Flask, and OpenTelemetry libraries and configures both manual and automatic OpenTelemetry instrumentation.
221
-
222
-
```python
223
-
from flask import Flask, request
224
-
import json
225
-
226
-
from opentelemetry import trace
227
-
from opentelemetry.sdk.resources import Resource
228
-
from opentelemetry.sdk.trace import TracerProvider
229
-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
230
-
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
There are 3 endpoints in the Flask app. For seeing manual instrumentation trigger the `"/manual"` endpoint. For seeing the automatic instrumentation trigger the `"/automatic"` endpoint respectively.
return "App works with automatic instrumentation."
255
-
256
-
@app.route("/")
257
-
def home():
258
-
return "App works."
259
-
```
260
-
261
-
The `Dockerfile` includes bootstrapping the needed OpenTelemetry packages. As you can see it does not have the `CMD` command. Instead, it's configured in the `docker-compose.yaml` below.
262
-
263
-
```Dockerfile
264
-
FROM python:3.10.1-slim
265
-
WORKDIR /opt/app
266
-
COPY . .
267
-
RUN pip install --no-cache-dir -r requirements.txt
268
-
RUN opentelemetry-bootstrap -a install
269
-
EXPOSE 8080
270
-
```
271
-
272
-
The `docker-compose.yaml` contains just one service for the Python app. The service is stared with the `command` parameter.
273
-
274
-
```yaml
275
-
version: '3'
276
-
services:
277
-
app:
278
-
image: quick-start-python
279
-
platform: linux/amd64
280
-
extra_hosts:
281
-
- "host.docker.internal:host-gateway"
282
-
build: .
283
-
ports:
284
-
- "8080:8080"
285
-
# using the command here instead of the Dockerfile
docker compose build # optional if you haven't already built the image
296
-
docker compose up
297
-
```
298
-
299
-
This will start the Python app. But, you're not sending the traces anywhere.
300
-
301
-
Let's fix this by configuring Tracetest and OpenTelemetry Collector.
302
-
303
-
## Tracetest
304
-
305
-
The `docker-compose.yaml` in the `tracetest` directory is configured with three services.
306
-
307
-
- **Postgres** - Postgres is a prerequisite for Tracetest to work. It stores trace data when running the trace-based tests.
308
-
- [**OpenTelemetry Collector**](https://opentelemetry.io/docs/collector/) - A vendor-agnostic implementation of how to receive, process and export telemetry data.
309
-
- [**Tracetest**](https://tracetest.io/) - Trace-based testing that generates end-to-end tests automatically from traces.
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.
362
-
363
-
The `tracetest-config.yaml` file contains the basic setup of connecting Tracetest to the Postgres instance.
364
-
365
-
```yaml
366
-
postgres:
367
-
host: postgres
368
-
user: postgres
369
-
password: postgres
370
-
port: 5432
371
-
dbname: postgres
372
-
params: sslmode=disable
373
-
```
374
-
375
-
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.
376
-
377
-
```yaml
378
-
---
379
-
type: DataStore
380
-
spec:
381
-
name: OpenTelemetry Collector
382
-
type: otlp
383
-
isdefault: true
384
-
```
385
-
386
-
But how are traces sent to Tracetest?
387
-
388
-
The `collector.config.yaml` explains that. It receives traces via either `grpc` or `http`. Then, exports them to Tracetest's otlp endpoint `tracetest:4317`.
389
-
390
-
```yaml
391
-
receivers:
392
-
otlp:
393
-
protocols:
394
-
grpc:
395
-
http:
396
-
397
-
processors:
398
-
batch:
399
-
timeout: 100ms
400
-
401
-
exporters:
402
-
logging:
403
-
loglevel: debug
404
-
otlp/1:
405
-
endpoint: tracetest:4317
406
-
# Send traces to Tracetest.
407
-
# Read more in docs here: https://docs.tracetest.io/configuration/connecting-to-data-stores/opentelemetry-collector
408
-
tls:
409
-
insecure: true
410
-
411
-
service:
412
-
pipelines:
413
-
traces/1:
414
-
receivers: [otlp]
415
-
processors: [batch]
416
-
exporters: [otlp/1]
417
-
418
-
```
419
-
420
-
## Run both the Python app and Tracetest
421
-
422
-
To start both the Python app and Tracetest we will run this command:
423
-
424
-
```bash
425
-
docker-compose -f docker-compose.yaml -f tracetest/docker-compose.yaml up # add --build if the images are not built already
426
-
```
427
-
428
-
This will start your Tracetest instance on `http://localhost:11633/`. Go ahead and open it up.
429
-
430
-
Start creating tests! Make sure to use the `http://app:8080/` url in your test creation, because your Python app and Tracetest are in the same network.
431
-
432
-
```mdx-code-block
433
-
</TabItem>
434
-
</Tabs>
435
-
```
436
-
437
-
## Learn more
438
-
439
187
Feel free to check out our [examples in GitHub](https://github.com/kubeshop/tracetest/tree/main/examples), and join our [Slack Community](https://dub.sh/tracetest-community) for more info!
0 commit comments