Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
**
!Dockerfile
!pyproject.toml
!setup.cfg
!README.md
!LICENSE
!src/
!src/**
!cookbook/
!cookbook/**
**/.env
**/.env.*
**/__pycache__
**/*.egg-info
**/.DS_Store
18 changes: 4 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# ---------- Stage 0: grab pre-built binaries ----------
FROM redis:7.4.9 AS redis
FROM grafana/otel-lgtm:0.28.0 AS lgtm
FROM modelscope-registry.cn-hangzhou.cr.aliyuncs.com/modelscope-repo/modelscope:ubuntu22.04-cuda13.0.3-py312-torch2.11.0-vllm0.21.0-modelscope1.36.3-swift4.2.3

# Put the real CUDA runtime ahead of package-private stubs, then keep the forward-compat
Expand All @@ -11,8 +10,7 @@ ENV LD_LIBRARY_PATH="/usr/local/cuda-13.0/targets/x86_64-linux/lib:/usr/local/cu
RUN pip config set global.index-url https://mirrors.cloud.aliyuncs.com/pypi/simple && \
pip config set install.trusted-host mirrors.cloud.aliyuncs.com && \
pip install --no-cache-dir \
tinker==0.16.1 \
"ray[serve]" && \
"ray[serve]>=2.55" && \
pip install --no-cache-dir --no-deps \
flash-linear-attention \
apache-tvm-ffi==0.1.9 \
Expand All @@ -22,22 +20,14 @@ RUN pip config set global.index-url https://mirrors.cloud.aliyuncs.com/pypi/simp
python -c "import ctypes, os; stub_path = '/usr/local/lib/python3.12/site-packages/tilelang/lib/libcudart_stub.so'; lib = ctypes.CDLL(stub_path); getattr(lib, 'cudaDeviceReset'); print(f'tilelang cudart path: {os.path.realpath(stub_path)}')" && \
rm -rf /root/.cache /tmp/*

# Clone and install twinkle, checkout to latest v-tag
RUN git clone https://github.com/modelscope/twinkle.git
WORKDIR /twinkle
RUN echo "Available release branches:" && git branch -r -l 'origin/release/*' --sort=-v:refname && \
LATEST_RELEASE=$(git branch -r -l 'origin/release/*' --sort=-v:refname | head -n 1 | tr -d ' ') && \
echo "Checking out: $LATEST_RELEASE" && \
git checkout --track "$LATEST_RELEASE"
COPY pyproject.toml setup.cfg README.md LICENSE ./
COPY src/ ./src/

# Install twinkle itself (with server extras: redis + otel + telemetry)
RUN pip install -e ".[server]" --no-build-isolation
COPY cookbook/ ./cookbook/

# ---------- Redis server ----------
COPY --from=redis /usr/local/bin/redis-server /usr/local/bin/redis-server
COPY --from=redis /usr/local/bin/redis-cli /usr/local/bin/redis-cli

# ---------- Observability: Grafana LGTM stack ----------
COPY --from=lgtm /otel-lgtm /otel-lgtm
COPY cookbook/observability/grafana/dashboards/twinkle-overview.json \
/otel-lgtm/grafana/conf/provisioning/dashboards/twinkle-overview.json
71 changes: 3 additions & 68 deletions cookbook/client/server/megatron/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# ============================================
# Twinkle Megatron 服务启动脚本
# ============================================
# 功能:启动 Ray 集群(支持 GPU 节点)、LGTM 观测栈和 Twinkle 服务器。
# 功能:启动 Redis、Ray 集群(支持 GPU 节点)和 Twinkle 服务器。
# 遥测由服务器配置中的 telemetry.otlp_endpoint 发送到独立的 Collector。
#
# 用法:./run.sh [选项]
#
Expand Down Expand Up @@ -63,16 +64,6 @@ REDIS_LOG_FILE="/twinkle/redis.log"
DEFAULT_SAVE_DIR="${TWINKLE_SAVE_DIR:-$TWINKLE_RUNTIME_DIR/save}"
DEFAULT_SERVER_CONFIG_FILE="/twinkle/cookbook/client/server/megatron/server_config.yaml"

# --- LGTM 版本配置(与 grafana/otel-lgtm:0.28.0 保持一致) ---
LGTM_VERSION="${LGTM_VERSION:-0.28.0}"
GRAFANA_VERSION="${GRAFANA_VERSION:-v13.0.1}"
PROMETHEUS_VERSION="${PROMETHEUS_VERSION:-v3.11.3}"
TEMPO_VERSION="${TEMPO_VERSION:-v2.10.5}"
LOKI_VERSION="${LOKI_VERSION:-v3.7.1}"
PYROSCOPE_VERSION="${PYROSCOPE_VERSION:-v2.0.2}"
OPENTELEMETRY_COLLECTOR_VERSION="${OPENTELEMETRY_COLLECTOR_VERSION:-v0.151.0}"
OBI_VERSION="${OBI_VERSION:-v0.9.0}"

# --- 单实例与重启请求配置 ---
RUN_SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
TWINKLE_RUN_OWNER="${USER:-shared}"
Expand Down Expand Up @@ -106,6 +97,7 @@ print_usage() {
--temp-dir DIR Ray 临时目录
--save-dir DIR Twinkle 模型保存目录 (默认: $DEFAULT_SAVE_DIR)
--server-config FILE Twinkle 服务器配置文件路径 (默认: $DEFAULT_SERVER_CONFIG_FILE)
部署时在该配置中设置 telemetry.otlp_endpoint
--help, -h 显示帮助信息

环境变量:
Expand Down Expand Up @@ -421,23 +413,6 @@ wait_for_redis_stopped() {
return 1
}

wait_for_lgtm_ready() {
local lgtm_pid="$1"
local timeout="${2:-90}"

for ((i=1; i<=timeout; i++)); do
if [ -f /tmp/ready ]; then
return 0
fi
if ! kill -0 "$lgtm_pid" 2>/dev/null; then
return 1
fi
sleep 1
done

return 1
}

start_log_tail() {
tail -F "$LOG_FILE" &
TAIL_PID=$!
Expand Down Expand Up @@ -503,15 +478,6 @@ cleanup_existing_runtime() {
return 1
fi
fi

print_info "停止已有的 LGTM 观测栈..."
pkill -f "/otel-lgtm/run-all.sh" 2>/dev/null || true
pkill prometheus 2>/dev/null || true
pkill grafana 2>/dev/null || true
pkill otelcol 2>/dev/null || true
pkill loki 2>/dev/null || true
pkill tempo 2>/dev/null || true
pkill pyroscope 2>/dev/null || true
}

cleanup_script_exit() {
Expand Down Expand Up @@ -656,36 +622,6 @@ start_ray_cluster() {
ray status 2>/dev/null || true
}

start_lgtm() {
print_header "启动 LGTM 观测栈(可选)"

if [ -d "/otel-lgtm" ]; then
if [ -f /otel-lgtm/prometheus.yaml.orig ]; then
cp /otel-lgtm/prometheus.yaml.orig /otel-lgtm/prometheus.yaml
fi

print_info "启动 LGTM 观测栈..."
rm -f /tmp/ready
export LGTM_VERSION GRAFANA_VERSION PROMETHEUS_VERSION TEMPO_VERSION LOKI_VERSION
export PYROSCOPE_VERSION OPENTELEMETRY_COLLECTOR_VERSION OBI_VERSION
(cd /otel-lgtm && exec nohup ./run-all.sh > /twinkle/lgtm.log 2>&1) &
LGTM_PID=$!

if wait_for_lgtm_ready "$LGTM_PID" 120; then
print_success "LGTM 观测栈已启动"
echo " - Grafana: http://localhost:3000 (admin/admin)"
echo " - OTLP gRPC: localhost:4317"
echo " - OTLP HTTP: localhost:4318"
echo " - 日志文件: /twinkle/lgtm.log"
else
print_warning "LGTM 观测栈未在 120 秒内就绪,Twinkle 将继续启动"
echo " - 日志文件: /twinkle/lgtm.log"
fi
else
print_warning "未检测到 LGTM 观测栈 (/otel-lgtm),跳过"
fi
}

start_twinkle_server() {
print_header "启动 Twinkle 服务器"

Expand Down Expand Up @@ -728,7 +664,6 @@ run_service_once() {
cleanup_existing_runtime
start_redis
start_ray_cluster
start_lgtm
start_twinkle_server
wait_runtime
}
Expand Down
5 changes: 2 additions & 3 deletions cookbook/client/server/megatron/server_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ http_options:
host: 0.0.0.0 # Listen on all network interfaces
port: 9000 # Port number for the server

# Telemetry: push traces/metrics/logs to LGTM's OTel Collector via OTLP
# Set otlp_endpoint in deployment config to the Collector reachable from Runtime.
telemetry:
enabled: true
otlp_endpoint: http://localhost:4317
enabled: false

# Persistence configuration for ServerState (sessions, models, futures, ...).
# Top-level placement makes the launcher propagate this to every Ray worker
Expand Down
6 changes: 6 additions & 0 deletions cookbook/observability/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM grafana/otel-lgtm:0.28.0

COPY grafana/dashboards/twinkle-overview.json \
/otel-lgtm/twinkle-overview.json
COPY grafana/overview-provider.yaml \
/otel-lgtm/grafana/conf/provisioning/dashboards/twinkle-overview.yaml
74 changes: 61 additions & 13 deletions cookbook/observability/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Twinkle Observability Stack

A one-container OTLP receiver + dashboard for Twinkle, built on the
For local development, build the standalone Twinkle LGTM image on top of the
[`grafana/otel-lgtm`](https://github.com/grafana/docker-otel-lgtm) image.
That image bundles OTel Collector, Mimir (Prometheus-compatible), Tempo,
Loki, and Grafana with everything pre-wired — no extra config files needed.
It bundles OTel Collector, Prometheus, Tempo, Loki, Grafana, and the Twinkle
Server Overview dashboard.

## What you get

Expand All @@ -18,7 +18,7 @@ Loki, and Grafana with everything pre-wired — no extra config files needed.
```bash
# 1. Start the stack
cd cookbook/observability
docker compose up -d
docker compose up -d --build

# 2. Make sure Twinkle has the OTLP exporter
pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp
Expand All @@ -40,10 +40,21 @@ open http://localhost:3000

Anonymous viewer access is on by default; full access is `admin` / `admin`.

To build just the LGTM image, run this command from the repository root.

```sh
docker build -t twinkle-lgtm:0.28.0 cookbook/observability
```

Build the CUDA Twinkle Server image from the repository root with
`docker build --platform linux/amd64 -t twinkle-server:local .`. The build
installs the checkout's source and packages its cookbook startup scripts.
Check out the target branch or tag before building a release image.

The provisioned **Twinkle / Twinkle Server Overview** dashboard shows:

- HTTP request rate and P95 latency per deployment (Gateway / Model / Sampler / Processor)
- Active resources (sessions, models, sampling sessions, futures) — reported by
- Active resources and retained futures — reported by
the cleanup-leader worker only, so the gauges read absolute values rather
than the count multiplied by the worker fleet size
- Task queue depth, execution P95, wait-time P95
Expand Down Expand Up @@ -85,14 +96,51 @@ the gauge value directly.
docker compose down -v # -v also removes the named volume
```

## Production note

The LGTM all-in-one image is **for local development and demos**. Each backend
runs single-instance and shares one volume. For production, deploy each
component (Mimir / Tempo / Loki / Grafana) separately with proper persistent
storage, replicas, and an OTel Collector tier in front. The OTLP endpoint and
metric names stay the same, so your `server_config.yaml` and dashboards
transfer without changes.
## Dedicated monitoring host

`docker-compose.production.yaml` runs Collector, Tempo, Loki, Mimir, and Grafana
as separate services on one internal CPU host. Place Docker's named volumes on
supported persistent block storage with capacity for 30 days of telemetry. Use
the platform's approved object storage configuration if the storage available
for telemetry is NFS. Keep Mimir's TSDB and component working directories on
supported local block storage.

1. Provision a private OTLP listener and a Grafana listener on the administrator
network. Allow TCP 4317 from Runtime and TCP 3000 from the managed network only.
Keep the business Redis and the three backends on internal networks.
2. Have the Redis administrator provision a dedicated account for GET on the
three snapshot keys in the operations guide. Use the same Redis database and
key prefix as Runtime.
3. Copy `production/usage-limits.example.json` to deployment configuration.
Set default rates and optional overrides keyed by the complete fingerprint.
Render the dashboard with the existing Runtime Redis key prefix:

```sh
python3 production/render_dashboard.py /path/to/usage-limits.json /path/to/twinkle-multitenant.json \
--redis-key-prefix '<runtime persistence.key_prefix>'
```

4. Set `OBS_OTLP_BIND_IP`, `OBS_GRAFANA_BIND_IP`, `OBS_DASHBOARD_FILE` (the
absolute output path above), `OBS_REDIS_URL` (address without credentials),
and `OBS_REDIS_USER`. Inject `OBS_REDIS_PASSWORD`, `OBS_GRAFANA_USER`, and
`OBS_GRAFANA_PASSWORD` via deployment configuration. The Grafana image
installs the pinned Redis data source plugin. For an offline host, stage the
signed ARM64 or AMD64 plugin in Grafana's plugin directory before starting.

```sh
docker compose -f docker-compose.production.yaml up -d
```

5. Merge `production/server_config.yaml` into the existing Runtime config,
select the internal Collector address, and inject the same
`TWINKLE_TELEMETRY_USER_ID_KEY` into every Runtime worker using a deployment
secret. With the Megatron container entrypoint, pass
`--server-config /path/to/deployment.yaml` to load the resulting config.
The entrypoint supervises Runtime independently of the monitoring stack.
Confirm all three signals arrive.

See the [multitenant operations guide](../../docs/source_en/Usage%20Guide/Server%20and%20Client/Multitenant-Operations.md)
for Redis ACLs, credential handling, dashboard queries, and release checks.

## Troubleshooting

Expand Down
69 changes: 69 additions & 0 deletions cookbook/observability/docker-compose.production.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Run on the internal monitoring host. Only Grafana and the OTLP receiver bind host ports.
services:
collector:
image: otel/opentelemetry-collector-contrib:0.132.0
command: ["--config=/etc/collector.yaml"]
restart: unless-stopped
ports:
- "${OBS_OTLP_BIND_IP:?Set the private OTLP IP}:4317:4317"
volumes:
- ./production/collector.yaml:/etc/collector.yaml:ro

mimir:
image: grafana/mimir:2.17.1
command: ["-config.file=/etc/mimir.yaml"]
restart: unless-stopped
volumes:
- ./production/mimir.yaml:/etc/mimir.yaml:ro
- mimir:/data

loki:
image: grafana/loki:3.5.5
command: ["-config.file=/etc/loki.yaml"]
restart: unless-stopped
volumes:
- ./production/loki.yaml:/etc/loki.yaml:ro
- loki:/loki

tempo:
image: grafana/tempo:2.8.2
command: ["-config.file=/etc/tempo.yaml"]
restart: unless-stopped
volumes:
- ./production/tempo.yaml:/etc/tempo.yaml:ro
- tempo:/var/tempo

grafana:
image: grafana/grafana:12.1.1
restart: unless-stopped
ports:
- "${OBS_GRAFANA_BIND_IP:?Set the managed Grafana IP}:3000:3000"
environment:
GF_AUTH_ANONYMOUS_ENABLED: "false"
GF_USERS_ALLOW_SIGN_UP: "false"
GF_SECURITY_ADMIN_USER: "${OBS_GRAFANA_USER:?Set the Grafana admin user}"
GF_SECURITY_ADMIN_PASSWORD: "${OBS_GRAFANA_PASSWORD:?Set the Grafana admin password}"
GF_SECURITY_DISABLE_GRAVATAR: "true"
GF_ANALYTICS_REPORTING_ENABLED: "false"
GF_ANALYTICS_CHECK_FOR_UPDATES: "false"
GF_ANALYTICS_CHECK_FOR_PLUGIN_UPDATES: "false"
GF_PLUGINS_PREINSTALL_SYNC: "redis-datasource@2.2.0"
OBS_REDIS_URL: "${OBS_REDIS_URL:?Set the internal Redis address}"
OBS_REDIS_USER: "${OBS_REDIS_USER:?Set the dedicated Redis username}"
OBS_REDIS_PASSWORD: "${OBS_REDIS_PASSWORD:?Set the dedicated Redis password}"
volumes:
- grafana:/var/lib/grafana
- ./production/datasources.yaml:/etc/grafana/provisioning/datasources/twinkle.yaml:ro
- ./production/dashboards.yaml:/etc/grafana/provisioning/dashboards/twinkle.yaml:ro
- type: bind
source: "${OBS_DASHBOARD_FILE:?Render the deployment dashboard first}"
target: /var/lib/twinkle-dashboards/twinkle-multitenant.json
read_only: true
bind:
create_host_path: false

volumes:
mimir:
loki:
tempo:
grafana:
Loading
Loading