feat: Add opt-in OpenTelemetry auto-instrumentation to Docker images

Builds on #4127 by adding OpenTelemetry auto-instrumentation support to Docker images. After #4127 migrated to automatic instrumentation, the Docker images lacked the necessary dependencies. This PR installs the OTEL packages and enables instrumentation when any OTEL_* environment variable is set.

Test Plan:

Build image:
docker build -f containers/Containerfile   --build-arg DISTRO_NAME=starter   --build-arg INSTALL_MODE=editable   --tag llamastack/distribution-starter:otel-test .

Run with trace propagation enabled (parentbased_traceidratio with 0.0 prevents new traces but allows propagation of incoming traces):
docker run -p 8321:8321   -e OTEL_EXPORTER_OTLP_ENDPOINT=http://host.docker.internal:4318   -e OTEL_SERVICE_NAME=llama-stack   -e OTEL_TRACES_SAMPLER=parentbased_traceidratio   -e OTEL_TRACES_SAMPLER_ARG=0.0   llamastack/distribution-starter:otel-test

Ran a sample flight search agent. Traces successfully captured.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Adrian Cole 2025-12-03 05:23:26 +08:00
parent e243892ef0
commit 3325f59c7b

View file

@ -120,6 +120,11 @@ RUN set -eux; \
printf '%s\n' "$deps" | xargs -L1 uv pip install --no-cache; \ printf '%s\n' "$deps" | xargs -L1 uv pip install --no-cache; \
fi fi
# Install OpenTelemetry auto-instrumentation support
RUN set -eux; \
pip install --no-cache opentelemetry-distro opentelemetry-exporter-otlp; \
opentelemetry-bootstrap -a install
# Cleanup # Cleanup
RUN set -eux; \ RUN set -eux; \
pip uninstall -y uv; \ pip uninstall -y uv; \
@ -135,15 +140,21 @@ RUN cat <<'EOF' >/usr/local/bin/llama-stack-entrypoint.sh
#!/bin/sh #!/bin/sh
set -e set -e
# Enable OpenTelemetry auto-instrumentation if any OTEL_* variable is set
CMD_PREFIX=""
if env | grep -q '^OTEL_'; then
CMD_PREFIX="opentelemetry-instrument"
fi
if [ -n "$RUN_CONFIG_PATH" ] && [ -f "$RUN_CONFIG_PATH" ]; then if [ -n "$RUN_CONFIG_PATH" ] && [ -f "$RUN_CONFIG_PATH" ]; then
exec llama stack run "$RUN_CONFIG_PATH" "$@" exec $CMD_PREFIX llama stack run "$RUN_CONFIG_PATH" "$@"
fi fi
if [ -n "$DISTRO_NAME" ]; then if [ -n "$DISTRO_NAME" ]; then
exec llama stack run "$DISTRO_NAME" "$@" exec $CMD_PREFIX llama stack run "$DISTRO_NAME" "$@"
fi fi
exec llama stack run "$@" exec $CMD_PREFIX llama stack run "$@"
EOF EOF
RUN chmod +x /usr/local/bin/llama-stack-entrypoint.sh RUN chmod +x /usr/local/bin/llama-stack-entrypoint.sh