Simplify OTEL wrapper helper and fix shell quoting

Collapse `otel_prefix` and `otel_prefix_str` into a single nameref-based
helper. Watchfiles call sites embed the array as a shell-quoted prefix
via `${wrap[*]@Q}`, which also fixes a quoting bug where an
`OTEL_SERVICE_NAME_PREFIX` containing a single quote would produce an
invalid command string and break the watcher.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-05-24 14:57:52 -04:00
parent f434fde772
commit a4f1f516bc

View File

@@ -62,33 +62,27 @@ error_log() {
exit 1
}
# Emit the opentelemetry-instrument wrapper as NUL-delimited argv tokens, or
# nothing if OTEL is disabled or the wrapper binary is missing.
# Populate a caller-provided array with the opentelemetry-instrument wrapper
# argv tokens for service "$2", or leave it empty if OTEL is disabled or the
# wrapper binary is missing. Use "${arr[@]}" to exec directly, or
# "${arr[*]@Q}" to embed as a shell-quoted prefix string.
otel_prefix() {
local service="$1"
local -n out_arr="$1"
# shellcheck disable=SC2034 # nameref binds out_arr to caller variable
out_arr=()
if [[ ${OTEL_SDK_DISABLED:-false} == "true" ]]; then return 0; fi
if ! command -v opentelemetry-instrument >/dev/null 2>&1; then
# stderr so it doesn't pollute the captured argv tokens
warn_log "opentelemetry-instrument not found, starting ${service} without OpenTelemetry instrumentation" >&2
warn_log "opentelemetry-instrument not found, starting $2 without OpenTelemetry instrumentation"
return 0
fi
printf '%s\0' opentelemetry-instrument --service_name "${OTEL_SERVICE_NAME_PREFIX-}${service}"
}
# Emit the opentelemetry-instrument wrapper as a shell-string prefix (with
# trailing space), for embedding inside `watchfiles --target-type command`.
otel_prefix_str() {
local service="$1"
if [[ ${OTEL_SDK_DISABLED:-false} == "true" ]] || ! command -v opentelemetry-instrument >/dev/null 2>&1; then
return 0
fi
printf "opentelemetry-instrument --service_name '%s%s' " "${OTEL_SERVICE_NAME_PREFIX-}" "${service}"
# shellcheck disable=SC2034 # nameref binds out_arr to caller variable
out_arr=(opentelemetry-instrument --service_name "${OTEL_SERVICE_NAME_PREFIX-}$2")
}
# Commands to run initial startup tasks
run_startup() {
local -a wrap=()
mapfile -d '' wrap < <(otel_prefix startup)
otel_prefix wrap startup
if ! PYTHONPATH="/backend:${PYTHONPATH-}" "${wrap[@]}" python3 /backend/startup.py; then
error_log "Startup script failed, exiting"
fi
@@ -131,7 +125,7 @@ start_bin_gunicorn() {
export PYTHONDONTWRITEBYTECODE=1
local -a wrap=()
mapfile -d '' wrap < <(otel_prefix api)
otel_prefix wrap api
"${wrap[@]}" gunicorn \
--bind=0.0.0.0:"${DEV_PORT:-5000}" \
--bind=unix:/tmp/gunicorn.sock \
@@ -252,9 +246,11 @@ start_bin_rq_worker() {
start_bin_watcher() {
info_log "Starting watcher"
local -a wrap=()
otel_prefix wrap watcher
watchfiles \
--target-type command \
"$(otel_prefix_str watcher)python3 watcher.py" \
"${wrap[*]@Q} python3 watcher.py" \
/romm/library &
WATCHER_PID=$!
echo "${WATCHER_PID}" >/tmp/watcher.pid
@@ -264,9 +260,11 @@ start_bin_sync_watcher() {
info_log "Starting sync folder watcher"
sync_base_path="${ROMM_BASE_PATH:-/romm}/sync"
mkdir -p "${sync_base_path}"
local -a wrap=()
otel_prefix wrap sync_watcher
watchfiles \
--target-type command \
"$(otel_prefix_str sync_watcher)python3 sync_watcher.py" \
"${wrap[*]@Q} python3 sync_watcher.py" \
"${sync_base_path}" &
SYNC_WATCHER_PID=$!
echo "${SYNC_WATCHER_PID}" >/tmp/sync_watcher.pid