I'm trying to get a pex binary to run ddtrace-run ...
# general
a
I'm trying to get a pex binary to run ddtrace-run on a file bundled in the pex but can't work out how to do this. If I set the script to ddtrace-run, when I try and invoke the pex (./foo.pex python bar/baz.py" it can't find the python file as the path is when I ran the pex. I've set the inherit_path to prefer and execution_mode to venv but this doesn't seem to help.
r
Have you tried setting PEX_MODULE=<path_to_the_module>? This is how I defined a pex_binary
Copy code
python_sources(name="sources")
pex_binary(
    name="custom_name", layout="packed", execution_mode="venv", dependencies=[":sources"]
)
Then,
Copy code
PEX_MODULE=<your.module.path> python custom_name.pex
a
I've given that a go but I am not sure it works, I think that just means the
python bar/baz.py
file gets run but not via the ddtrace-run script
e
@average-flag-94768 is this the primary purpose of the PEX or are you trying to run this as a debug command for a PEX that happens to include ddtrace-run? And what is
ddtrace-run
? Is it a console script from here?: https://pypi.org/project/ddtrace/
a
ddtrace run is a script bundled from that ddtrace package that instruments you code for use with datadog. Without it the code will run fine but without instrumentation.
e
Ok, and is this the primary purpose or secondary for the PEX
A PEX can have 18 entrypoints, 1 default.
So, skipping that question, a PEX can act like a Python interpreter. It does this by default if it has no entrypoint, but that can also be forced with
PEX_INTERPRETER=1 my.pex ...
I.E. You should be able to substiture that for
python
in
ddtrace-run python ...
a
Hmm I think there is something odd going on as it works fine for a different pex target that uses it to run gunicorn. However trying to run it as an interpreter just gives me a
FileNotFoundError: [Errno 2] No such file or directory: 'ddtrace-run/__main__.py'
error
e
Copy code
jsirois@Gill-Windows:/tmp/ddtrace $ tree src/
src/
└── foo
    ├── bar.py
    └── __init__.py

1 directory, 2 files
jsirois@Gill-Windows:/tmp/ddtrace $ pex ddtrace -oddtrace.pex -Dsrc --venv prepend
jsirois@Gill-Windows:/tmp/ddtrace $ PEX_SCRIPT=ddtrace-run ./ddtrace.pex ddtrace.pex -mfoo.bar
Re-executing with Python interpreter options: cmdline='/home/jsirois/.pex/venvs/c95dc24c3231afbe2b619c0013a8def377be1e72/5985ed09b49a653d6596b0e14d134c5456cf1a9f/bin/python -mfoo.bar /home/jsirois/.pex/venvs/c95dc24c3231afbe2b619c0013a8def377be1e72/5985ed09b49a653d6596b0e14d134c5456cf1a9f/pex -mfoo.bar'
Hello baz!
So that works. Not sure if that style works for you.
Basically, that's
ddtrace-run <pex-instead-of-python> <-m module instead of py file>
.
Basically you will never get referencing a py file in the PEX to work without a custom main.
The
-m
avoids that.
a
Yeah that style works for me, it's only ever called like that in a kube deployment anyway.
e
Great.
a
Thanks for the help I really appreciate it
e
You're welcome.