summaryrefslogtreecommitdiff
path: root/f3s/tracing-demo/README.md
blob: 5934c00566b1b237b06e427a6621042e5d109b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Tracing Demo Application

Three-tier Python Flask application demonstrating distributed tracing with OpenTelemetry and Grafana Tempo.

## Overview

This demo application shows how distributed tracing works across multiple microservices:

- **Frontend**: Receives HTTP requests, forwards to middleware
- **Middleware**: Transforms data, calls backend
- **Backend**: Returns data (simulates database queries)

Each service is instrumented with OpenTelemetry and sends traces to Grafana Tempo via Alloy.

## Architecture

```
User → Frontend (Flask:5000) → Middleware (Flask:5001) → Backend (Flask:5002)
           ↓                          ↓                        ↓
                    Alloy (OTLP:4317) → Tempo → Grafana
```

## Components

### Frontend Service
- Port: 5000
- Endpoints:
  - `GET /` - Service info and health
  - `GET /health` - Kubernetes health probe
  - `GET|POST /api/process` - Main processing endpoint
- Calls: Middleware service

### Middleware Service
- Port: 5001
- Endpoints:
  - `GET /` - Service info and health
  - `GET /health` - Kubernetes health probe
  - `POST /api/transform` - Data transformation endpoint
- Calls: Backend service

### Backend Service
- Port: 5002
- Endpoints:
  - `GET /` - Service info and health
  - `GET /health` - Kubernetes health probe
  - `GET /api/data` - Data retrieval endpoint (simulates DB query)
- Calls: None (leaf service)

## OpenTelemetry Instrumentation

All services use:
- **Auto-instrumentation**: Flask and Requests libraries automatically create spans
- **Manual spans**: Custom spans for business logic with attributes
- **OTLP export**: Traces sent to Alloy via gRPC on port 4317
- **Resource attributes**: Service name, namespace, version identify each service

## Build and Deploy

### Prerequisites

1. Tempo must be deployed and running in `monitoring` namespace
2. Alloy must be configured with OTLP receivers
3. Docker installed for building images
4. Access to k3s cluster (SSH to r0)

### Quick Start

```bash
# Build Docker images
just build

# Import images to k3s
just import

# Deploy with Helm
just install

# Check status
just status
```

### Rebuild and Update

```bash
# Rebuild images, import, and upgrade deployment
just rebuild
```

## Testing

### Basic Test

```bash
# Test health endpoint
curl http://tracing-demo.f3s.buetow.org/

# Test API endpoint (generates a trace)
curl http://tracing-demo.f3s.buetow.org/api/process
```

### Load Test

Generate 50 requests to create multiple traces:

```bash
just load-test
```

### View Logs

```bash
# View logs from all services
just logs

# Follow frontend logs
just logs-frontend

# Follow middleware logs
just logs-middleware

# Follow backend logs
just logs-backend
```

## Viewing Traces in Grafana

1. Navigate to Grafana: https://grafana.f3s.buetow.org
2. Go to Explore → Select "Tempo" datasource
3. Use TraceQL queries:

```
# All traces from demo app
{ resource.service.namespace = "tracing-demo" }

# Slow requests (>200ms)
{ duration > 200ms }

# Traces from specific service
{ resource.service.name = "frontend" }

# Errors
{ status = error }
```

4. View Service Graph to see connections between services

## Trace Features Demonstrated

### Distributed Context Propagation
Traces automatically span all three services, showing:
- Frontend span (root)
- Middleware span (child of frontend)
- Backend span (child of middleware)

### Custom Attributes
Each service adds custom attributes:
- `service.name` - Service identifier
- `service.namespace` - Application namespace
- Custom business logic attributes

### Trace Correlation
- **Traces-to-Logs**: Click on a span to see related logs in Loki
- **Traces-to-Metrics**: View Prometheus metrics for services in the trace
- **Service Graph**: Visualize service dependencies

## Development

### Local Testing with Port Forwarding

```bash
# Forward frontend
just port-forward-frontend
curl http://localhost:5000/

# Forward middleware
just port-forward-middleware
curl http://localhost:5001/

# Forward backend
just port-forward-backend
curl http://localhost:5002/
```

### Modifying the Application

1. Edit Python code in `docker/*/app.py`
2. Rebuild: `just build`
3. Import: `just import`
4. Upgrade: `just upgrade`

Or use the combined command: `just rebuild`

## Troubleshooting

### No traces appearing in Grafana

1. Check pods are running:
```bash
kubectl get pods -n services | grep tracing-demo
```

2. Check Alloy is receiving traces:
```bash
kubectl logs -n monitoring -l app.kubernetes.io/name=alloy | grep -i otlp
```

3. Check Tempo is storing traces:
```bash
kubectl logs -n monitoring -l app.kubernetes.io/name=tempo | grep -i trace
```

4. Verify OTLP endpoint is accessible:
```bash
kubectl exec -n services $(kubectl get pod -n services -l app=tracing-demo-frontend -o jsonpath='{.items[0].metadata.name}') -- wget -qO- http://alloy.monitoring.svc.cluster.local:4317
```

### Pods not starting

Check events and logs:
```bash
kubectl describe pod -n services -l app=tracing-demo-frontend
kubectl logs -n services -l app=tracing-demo-frontend
```

### Images not found

Verify images are imported to k3s:
```bash
ssh r0 'k3s crictl images | grep tracing-demo'
```

If missing, run:
```bash
just import
```

## Cleanup

Remove the demo application:

```bash
just delete
```

## References

- [OpenTelemetry Python Documentation](https://opentelemetry.io/docs/languages/python/)
- [Flask Instrumentation](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/flask/flask.html)
- [Grafana Tempo Documentation](https://grafana.com/docs/tempo/latest/)
- [TraceQL Query Language](https://grafana.com/docs/tempo/latest/traceql/)