Skip to content

Commit

Permalink
ci: enable exception replay in Django profile job (#11014)
Browse files Browse the repository at this point in the history
We enable Exception Replay in the Django profile workflow and create a
scenario that leads to an exception to profile the relevant code paths.

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [x] Reviewer has checked that all the criteria below are met
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)

(cherry picked from commit 34d69be)
  • Loading branch information
P403n1x87 authored and github-actions[bot] committed Jan 15, 2025
1 parent 03708c7 commit 9659c3d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 46 deletions.
1 change: 1 addition & 0 deletions .github/workflows/django-overhead-profile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
DD_PROFILING_ENABLED: "1"
DD_PROFILING_STACK_V2_ENABLED: ${{ matrix.stack_v2 }}
DD_PROFILING_OUTPUT_PPROF: ${{ github.workspace }}/prefix/artifacts/ddtrace_profile
DD_EXCEPTION_REPLAY_ENABLED: "1"
defaults:
run:
working-directory: ddtrace
Expand Down
10 changes: 10 additions & 0 deletions scripts/profiles/django-simple/k6-exc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import http from 'k6/http';

export const options = {
duration: '60s',
vus: 100,
};

export default function () {
const res = http.get('http://127.0.0.1:8080/polls/123/results/');
}
3 changes: 1 addition & 2 deletions scripts/profiles/django-simple/k6-load.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import http from 'k6/http';
import { sleep } from 'k6';

export const options = {
duration: '60s',
vus: 100,
};

export default function () {
const res = http.get('http://127.0.0.1:8000/accounts/signup/');
const res = http.get('http://127.0.0.1:8080/polls/123/vote/');
}
100 changes: 62 additions & 38 deletions scripts/profiles/django-simple/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,83 @@ AUSTIN_EXPOSURE=5 # sec

test -f ${PREFIX}/gunicorn.pid && (kill -9 `cat ${PREFIX}/gunicorn.pid` ; sleep 3) || rm -f ${PREFIX}/gunicorn.pid
pkill k6 || true
test -d ${PREFIX}/artifacts && rm -rf ${PREFIX}/artifacts || mkdir -p ${PREFIX}/artifacts
pkill -9 -f uwsgi || true
test -d ${PREFIX}/artifacts && rm -rf ${PREFIX}/artifacts
mkdir -p ${PREFIX}/artifacts

sudo echo "sudo OK"

sudo rm -f ${PREFIX}/uwsgi.pid

function profile_with_load {
name=${1}
scenario=${2}

echo "- profiling for ${name}"

sleep 3
${PREFIX}/k6*/k6 run --quiet scripts/profiles/django-simple/k6-load.js &
echo "Starting load"
${PREFIX}/k6*/k6 run --quiet scripts/profiles/django-simple/k6-${scenario}.js &
sleep 2
sudo `which austin` -bsCi ${AUSTIN_INTERVAL} -o ${PREFIX}/artifacts/${name}.mojo -p `cat ${PREFIX}/gunicorn.pid` -x ${AUSTIN_EXPOSURE}
LC_ALL=C sed -i 's|/home/runner/work/dd-trace-py/dd-trace-py/ddtrace/||g' ${PREFIX}/artifacts/${name}.mojo
echo "Attaching Austin to $(cat ${PREFIX}/uwsgi.pid)"
sudo `which austin` -bsCi ${AUSTIN_INTERVAL} -o ${PREFIX}/artifacts/${scenario}_${name}.mojo -p `cat ${PREFIX}/uwsgi.pid` -x ${AUSTIN_EXPOSURE}
LC_ALL=C sed -i 's|/home/runner/work/dd-trace-py/dd-trace-py/ddtrace/||g' ${PREFIX}/artifacts/${scenario}_${name}.mojo
echo "Stopping load"
pkill k6
}

source ${PREFIX}/bin/activate

export DJANGO_SETTINGS_MODULE="config.settings.production"
export DJANGO_ALLOWED_HOSTS="127.0.0.1"
export DJANGO_SECRET_KEY="SECRET_KEY"
export DATABASE_URL="sqlite:///django.db"
export DJANGO_ALLOWED_HOSTS="127.0.0.1"
export DEVELOPMENT_MODE=True

# Tag traces with HTTP headers to benchmark the related code
export DD_TRACE_HEADER_TAGS="User-Agent:http.user_agent,Referer:http.referer,Content-Type:http.content_type,Etag:http.etag"

