Skip to content

Commit b8ae439

Browse files
authored
chore(examples/docs): Updating Otel Examples/Recipes (#3967)
* chore(examples/docs): Updating Otel Examples/Recipes * chore(examples/docs): Updating Otel Examples/Recipes * updating nodejs manual instrumentation * updating python example * updating recipes
1 parent 2e10f27 commit b8ae439

38 files changed

+377
-1438
lines changed

docs/docs/examples-tutorials/recipes/running-python-app-with-opentelemetry-collector-and-tracetest.mdx

Lines changed: 25 additions & 277 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ keywords:
1717
image: https://res.cloudinary.com/djwdcmwdz/image/upload/v1698686403/docs/Blog_Thumbnail_14_rsvkmo.jpg
1818
---
1919

20-
import Tabs from '@theme/Tabs';
21-
import TabItem from '@theme/TabItem';
22-
import CodeBlock from '@theme/CodeBlock';
23-
2420
:::note
2521
[Check out the source code on GitHub here.](https://github.com/kubeshop/tracetest/tree/main/examples/quick-start-python)
2622
:::
@@ -31,19 +27,12 @@ import CodeBlock from '@theme/CodeBlock';
3127

3228
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.
3329

34-
```mdx-code-block
35-
<Tabs groupId="running-tracetest-without-a-trace-data-store">
36-
<TabItem value="Tracetest" label="Cloud-based Managed Tracetest" default>
37-
```
38-
3930
## Prerequisites
4031

4132
**Tracetest Account**:
4233

4334
- 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).
4736

4837
**Docker**: Have [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed on your machine.
4938

@@ -61,11 +50,9 @@ cd tracetest/examples/quick-start-python
6150
Follow these instructions to run the quick start:
6251

6352
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.
6956

7057
Follow along with the sections below for a detailed breakdown of what the example you just ran did and how it works.
7158

@@ -82,8 +69,15 @@ The Python app is a simple Flask app, contained in the `app.py` file.
8269
Configure the `.env` like shown below.
8370

8471
```bash
72+
# Get the required information here: https://app.tracetest.io/retrieve-token
73+
74+
TRACETEST_TOKEN="<YOUR_TRACETEST_TOKEN>"
75+
TRACETEST_ENVIRONMENT_ID="<YOUR_ENV_ID>"
76+
77+
# GRPC
8578
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://tracetest-agent:4317/"
86-
TRACETEST_API_KEY="<YOUR_TRACETEST_API_KEY>"
79+
# or, use HTTP
80+
# OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://tracetest-agent:4318/v1/traces"
8781
```
8882

8983
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
107101

108102
The `docker-compose.yaml` contains just one service for the Python app. The service is started with the `command` parameter.
109103

110-
111104
```yaml title="docker-compose.yaml"
112105
command: opentelemetry-instrument --traces_exporter otlp --service_name app --exporter_otlp_endpoint ${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT} --exporter_otlp_insecure true flask run --host=0.0.0.0 --port=8080
113106
```
@@ -139,16 +132,16 @@ To start the full setup, run the following command:
139132
docker compose up -d
140133
```
141134

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.
143136

144137
```python
145138
app = Flask(__name__)
146139
147140
@app.route("/manual")
148141
def manual():
149142
with tracer.start_as_current_span(
150-
"manual",
151-
attributes={ "endpoint": "/manual", "foo": "bar" }
143+
"manual",
144+
attributes={ "endpoint": "/manual", "foo": "bar" }
152145
):
153146
return "App works with a manual instrumentation."
154147
@@ -163,277 +156,32 @@ def home():
163156

164157
## Run Tracetest Tests
165158

166-
1. Open [Tracetest](https://app.tracetest.io/)
167-
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:
169160

170161
```bash
171-
tracetest configure -t <YOUR_API_TOKEN>
172-
tracetest run test -f ./test-api.yaml
162+
docker compose run tracetest-run
173163
```
174164

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+
175171
Here's a sample of a failed test run, which happens if you use this selector and assertion pair.
176172

177173
Selector:
174+
178175
```
179176
span[tracetest.span.type="http" name="/automatic" http.target="/automatic" http.method="GET"]
180177
```
181178

182179
Assertion:
180+
183181
```
184182
attr:http.status_code = 200
185183
```
186184

187185
![assertion](https://res.cloudinary.com/djwdcmwdz/image/upload/v1715620289/docs/app.tracetest.io_organizations_ttorg_e66318ba6544b856_environments_ttenv_8fca16a31b8b6e24_tests_ayy2cq.png)
188186

189-
```mdx-code-block
190-
</TabItem>
191-
<TabItem value="Tracetest Core" label="Hobby Open-Source Tracetest Core">
192-
```
193-
194-
## Prerequisites
195-
196-
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
231-
232-
provider = TracerProvider()
233-
processor = BatchSpanProcessor(ConsoleSpanExporter())
234-
provider.add_span_processor(processor)
235-
trace.set_tracer_provider(provider)
236-
tracer = trace.get_tracer(__name__)
237-
```
238-
239-
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.
240-
241-
```python
242-
app = Flask(__name__)
243-
244-
@app.route("/manual")
245-
def manual():
246-
with tracer.start_as_current_span(
247-
"manual",
248-
attributes={ "endpoint": "/manual", "foo": "bar" }
249-
):
250-
return "App works with a manual instrumentation."
251-
252-
@app.route('/automatic')
253-
def automatic():
254-
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
286-
command: opentelemetry-instrument --traces_exporter otlp --service_name app --exporter_otlp_endpoint otel-collector:4317 --exporter_otlp_insecure true flask run --host=0.0.0.0 --port=8080
287-
depends_on:
288-
tracetest:
289-
condition: service_started
290-
```
291-
292-
To start it, run this command:
293-
294-
```bash
295-
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.
310-
311-
```yaml
312-
version: "3"
313-
services:
314-
tracetest:
315-
image: kubeshop/tracetest:latest
316-
platform: linux/amd64
317-
volumes:
318-
- type: bind
319-
source: ./tracetest/tracetest-config.yaml
320-
target: /app/tracetest.yaml
321-
- type: bind
322-
source: ./tracetest/tracetest-provision.yaml
323-
target: /app/provisioning.yaml
324-
ports:
325-
- 11633:11633
326-
command: --provisioning-file /app/provisioning.yaml
327-
depends_on:
328-
postgres:
329-
condition: service_healthy
330-
otel-collector:
331-
condition: service_started
332-
healthcheck:
333-
test: ["CMD", "wget", "--spider", "localhost:11633"]
334-
interval: 1s
335-
timeout: 3s
336-
retries: 60
337-
environment:
338-
TRACETEST_DEV: ${TRACETEST_DEV}
339-
340-
postgres:
341-
image: postgres:14
342-
environment:
343-
POSTGRES_PASSWORD: postgres
344-
POSTGRES_USER: postgres
345-
healthcheck:
346-
test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
347-
interval: 1s
348-
timeout: 5s
349-
retries: 60
350-
351-
otel-collector:
352-
image: otel/opentelemetry-collector-contrib:0.59.0
353-
command:
354-
- "--config"
355-
- "/otel-local-config.yaml"
356-
volumes:
357-
- ./tracetest/collector.config.yaml:/otel-local-config.yaml
358-
359-
```
360-
361-
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-
439187
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

Comments
 (0)