Skip to content

Commit

Permalink
optional datadog
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Apr 6, 2024
1 parent 1ec72fe commit 6ce6c62
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
30 changes: 15 additions & 15 deletions emmet-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 .
Expand All @@ -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
34 changes: 25 additions & 9 deletions emmet-api/start.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6ce6c62

Please sign in to comment.