From 6ce6c62cb023f699c01d5feed71c560d8b967216 Mon Sep 17 00:00:00 2001 From: Patrick Huck Date: Fri, 5 Apr 2024 17:45:43 -0700 Subject: [PATCH] optional datadog --- emmet-api/Dockerfile | 30 +++++++++++++++--------------- emmet-api/start.sh | 34 +++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/emmet-api/Dockerfile b/emmet-api/Dockerfile index bd274fdacd..a3788dd841 100644 --- a/emmet-api/Dockerfile +++ b/emmet-api/Dockerfile @@ -8,14 +8,14 @@ WORKDIR /emmet-api ENV PIP_FLAGS "--user --no-cache-dir --compile" COPY requirements/deployment.txt ./requirements.txt RUN pip install $PIP_FLAGS --upgrade pip pip-tools setuptools-scm && \ - pip-sync requirements.txt --pip-args "$PIP_FLAGS" + pip-sync requirements.txt --pip-args "$PIP_FLAGS" COPY emmet emmet COPY setup.py . ARG API_VERSION RUN SETUPTOOLS_SCM_PRETEND_VERSION=${API_VERSION} pip install $PIP_FLAGS --no-deps . RUN wget -q https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh && \ - chmod +x wait-for-it.sh && mv wait-for-it.sh /root/.local/bin/ + chmod +x wait-for-it.sh && mv wait-for-it.sh /root/.local/bin/ FROM base COPY --from=builder /root/.local/lib/python3.10/site-packages /root/.local/lib/python3.10/site-packages @@ -25,18 +25,18 @@ COPY --from=builder /emmet-api /emmet-api WORKDIR /emmet-api ARG VERSION ENV PATH=/root/.local/bin:$PATH \ - PYTHONUNBUFFERED=1 \ - FLASK_APP=emmet-api \ - FLASK_ENV=production \ - PORT=10001 \ - NUM_WORKERS=4 \ - RELOAD="" \ - MAX_REQUESTS=0 \ - MAX_REQUESTS_JITTER=0 \ - DD_TRACE_HOST=localhost:8126 \ - DD_SERVICE=next-gen-api \ - DD_ENV=prod \ - DD_VERSION=$VERSION + PYTHONUNBUFFERED=1 \ + FLASK_APP=emmet-api \ + FLASK_ENV=production \ + PORT=10001 \ + NUM_WORKERS=4 \ + RELOAD="" \ + MAX_REQUESTS=0 \ + MAX_REQUESTS_JITTER=0 \ + DD_TRACE_HOST=localhost:8126 \ + DD_SERVICE=next-gen-api \ + DD_ENV=prod \ + DD_VERSION=$VERSION COPY app.py . COPY material_resources.py . @@ -47,4 +47,4 @@ RUN chmod +x start.sh LABEL com.datadoghq.ad.logs='[{"source": "gunicorn", "service": "next-gen-api"}]' EXPOSE 10001 20001 -CMD wait-for-it.sh $DD_TRACE_HOST -q -s -t 60 -- ./start.sh +CMD ./start.sh diff --git a/emmet-api/start.sh b/emmet-api/start.sh index a070a9657a..5765190a30 100644 --- a/emmet-api/start.sh +++ b/emmet-api/start.sh @@ -1,11 +1,27 @@ -#!/bin/bash -x +#!/bin/bash +set -e pmgrc=$HOME/.pmgrc.yaml -[[ ! -e $pmgrc ]] && echo "PMG_DUMMY_VAR: dummy" > $pmgrc - -exec ddtrace-run gunicorn --statsd-host $DD_AGENT_HOST:8125 \ - -b 0.0.0.0:$PORT -k uvicorn.workers.UvicornWorker -w $NUM_WORKERS \ - --access-logfile - --error-logfile - $RELOAD \ - --max-requests $MAX_REQUESTS --max-requests-jitter $MAX_REQUESTS_JITTER \ - --timeout 120 \ - app:app +[[ ! -e $pmgrc ]] && echo "PMG_DUMMY_VAR: dummy" >$pmgrc + +STATS_ARG="" + +if [[ -n "$DD_TRACE_HOST" ]]; then + wait-for-it.sh $DD_TRACE_HOST -q -s -t 10 && STATS_ARG="--statsd-host $DD_AGENT_HOST:8125" +fi + +SERVER_APP="app:app" +BIND_ARG="-b 0.0.0.0:$PORT" +WORKER_ARGS="-k uvicorn.workers.UvicornWorker -w $NUM_WORKERS" +LOG_ARGS="--access-logfile - --error-logfile - $RELOAD" +REQS_ARGS="--max-requests $MAX_REQUESTS --max-requests-jitter $MAX_REQUESTS_JITTER" +OTHER_ARGS="--timeout 120" +MAIN_ARGS="$BIND_ARG $WORKER_ARGS $LOG_ARGS $REQS_ARGS $OTHER_ARGS" + +ACCESS_LOG_FORMAT=(--access-logformat '%(h)s %(t)s %(m)s %(U)s?%(q)s %(H)s %(s)s %(b)s "%(f)s" "%(a)s" %(D)s %(p)s %({x-consumer-id}i)s %({x-callback-name}o)s %({x-consumer-groups}o)s') + +if [[ -n "$STATS_ARG" ]]; then + exec ddtrace-run gunicorn $STATS_ARG $MAIN_ARGS "${ACCESS_LOG_FORMAT[@]}" $SERVER_APP +else + exec gunicorn $MAIN_ARGS "${ACCESS_LOG_FORMAT[@]}" $SERVER_APP +fi