Skip to content

Commit 5a7478c

Browse files
codefromthecryptr-bit-rry
authored andcommitted
feat: Add opt-in OpenTelemetry auto-instrumentation to Docker images (llamastack#4281)
# What does this PR do? This allows llama-stack users of the Docker image to use OpenTelemetry like previous versions. llamastack#4127 migrated to automatic instrumentation, but unless we add those libraries to the image, everyone needs to build a custom image to enable otel. Also, unless we establish a convention for enabling it, users who formerly just set config now need to override the entrypoint. This PR bootstraps OTEL packages, so they are available (only +10MB). It also prefixes `llama stack run` with `opentelemetry-instrument` when any `OTEL_*` environment variable is set. The result is implicit tracing like before, where you don't need a custom image to use traces or metrics. ## Test Plan ```bash # 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 OTEL env to implicitly use `opentelemetry-instrument`. The # Settings below ensure inbound traces are honored, but no # "junk traces" like SQL connects are created. 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 which is instrumented on the client side. This and llama-stack target [otel-tui](https://github.com/ymtdzzz/otel-tui) I verified no root database spans, yet database spans are attached to incoming traces. <img width="1608" height="742" alt="screenshot" src="https://github.com/user-attachments/assets/69f59b74-3054-42cd-947d-a6c0d9472a7c" /> Signed-off-by: Adrian Cole <[email protected]>
1 parent 952fa2a commit 5a7478c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

containers/Containerfile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ RUN set -eux; \
120120
printf '%s\n' "$deps" | xargs -L1 uv pip install --no-cache; \
121121
fi
122122

123+
# Install OpenTelemetry auto-instrumentation support
124+
RUN set -eux; \
125+
pip install --no-cache opentelemetry-distro opentelemetry-exporter-otlp; \
126+
opentelemetry-bootstrap -a install
127+
123128
# Cleanup
124129
RUN set -eux; \
125130
pip uninstall -y uv; \
@@ -135,15 +140,21 @@ RUN cat <<'EOF' >/usr/local/bin/llama-stack-entrypoint.sh
135140
#!/bin/sh
136141
set -e
137142

143+
# Enable OpenTelemetry auto-instrumentation if any OTEL_* variable is set
144+
CMD_PREFIX=""
145+
if env | grep -q '^OTEL_'; then
146+
CMD_PREFIX="opentelemetry-instrument"
147+
fi
148+
138149
if [ -n "$RUN_CONFIG_PATH" ] && [ -f "$RUN_CONFIG_PATH" ]; then
139-
exec llama stack run "$RUN_CONFIG_PATH" "$@"
150+
exec $CMD_PREFIX llama stack run "$RUN_CONFIG_PATH" "$@"
140151
fi
141152

142153
if [ -n "$DISTRO_NAME" ]; then
143-
exec llama stack run "$DISTRO_NAME" "$@"
154+
exec $CMD_PREFIX llama stack run "$DISTRO_NAME" "$@"
144155
fi
145156

146-
exec llama stack run "$@"
157+
exec $CMD_PREFIX llama stack run "$@"
147158
EOF
148159
RUN chmod +x /usr/local/bin/llama-stack-entrypoint.sh
149160

0 commit comments

Comments
 (0)