Complete Observability Stack with Docker Compose - Python, PHP, Prometheus, Loki, OpenTelemetry and Grafana
1. Recap: the Loose Pieces So Far
Each article in this series brought up an isolated piece: Prometheus + Loki + Grafana in article 1, the Python mini app in article 2, the PHP mini app in article 3, sending logs to Loki in article 4, and trace instrumentation (so far only printed to the console) in article 5. This article brings everything together in a single docker-compose.yml, with a new piece: an OpenTelemetry collector, responsible for receiving traces from the mini apps via OTLP and forwarding them on.
2. Why a Collector in the Middle
In article 5, the mini apps exported spans straight to the console (ConsoleSpanExporter). That proves the instrumentation works, but it’s not how a real application talks to an observability stack — each application would have to know how to export directly to each final backend, duplicating endpoint, authentication and format configuration in all of them.
The OpenTelemetry Collector solves this by sitting in the middle: applications always export to the same place (the collector, via OTLP), and it’s the collector — configured once, centrally — that decides where the data goes next: a tracing backend, additional processing (sampling, attribute enrichment), or multiple destinations at once. Switching backends becomes a collector configuration change, not a redeploy of every instrumented application.
3. Swapping the Console Exporter for OTLP
In both mini apps, the only thing that changes from article 5 is the exporter — the way spans leave the process — not the instrumentation itself (tracer.start_as_current_span and spanBuilder(...)->startSpan() stay identical).
# tracing.py (Python) — swap ConsoleSpanExporter for OTLP
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter,
)
provider = TracerProvider()
exporter = OTLPSpanExporter(endpoint="http://otel-collector:4318/v1/traces")
provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("python-app")
pip install opentelemetry-exporter-otlp-proto-http
<?php
// tracing.php (PHP) — swap ConsoleSpanExporter for OTLP
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Common\Transport\Http\PsrTransportFactory;
$transport = (new PsrTransportFactory())->create(
'http://otel-collector:4318/v1/traces',
'application/x-protobuf'
);
$exporter = new SpanExporter($transport);
$tracerProvider = new TracerProvider(new SimpleSpanProcessor($exporter));
$tracer = $tracerProvider->getTracer('php-app');
The otel-collector hostname used in both cases is the Compose service name that appears in section 5.
4. Configuring the Collector
The collector needs a configuration file declaring three things: receivers (how data comes in), processors (optional transformations along the way) and exporters (where data goes out):
# otel-collector-config.yaml
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
processors:
batch:
exporters:
debug:
verbosity: detailed
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [debug]
This example uses the debug exporter (the successor to the old logging exporter), which prints received traces to the collector’s own log — enough to confirm the full pipeline works end to end: mini app → collector → output. Visualizing traces graphically, with a navigable span tree, would require a dedicated tracing backend (like Grafana Tempo or Jaeger) receiving from this same pipeline — outside the scope of this minimal stack, but a natural next step for anyone who wants to go deeper after this series.
5. The Unified docker-compose.yml
Putting everything the series has built so far into a single file:
# docker-compose.yml
services:
prometheus:
image: prom/prometheus:v2.55.0
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
loki:
image: grafana/loki:3.2.0
ports:
- "3100:3100"
otel-collector:
image: otel/opentelemetry-collector:0.112.0
command: ["--config=/etc/otel-collector-config.yaml"]
volumes:
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml:ro
ports:
- "4318:4318"
grafana:
image: grafana/grafana:11.3.0
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
depends_on:
- prometheus
- loki
python-app:
image: python:3.12-slim
working_dir: /app
volumes:
- ./python-app:/app:ro
command: ["sh", "-c", "pip install --no-cache-dir opentelemetry-sdk opentelemetry-exporter-otlp-proto-http && python3 app.py"]
ports:
- "8000:8000"
depends_on:
- otel-collector
- loki
php-app:
image: php:8.3-cli
working_dir: /app
volumes:
- ./php-app:/app
command: ["php", "-S", "0.0.0.0:8001", "app.php"]
ports:
- "8001:8001"
depends_on:
- otel-collector
- loki
And the prometheus.yml bringing together the targets from articles 2 and 3:
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "python-app"
static_configs:
- targets: ["python-app:8000"]
- job_name: "php-app"
static_configs:
- targets: ["php-app:8001"]
Bringing everything up at once:
docker compose up -d
docker compose ps
Six services should show up running: prometheus, loki, otel-collector, grafana, python-app and php-app.
6. Configuring the Datasources in Grafana
With every service on the same Compose network, under Connections → Data sources in Grafana, two datasources point at the services by name:
- Prometheus —
http://prometheus:9090 - Loki —
http://loki:3100
These are the two datasources article 7 of this series will actually use in dashboards. The OpenTelemetry collector doesn’t show up as a Grafana datasource in this setup — it only forwards traces to the debug exporter configured in section 4; connecting a traces datasource is the next step if a tracing backend is added to the stack.
7. Confirming the Full Pipeline Works
Generating traffic on both applications and checking each end:
curl http://localhost:8000/
curl http://localhost:8001/
# metrics
curl http://localhost:8000/metrics
curl http://localhost:8001/metrics
# collector logs, where the traces should show up printed
docker compose logs otel-collector --tail 50
If docker compose logs otel-collector shows blocks with TraceID, SpanID and Name for the requests you made, the end-to-end pipeline is working: the mini apps generate spans, the collector receives them via OTLP and processes them.
8. Conclusion and Next Steps
We brought together the six pieces built throughout the series into a single, reproducible stack: two mini applications instrumented for metrics, logs and traces, Prometheus scraping, Loki receiving logs, an OpenTelemetry collector receiving traces via OTLP, and Grafana with the Prometheus and Loki datasources configured. In the next article, the focus returns to Grafana: building real dashboards that combine PromQL and LogQL to follow the mini apps end to end in a single panel.
References: