ติดตั้ง OpenTelemetry Collector บน VPS — Observability ครบจบที่เดียว
OpenTelemetry (OTel) คือ CNCF standard สำหรับ observability ที่รวม metrics, distributed traces และ logs ไว้ในโปรโตคอลเดียวกัน — OTLP (OpenTelemetry Protocol) แทนที่จะต้องติดตั้ง agent แยกสำหรับแต่ละ backend คุณติดตั้ง OTel Collector ตัวเดียว แล้วให้ app ส่งข้อมูลมาที่ Collector ผ่าน OTLP จากนั้น Collector จะ route ไปยัง Prometheus, Jaeger, Loki หรือ backend ใดก็ได้
บทความนี้จะพาติดตั้ง OTel Collector บน Ubuntu VPS แบบ standalone (ไม่ต้องใช้ Kubernetes) พร้อมตัวอย่าง instrument app ด้วย Python และ Node.js
สถาปัตยกรรม OpenTelemetry
ในโลก OTel มีส่วนประกอบหลัก 3 อย่าง:
- SDK / Auto-instrumentation — ฝั่ง app (Python, Go, Java, Node.js) สร้าง spans, metrics และ log records
- OTel Collector — รับข้อมูลจาก app ผ่าน OTLP gRPC/HTTP, แปลง, กรอง แล้วส่งต่อไป backend
- Backend — Prometheus (metrics), Jaeger/Tempo (traces), Loki (logs)
| Signal | Protocol | Default Port | Backend แนะนำ |
|---|---|---|---|
| Metrics | OTLP gRPC / HTTP | 4317 / 4318 | Prometheus |
| Traces | OTLP gRPC / HTTP | 4317 / 4318 | Jaeger / Tempo |
| Logs | OTLP gRPC / HTTP | 4317 / 4318 | Loki |
สิ่งที่ต้องเตรียม
- VPS Ubuntu 22.04 / 24.04 หรือ Debian 12
- RAM อย่างน้อย 1 GB (แนะนำ 2 GB ถ้า run Jaeger ด้วย)
- Docker + Docker Compose (สำหรับ run backend)
- สิทธิ์ root หรือ sudo
ติดตั้ง OTel Collector
เราจะใช้ otelcol-contrib distribution ที่มี component ครบกว่า official release
ดาวน์โหลด binary
# ตรวจสอบ version ล่าสุดที่ GitHub releases
OTELCOL_VERSION="0.115.0"
ARCH=$(dpkg --print-architecture) # amd64 หรือ arm64
curl -LO "https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v${OTELCOL_VERSION}/otelcol-contrib_${OTELCOL_VERSION}_linux_${ARCH}.deb"
sudo dpkg -i "otelcol-contrib_${OTELCOL_VERSION}_linux_${ARCH}.deb"
# ตรวจสอบ
otelcol-contrib --version
dpkg -i ซ้ำได้เลย — มันจะ replace ไฟล์เก่าโดยอัตโนมัติสร้าง config ไฟล์
OTel Collector ใช้ YAML config แบ่งเป็น 4 ส่วน: receivers, processors, exporters และ service
sudo mkdir -p /etc/otelcol-contrib
sudo tee /etc/otelcol-contrib/config.yaml <<'EOF'
receivers:
otlp:
protocols:
grpc:
endpoint: "0.0.0.0:4317"
http:
endpoint: "0.0.0.0:4318"
# รับ host metrics จาก VPS ตัวเอง
hostmetrics:
collection_interval: 30s
scrapers:
cpu:
memory:
disk:
load:
network:
filesystem:
processors:
# กรอง spans ที่ไม่จำเป็น
batch:
timeout: 5s
send_batch_size: 1000
# เพิ่ม resource attributes
resourcedetection:
detectors: [env, system]
system:
hostname_sources: [os]
# จำกัด memory ไม่ให้ OOM
memory_limiter:
check_interval: 1s
limit_mib: 400
spike_limit_mib: 100
exporters:
# ส่ง metrics ไป Prometheus (scrape mode)
prometheus:
endpoint: "0.0.0.0:8889"
namespace: otelcol
# ส่ง traces ไป Jaeger
otlp/jaeger:
endpoint: "localhost:14250"
tls:
insecure: true
# debug output (เปิดระหว่าง test)
debug:
verbosity: basic
service:
pipelines:
metrics:
receivers: [otlp, hostmetrics]
processors: [memory_limiter, resourcedetection, batch]
exporters: [prometheus]
traces:
receivers: [otlp]
processors: [memory_limiter, resourcedetection, batch]
exporters: [otlp/jaeger, debug]
logs:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [debug]
EOF
เปิดใช้งาน service
sudo systemctl enable otelcol-contrib
sudo systemctl start otelcol-contrib
sudo systemctl status otelcol-contrib
ติดตั้ง Jaeger สำหรับ Distributed Tracing
Jaeger เป็น backend สำหรับ visualize distributed traces — ใช้ Docker Compose รัน all-in-one mode ได้ทันที
mkdir -p ~/jaeger && cat > ~/jaeger/docker-compose.yml <<'EOF'
services:
jaeger:
image: jaegertracing/all-in-one:1.62
ports:
- "16686:16686" # Jaeger UI
- "14250:14250" # gRPC (รับจาก OTel Collector)
- "14268:14268" # HTTP thrift (legacy)
environment:
COLLECTOR_OTLP_ENABLED: "true"
restart: unless-stopped
EOF
cd ~/jaeger && docker compose up -d
http://your-vps-ip:16686 — ถ้าใช้ Cloudflare Tunnel หรือ Nginx reverse proxy อย่าลืมเปิด port 16686 ให้ internal เท่านั้นInstrument แอป Python
OTel มี auto-instrumentation สำหรับ Python ที่ใช้กับ Flask, FastAPI, Django, SQLAlchemy ได้โดยไม่ต้องแก้โค้ด
ติดตั้ง package
pip install opentelemetry-distro opentelemetry-exporter-otlp
# auto-detect framework และติดตั้ง instrumentation ให้อัตโนมัติ
opentelemetry-bootstrap -a install
เริ่ม Flask app พร้อม OTel
# ตั้ง environment variables
export OTEL_SERVICE_NAME="my-flask-app"
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=production"
# รัน app ผ่าน opentelemetry-instrument
opentelemetry-instrument python app.py
Instrument แอป Node.js
สำหรับ Node.js ใช้ @opentelemetry/sdk-node เป็น auto-instrumentation สำหรับ Express, HTTP calls และ database queries
ติดตั้ง package
npm install \
@opentelemetry/sdk-node \
@opentelemetry/auto-instrumentations-node \
@opentelemetry/exporter-trace-otlp-http \
@opentelemetry/exporter-metrics-otlp-http
สร้าง tracing.js
// tracing.js — โหลดก่อน app ทุกครั้ง
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
const { PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const sdk = new NodeSDK({
serviceName: 'my-node-app',
traceExporter: new OTLPTraceExporter({
url: 'http://localhost:4318/v1/traces',
}),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: 'http://localhost:4318/v1/metrics',
}),
exportIntervalMillis: 15000,
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
เริ่ม app
node --require ./tracing.js server.js
# หรือใน package.json:
# "start": "node --require ./tracing.js server.js"
เชื่อม Prometheus กับ OTel Collector
OTel Collector expose metrics endpoint ที่ port 8889 ให้ Prometheus scrape — เพิ่ม job ใน Prometheus config:
# /etc/prometheus/prometheus.yml — เพิ่ม scrape job
scrape_configs:
- job_name: 'otelcol'
static_configs:
- targets: ['localhost:8889']
scrape_interval: 30s
# reload Prometheus
sudo systemctl reload prometheus
# หรือ curl -X POST http://localhost:9090/-/reload
ตรวจสอบว่า OpenTelemetry Collector ทำงานถูกต้อง
# ดู service status
sudo systemctl status otelcol-contrib
# ดู log แบบ real-time
sudo journalctl -u otelcol-contrib -f
# ตรวจสอบ port ที่ listen อยู่
ss -tlnp | grep -E '4317|4318|8889'
# ทดสอบส่ง span ด้วย curl (OTLP HTTP JSON)
curl -X POST http://localhost:4318/v1/traces \
-H "Content-Type: application/json" \
-d '{"resourceSpans":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"test-service"}}]},"scopeSpans":[{"spans":[{"traceId":"5B8EFFF798038103D269B633813FC60C","spanId":"EEE19B7EC3C1B174","name":"test-span","startTimeUnixNano":"1700000000000000000","endTimeUnixNano":"1700000001000000000","kind":1,"status":{}}]}]}]}'
# ตรวจสอบ metrics ที่ Prometheus endpoint
curl -s http://localhost:8889/metrics | grep otelcol | head -20
journalctl แสดง Memory limit reached ให้เพิ่ม limit_mib ใน memory_limiter processor หรือลด collection_interval ของ hostmetrics receiverแก้ปัญหาที่พบบ่อย
Error: "bind: address already in use" บน port 4317/4318
มี process อื่นใช้ port เหล่านี้อยู่แล้ว หรือ otelcol-contrib เริ่มมากกว่าหนึ่งครั้ง
# หา process ที่ใช้ port
sudo lsof -i :4317
sudo lsof -i :4318
# หรือ
ss -tlnp | grep -E '4317|4318'
# ถ้ามี otelcol process ค้างอยู่
sudo pkill -f otelcol-contrib
sudo systemctl start otelcol-contrib
Error: "failed to export traces: context deadline exceeded"
OTel Collector ส่ง traces ไป Jaeger แต่ Jaeger ไม่ตอบสนอง — ตรวจสอบว่า Jaeger container ทำงานอยู่
docker ps | grep jaeger
docker logs jaeger --tail 50
# ถ้า Jaeger หลุดออกไป ให้ restart
cd ~/jaeger && docker compose restart
# ทดสอบ connectivity จาก OTel Collector ไป Jaeger
nc -zv localhost 14250
Error: "pipeline: traces; exporter: debug; Dropping data because sending queue is full"
App ส่ง traces เยอะเกินไปและ exporter queue เต็ม ต้อง tune batch processor
# เพิ่ม queue capacity ใน /etc/otelcol-contrib/config.yaml
exporters:
otlp/jaeger:
endpoint: "localhost:14250"
tls:
insecure: true
sending_queue:
num_consumers: 4
queue_size: 1000
retry_on_failure:
enabled: true
initial_interval: 5s
max_interval: 30s
sudo systemctl restart otelcol-contrib
Error: "grpc: the client connection is closing" จาก app
App พยายาม connect ผ่าน gRPC (port 4317) แต่ OTel Collector ยังไม่ start หรือ firewall block
# ตรวจสอบ firewall
sudo ufw status | grep -E '4317|4318'
# เปิด port เฉพาะ internal (127.0.0.1) ถ้า app และ collector อยู่เครื่องเดียวกัน
# แก้ config ให้ bind เฉพาะ localhost
# endpoint: "127.0.0.1:4317" ← ปลอดภัยกว่า 0.0.0.0
Metrics ไม่ปรากฏใน Prometheus
OTel Collector expose metrics ที่ port 8889 แต่ Prometheus config ยังไม่ได้เพิ่ม scrape job
# ตรวจสอบ Prometheus targets
curl -s http://localhost:9090/api/v1/targets | python3 -m json.tool | grep otelcol
# ดู Prometheus config ปัจจุบัน
cat /etc/prometheus/prometheus.yml | grep -A5 otelcol
# reload หลังแก้ config
sudo systemctl reload prometheus
คำถามที่พบบ่อย
OTel Collector ต่างจาก Prometheus Node Exporter ตรงไหน?
Node Exporter รวบรวม host metrics เฉพาะ Linux system (CPU, memory, disk) และ expose ให้ Prometheus scrape ส่วน OTel Collector เป็น universal pipeline ที่รับทั้ง metrics, traces และ logs จาก app ด้วย OTLP protocol แล้วส่งต่อไป backend หลายตัวพร้อมกัน ในทางปฏิบัติหลายคนใช้ทั้งสองตัวควบคู่กัน
ต้องเปลี่ยน code เยอะไหมถ้าจะ instrument app?
ถ้าใช้ auto-instrumentation (Python opentelemetry-instrument หรือ Node.js --require ./tracing.js) แทบไม่ต้องแก้ code เลย — framework อย่าง Flask, FastAPI, Express จะถูก instrument อัตโนมัติ รวมถึง HTTP calls, database queries และ Redis ด้วย แต่ถ้าอยากเพิ่ม custom span หรือ attribute ก็ต้องเพิ่ม code เล็กน้อย
OTel Collector กินทรัพยากรมากไหม?
บน VPS ขนาด 1 vCPU / 1 GB RAM OTel Collector ใช้ RAM ประมาณ 50-150 MB และ CPU <1% ในสภาวะปกติ ถ้า app ส่ง traces เยอะมาก (high-traffic) ให้เพิ่ม RAM budget ใน memory_limiter และเปิด tail_sampling processor เพื่อ sample เฉพาะ traces ที่สำคัญ