Add OpenTelemetry tracing learning lab
This commit is contained in:
parent
f300c9eb99
commit
0766e4f081
|
|
@ -0,0 +1,60 @@
|
||||||
|
# OpenTelemetry trace lab
|
||||||
|
|
||||||
|
This small, profile-independent lab is managed by `./jeannie apps` through
|
||||||
|
Argo CD in the existing `monitoring` namespace. It adds a single OpenTelemetry
|
||||||
|
Collector, a single Tempo server, a Grafana `Tempo Lab` datasource, and a
|
||||||
|
five-minute synthetic trace job. Existing Prometheus and Loki are unchanged.
|
||||||
|
|
||||||
|
The path is **synthetic trace job → OTLP/HTTP collector → OTLP/gRPC Tempo →
|
||||||
|
Grafana Explore**. The job emits a parent span and child span for two simulated
|
||||||
|
services; it does not make real service calls or use the OpenTelemetry SDK.
|
||||||
|
This isolates the first lesson—trace IDs, span IDs, parent/child relationships,
|
||||||
|
collection and storage. The next step is instrumenting real applications.
|
||||||
|
|
||||||
|
Tempo stores traces in a size-limited `emptyDir`. Traces are disposable and
|
||||||
|
disappear when the Tempo pod is replaced or the node is lost; this is not a
|
||||||
|
backup or production storage. Retention is at most 24 hours while the pod and
|
||||||
|
its local volume survive, subject to the storage limit. The lab adds workload
|
||||||
|
resource requests and image pulls on every cluster profile. Neither endpoint
|
||||||
|
is published as an Ingress or NodePort; cluster-internal OTLP is unauthenticated
|
||||||
|
and unencrypted, so send only non-sensitive learning data.
|
||||||
|
|
||||||
|
After pushing the repository changes, on Debian select the intended profile
|
||||||
|
and apply the apps pipeline:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd ~/my-homelab-configs
|
||||||
|
git pull --ff-only gitea main
|
||||||
|
./jeannie cluster-profile select k3s
|
||||||
|
./jeannie apps
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `debian` or `pimox` instead of `k3s` when that profile is your intended
|
||||||
|
active cluster. On Debian, check the workloads and datasource:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kubectl --kubeconfig="$HOME/.kube/opi5-k3s-config" -n monitoring \
|
||||||
|
get deployment otel-lab-collector otel-lab-tempo
|
||||||
|
kubectl --kubeconfig="$HOME/.kube/opi5-k3s-config" -n monitoring \
|
||||||
|
get cronjob otel-lab-demo
|
||||||
|
```
|
||||||
|
|
||||||
|
The job runs every five minutes. In Grafana, open **Explore → Tempo Lab →
|
||||||
|
Search**, choose `jeannie-demo-frontend` as the service and run the query. Open
|
||||||
|
a trace to inspect its child span. The datasource relies on the Grafana
|
||||||
|
datasource sidecar enabled by the platform chart. For immediate testing, create
|
||||||
|
one job and inspect its trace ID:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kubectl --kubeconfig="$HOME/.kube/opi5-k3s-config" -n monitoring \
|
||||||
|
create job --from=cronjob/otel-lab-demo otel-lab-demo-manual
|
||||||
|
kubectl --kubeconfig="$HOME/.kube/opi5-k3s-config" -n monitoring \
|
||||||
|
logs job/otel-lab-demo-manual
|
||||||
|
```
|
||||||
|
|
||||||
|
The printed trace ID can be pasted into Grafana's Tempo trace-ID query. A
|
||||||
|
successful generator job confirms the collector accepted the request; check
|
||||||
|
Tempo and Grafana as well to establish end-to-end visibility. Manual job names
|
||||||
|
must be unique, or wait for Kubernetes' job TTL to remove the previous one.
|
||||||
|
ConfigMaps are mounted as files; restart the relevant deployment after changing
|
||||||
|
its configuration because these binaries do not automatically reload it.
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-collector-config
|
||||||
|
namespace: monitoring
|
||||||
|
data:
|
||||||
|
config.yaml: |
|
||||||
|
receivers:
|
||||||
|
otlp:
|
||||||
|
protocols:
|
||||||
|
grpc:
|
||||||
|
endpoint: 0.0.0.0:4317
|
||||||
|
http:
|
||||||
|
endpoint: 0.0.0.0:4318
|
||||||
|
processors:
|
||||||
|
memory_limiter:
|
||||||
|
check_interval: 1s
|
||||||
|
limit_mib: 128
|
||||||
|
spike_limit_mib: 32
|
||||||
|
batch:
|
||||||
|
exporters:
|
||||||
|
otlp:
|
||||||
|
endpoint: otel-lab-tempo.monitoring.svc:4317
|
||||||
|
tls:
|
||||||
|
insecure: true
|
||||||
|
service:
|
||||||
|
pipelines:
|
||||||
|
traces:
|
||||||
|
receivers: [otlp]
|
||||||
|
processors: [memory_limiter, batch]
|
||||||
|
exporters: [otlp]
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-collector
|
||||||
|
namespace: monitoring
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: otel-lab-collector
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: otel-lab-collector
|
||||||
|
spec:
|
||||||
|
automountServiceAccountToken: false
|
||||||
|
securityContext:
|
||||||
|
runAsNonRoot: true
|
||||||
|
runAsUser: 10001
|
||||||
|
seccompProfile:
|
||||||
|
type: RuntimeDefault
|
||||||
|
containers:
|
||||||
|
- name: collector
|
||||||
|
image: otel/opentelemetry-collector:0.120.1
|
||||||
|
args: ["--config=/conf/config.yaml"]
|
||||||
|
ports:
|
||||||
|
- name: otlp-grpc
|
||||||
|
containerPort: 4317
|
||||||
|
- name: otlp-http
|
||||||
|
containerPort: 4318
|
||||||
|
readinessProbe:
|
||||||
|
tcpSocket:
|
||||||
|
port: otlp-http
|
||||||
|
securityContext:
|
||||||
|
allowPrivilegeEscalation: false
|
||||||
|
readOnlyRootFilesystem: true
|
||||||
|
capabilities:
|
||||||
|
drop: ["ALL"]
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
cpu: 25m
|
||||||
|
memory: 64Mi
|
||||||
|
limits:
|
||||||
|
cpu: 250m
|
||||||
|
memory: 192Mi
|
||||||
|
volumeMounts:
|
||||||
|
- name: config
|
||||||
|
mountPath: /conf
|
||||||
|
readOnly: true
|
||||||
|
volumes:
|
||||||
|
- name: config
|
||||||
|
configMap:
|
||||||
|
name: otel-lab-collector-config
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-collector
|
||||||
|
namespace: monitoring
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: otel-lab-collector
|
||||||
|
ports:
|
||||||
|
- name: otlp-grpc
|
||||||
|
port: 4317
|
||||||
|
targetPort: otlp-grpc
|
||||||
|
- name: otlp-http
|
||||||
|
port: 4318
|
||||||
|
targetPort: otlp-http
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-tempo-datasource
|
||||||
|
namespace: monitoring
|
||||||
|
labels:
|
||||||
|
grafana_datasource: "1"
|
||||||
|
data:
|
||||||
|
tempo-lab.yaml: |
|
||||||
|
apiVersion: 1
|
||||||
|
datasources:
|
||||||
|
- name: Tempo Lab
|
||||||
|
uid: tempo-lab
|
||||||
|
type: tempo
|
||||||
|
access: proxy
|
||||||
|
url: http://otel-lab-tempo.monitoring.svc:3200
|
||||||
|
isDefault: false
|
||||||
|
editable: false
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
kind: Kustomization
|
||||||
|
configMapGenerator:
|
||||||
|
- name: otel-lab-demo-code
|
||||||
|
namespace: monitoring
|
||||||
|
files:
|
||||||
|
- trace_demo.py
|
||||||
|
generatorOptions:
|
||||||
|
disableNameSuffixHash: true
|
||||||
|
resources:
|
||||||
|
- collector.yaml
|
||||||
|
- tempo.yaml
|
||||||
|
- grafana-datasource.yaml
|
||||||
|
- trace-demo.yaml
|
||||||
|
|
@ -0,0 +1,118 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-tempo-config
|
||||||
|
namespace: monitoring
|
||||||
|
data:
|
||||||
|
tempo.yaml: |
|
||||||
|
server:
|
||||||
|
http_listen_port: 3200
|
||||||
|
distributor:
|
||||||
|
receivers:
|
||||||
|
otlp:
|
||||||
|
protocols:
|
||||||
|
grpc:
|
||||||
|
endpoint: 0.0.0.0:4317
|
||||||
|
ingester:
|
||||||
|
trace_idle_period: 10s
|
||||||
|
max_block_duration: 5m
|
||||||
|
compactor:
|
||||||
|
compaction:
|
||||||
|
block_retention: 24h
|
||||||
|
storage:
|
||||||
|
trace:
|
||||||
|
backend: local
|
||||||
|
wal:
|
||||||
|
path: /var/tempo/wal
|
||||||
|
local:
|
||||||
|
path: /var/tempo/blocks
|
||||||
|
usage_report:
|
||||||
|
reporting_enabled: false
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-tempo
|
||||||
|
namespace: monitoring
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
strategy:
|
||||||
|
type: Recreate
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: otel-lab-tempo
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: otel-lab-tempo
|
||||||
|
spec:
|
||||||
|
automountServiceAccountToken: false
|
||||||
|
securityContext:
|
||||||
|
runAsNonRoot: true
|
||||||
|
runAsUser: 10001
|
||||||
|
runAsGroup: 10001
|
||||||
|
fsGroup: 10001
|
||||||
|
seccompProfile:
|
||||||
|
type: RuntimeDefault
|
||||||
|
containers:
|
||||||
|
- name: tempo
|
||||||
|
image: grafana/tempo:2.7.2
|
||||||
|
args: ["-config.file=/etc/tempo/tempo.yaml"]
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
containerPort: 3200
|
||||||
|
- name: otlp-grpc
|
||||||
|
containerPort: 4317
|
||||||
|
readinessProbe:
|
||||||
|
httpGet:
|
||||||
|
path: /ready
|
||||||
|
port: http
|
||||||
|
initialDelaySeconds: 5
|
||||||
|
securityContext:
|
||||||
|
allowPrivilegeEscalation: false
|
||||||
|
readOnlyRootFilesystem: true
|
||||||
|
capabilities:
|
||||||
|
drop: ["ALL"]
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
cpu: 50m
|
||||||
|
memory: 128Mi
|
||||||
|
ephemeral-storage: 128Mi
|
||||||
|
limits:
|
||||||
|
cpu: 500m
|
||||||
|
memory: 512Mi
|
||||||
|
ephemeral-storage: 1Gi
|
||||||
|
volumeMounts:
|
||||||
|
- name: config
|
||||||
|
mountPath: /etc/tempo
|
||||||
|
readOnly: true
|
||||||
|
- name: data
|
||||||
|
mountPath: /var/tempo
|
||||||
|
- name: tmp
|
||||||
|
mountPath: /tmp
|
||||||
|
volumes:
|
||||||
|
- name: config
|
||||||
|
configMap:
|
||||||
|
name: otel-lab-tempo-config
|
||||||
|
- name: data
|
||||||
|
emptyDir:
|
||||||
|
sizeLimit: 512Mi
|
||||||
|
- name: tmp
|
||||||
|
emptyDir:
|
||||||
|
sizeLimit: 64Mi
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-tempo
|
||||||
|
namespace: monitoring
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: otel-lab-tempo
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
port: 3200
|
||||||
|
targetPort: http
|
||||||
|
- name: otlp-grpc
|
||||||
|
port: 4317
|
||||||
|
targetPort: otlp-grpc
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
apiVersion: batch/v1
|
||||||
|
kind: CronJob
|
||||||
|
metadata:
|
||||||
|
name: otel-lab-demo
|
||||||
|
namespace: monitoring
|
||||||
|
spec:
|
||||||
|
schedule: "*/5 * * * *"
|
||||||
|
concurrencyPolicy: Forbid
|
||||||
|
successfulJobsHistoryLimit: 1
|
||||||
|
failedJobsHistoryLimit: 2
|
||||||
|
jobTemplate:
|
||||||
|
spec:
|
||||||
|
backoffLimit: 2
|
||||||
|
activeDeadlineSeconds: 90
|
||||||
|
ttlSecondsAfterFinished: 3600
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
restartPolicy: Never
|
||||||
|
automountServiceAccountToken: false
|
||||||
|
securityContext:
|
||||||
|
runAsNonRoot: true
|
||||||
|
runAsUser: 65532
|
||||||
|
seccompProfile:
|
||||||
|
type: RuntimeDefault
|
||||||
|
containers:
|
||||||
|
- name: trace-demo
|
||||||
|
image: python:3.12-alpine
|
||||||
|
command: ["python3", "/app/trace_demo.py"]
|
||||||
|
securityContext:
|
||||||
|
allowPrivilegeEscalation: false
|
||||||
|
readOnlyRootFilesystem: true
|
||||||
|
capabilities:
|
||||||
|
drop: ["ALL"]
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
cpu: 10m
|
||||||
|
memory: 24Mi
|
||||||
|
limits:
|
||||||
|
cpu: 100m
|
||||||
|
memory: 96Mi
|
||||||
|
volumeMounts:
|
||||||
|
- name: demo-code
|
||||||
|
mountPath: /app
|
||||||
|
readOnly: true
|
||||||
|
volumes:
|
||||||
|
- name: demo-code
|
||||||
|
configMap:
|
||||||
|
name: otel-lab-demo-code
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Emit one synthetic parent/child OTLP trace without external dependencies."""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import secrets
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
|
||||||
|
def build_trace(now_ns, trace_id, parent_id, child_id):
|
||||||
|
def span(name, span_id, start, end, parent=None):
|
||||||
|
value = {'traceId': trace_id, 'spanId': span_id, 'name': name,
|
||||||
|
'startTimeUnixNano': str(start), 'endTimeUnixNano': str(end), 'kind': 1}
|
||||||
|
if parent:
|
||||||
|
value['parentSpanId'] = parent
|
||||||
|
return value
|
||||||
|
|
||||||
|
def resource(service, spans):
|
||||||
|
return {'resource': {'attributes': [{'key': 'service.name', 'value': {'stringValue': service}}]},
|
||||||
|
'scopeSpans': [{'scope': {'name': 'jeannie.synthetic-trace-demo'}, 'spans': spans}]}
|
||||||
|
|
||||||
|
return {'resourceSpans': [
|
||||||
|
resource('jeannie-demo-frontend', [span('handle request', parent_id, now_ns, now_ns + 200_000_000)]),
|
||||||
|
resource('jeannie-demo-backend', [span('fetch data', child_id, now_ns + 40_000_000,
|
||||||
|
now_ns + 160_000_000, parent_id)]),
|
||||||
|
]}
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
trace_id = secrets.token_hex(16)
|
||||||
|
payload = build_trace(time.time_ns(), trace_id, secrets.token_hex(8), secrets.token_hex(8))
|
||||||
|
endpoint = os.environ.get('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT',
|
||||||
|
'http://otel-lab-collector.monitoring.svc:4318/v1/traces')
|
||||||
|
request = urllib.request.Request(endpoint, data=json.dumps(payload).encode(),
|
||||||
|
headers={'Content-Type': 'application/json'}, method='POST')
|
||||||
|
with urllib.request.urlopen(request, timeout=10) as response:
|
||||||
|
if response.status != 200:
|
||||||
|
raise RuntimeError(f'Collector returned HTTP {response.status}')
|
||||||
|
print(f'Sent synthetic trace {trace_id}')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -52,6 +52,15 @@ variable "applications" {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
default = {
|
default = {
|
||||||
|
opentelemetry-lab = {
|
||||||
|
project = "default"
|
||||||
|
path = "apps/opentelemetry-lab"
|
||||||
|
namespace = "monitoring"
|
||||||
|
target_revision = "main"
|
||||||
|
prune = true
|
||||||
|
self_heal = true
|
||||||
|
create_namespace = false
|
||||||
|
}
|
||||||
container-registry = {
|
container-registry = {
|
||||||
project = "default"
|
project = "default"
|
||||||
path = "apps/container-registry"
|
path = "apps/container-registry"
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,7 @@ validate_security_static() {
|
||||||
validate_unit_tests() {
|
validate_unit_tests() {
|
||||||
"${REPO_ROOT}/tests/jeannie-unit"
|
"${REPO_ROOT}/tests/jeannie-unit"
|
||||||
python3 -m unittest discover -s "${REPO_ROOT}/tests/jeannie" -p 'test_doctor_dex_drift.py'
|
python3 -m unittest discover -s "${REPO_ROOT}/tests/jeannie" -p 'test_doctor_dex_drift.py'
|
||||||
|
python3 -m unittest discover -s "${REPO_ROOT}/tests/jeannie" -p 'test_opentelemetry_lab.py'
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
import importlib.util
|
||||||
|
from pathlib import Path
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[2]
|
||||||
|
LAB = ROOT / 'apps' / 'opentelemetry-lab'
|
||||||
|
spec = importlib.util.spec_from_file_location('trace_demo', LAB / 'trace_demo.py')
|
||||||
|
demo = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(demo)
|
||||||
|
|
||||||
|
|
||||||
|
class OpenTelemetryLabTest(unittest.TestCase):
|
||||||
|
def test_synthetic_trace_has_connected_parent_and_child(self):
|
||||||
|
trace_id, parent_id, child_id = 'a' * 32, 'b' * 16, 'c' * 16
|
||||||
|
trace = demo.build_trace(1_000_000_000, trace_id, parent_id, child_id)
|
||||||
|
frontend, backend = trace['resourceSpans']
|
||||||
|
root = frontend['scopeSpans'][0]['spans'][0]
|
||||||
|
child = backend['scopeSpans'][0]['spans'][0]
|
||||||
|
self.assertEqual(root['traceId'], child['traceId'])
|
||||||
|
self.assertEqual(child['parentSpanId'], root['spanId'])
|
||||||
|
self.assertLessEqual(int(root['startTimeUnixNano']), int(child['startTimeUnixNano']))
|
||||||
|
self.assertGreaterEqual(int(root['endTimeUnixNano']), int(child['endTimeUnixNano']))
|
||||||
|
|
||||||
|
def test_collector_and_tempo_are_cluster_internal_and_bounded(self):
|
||||||
|
for filename in ('collector.yaml', 'tempo.yaml'):
|
||||||
|
documents = list(yaml.safe_load_all((LAB / filename).read_text()))
|
||||||
|
deployment = next(doc for doc in documents if doc['kind'] == 'Deployment')
|
||||||
|
service = next(doc for doc in documents if doc['kind'] == 'Service')
|
||||||
|
self.assertNotIn('type', service['spec'])
|
||||||
|
self.assertEqual(deployment['spec']['replicas'], 1)
|
||||||
|
limits = deployment['spec']['template']['spec']['containers'][0]['resources']['limits']
|
||||||
|
self.assertIn('memory', limits)
|
||||||
|
tempo = list(yaml.safe_load_all((LAB / 'tempo.yaml').read_text()))
|
||||||
|
deployment = next(doc for doc in tempo if doc['kind'] == 'Deployment')
|
||||||
|
data = next(volume for volume in deployment['spec']['template']['spec']['volumes'] if volume['name'] == 'data')
|
||||||
|
self.assertEqual(data['emptyDir']['sizeLimit'], '512Mi')
|
||||||
Loading…
Reference in New Issue