# Baseline
pushd ${PREFIX}/trace-examples/python/django/django-simple
gunicorn config.wsgi --pid ${PREFIX}/gunicorn.pid > /dev/null &
echo "Done"
popd
profile_with_load "baseline"
kill $(cat ${PREFIX}/gunicorn.pid)

pushd ${PREFIX}/trace-examples/python/django/django-simple
ddtrace-run gunicorn config.wsgi --pid ${PREFIX}/gunicorn.pid > /dev/null &
popd
profile_with_load "head"
kill $(cat ${PREFIX}/gunicorn.pid)

sudo chown -R $(id -u):$(id -g) ${PREFIX}/artifacts/*

echo -n "Converting MOJO to Austin ... "
mojo2austin ${PREFIX}/artifacts/head.mojo ${PREFIX}/artifacts/head.austin.tmp
mojo2austin ${PREFIX}/artifacts/baseline.mojo ${PREFIX}/artifacts/baseline.austin.tmp
echo "[done]"

echo -n "Diffing ... "
python scripts/diff.py \
${PREFIX}/artifacts/head.austin.tmp \
${PREFIX}/artifacts/baseline.austin.tmp \
${PREFIX}/artifacts/baseline_head.diff
echo "[done]"

rm ${PREFIX}/artifacts/*.austin.tmp

head -n 25 ${PREFIX}/artifacts/baseline_head.diff.top

function run_scenario {
scenario=${1}

echo "Running scenario ${scenario}"

# Baseline
pushd ${PREFIX}/trace-examples/python/django/sample-django
uwsgi --http :8080 --enable-threads --module mysite.wsgi --pidfile ${PREFIX}/uwsgi.pid 2> /dev/null &
echo "Done"
popd
profile_with_load "baseline" ${scenario}
echo "Stopping uwsgi"
uwsgi --stop ${PREFIX}/uwsgi.pid
pkill -9 -f uwsgi || true

pushd ${PREFIX}/trace-examples/python/django/sample-django
uwsgi --http :8080 --enable-threads --module mysite.wsgi --pidfile ${PREFIX}/uwsgi.pid --import=ddtrace.bootstrap.sitecustomize 2> /dev/null &
popd
profile_with_load "head" ${scenario}
echo "Stopping uwsgi"
uwsgi --stop ${PREFIX}/uwsgi.pid
pkill -9 -f uwsgi || true

sudo chown -R $(id -u):$(id -g) ${PREFIX}/artifacts/*

echo -n "Converting MOJO to Austin ... "
mojo2austin ${PREFIX}/artifacts/${scenario}_head.mojo ${PREFIX}/artifacts/${scenario}_head.austin.tmp
mojo2austin ${PREFIX}/artifacts/${scenario}_baseline.mojo ${PREFIX}/artifacts/${scenario}_baseline.austin.tmp
echo "[done]"

echo -n "Diffing ... "
python scripts/diff.py \
${PREFIX}/artifacts/${scenario}_head.austin.tmp \
${PREFIX}/artifacts/${scenario}_baseline.austin.tmp \
${PREFIX}/artifacts/${scenario}_baseline_head.diff
echo "[done]"

rm ${PREFIX}/artifacts/*.austin.tmp

head -n 25 ${PREFIX}/artifacts/${scenario}_baseline_head.diff.top
}


run_scenario "load"
run_scenario "exc"
10 changes: 4 additions & 6 deletions scripts/profiles/django-simple/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ PREFIX=${1}
AUSTIN_VERSION="3.6"
K6_VERSION="0.26.2"

export DJANGO_SETTINGS_MODULE="config.settings.production"
export DJANGO_ALLOWED_HOSTS="127.0.0.1"
export DJANGO_SECRET_KEY="SECRET_KEY"
export DATABASE_URL="sqlite:///django.db"

# Clean up existing installation
Expand All @@ -26,12 +24,12 @@ source ${PREFIX}/bin/activate
pip install pip --upgrade

# Install the application
git clone https://github.com/DataDog/trace-examples.git ${PREFIX}/trace-examples
test -d ${PREFIX}/trace-examples || git clone -b sample-django --single-branch https://github.com/DataDog/trace-examples.git ${PREFIX}/trace-examples
pushd ${PREFIX}/trace-examples/
git checkout origin/django-simple
pushd python/django/django-simple
pip install -r requirements/production.txt
pushd python/django/sample-django
pip install -r requirements.txt
python manage.py migrate
python manage.py collectstatic
popd
popd

Expand Down

0 comments on commit 9659c3d

Please sign in to comment.