fix(sdk): use sys.orig_argv for process.command to handle python -m invocations#5083
fix(sdk): use sys.orig_argv for process.command to handle python -m invocations#5083alliasgher wants to merge 3 commits intoopen-telemetry:mainfrom
Conversation
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Looks good, thanks @alliasgher. I've left a couple of small suggestions, with the main one being now the SDK requires python 3.10+, we don't need the fallback anymore.
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Got a little more clean-up needed to remove sys.argv but otherwise looks good.
6db2e8a to
0fea141
Compare
| tuple(sys.orig_argv), | ||
| ) | ||
|
|
||
| @patch("sys.argv", ["/path/to/myapp/__main__.py"]) |
There was a problem hiding this comment.
Yes, we need both mocks here. The whole point of test_process_detector_uses_orig_argv_for_python_m is to demonstrate the fix for the "python -m" scenario:
sys.argv[0]="/path/to/myapp/__main__.py"(what the interpreter rewrites it to)sys.orig_argv=["/usr/bin/python", "-m", "myapp"](the original command)
By mocking both, we test that the detector correctly prefers sys.orig_argv and produces process.command = "/usr/bin/python" and process.command_line = "/usr/bin/python -m myapp", which was the bug report in #4518.
There was a problem hiding this comment.
You mock sys.orig_argv and you assert that resource attributes matches it. If you don't mock sys.argv I'm pretty sure you won't get in it anything near your orig_argv mock and so if you mock it or not it shouldn't make any difference.
0fea141 to
72470e6
Compare
Since ProcessResourceDetector now prefers sys.orig_argv (Python 3.10+), the @patch('sys.argv', ...) decorator on test_process_detector is redundant — the detector reads from sys.orig_argv which is already patched. Remove it as suggested in review. Addresses xrmx's review comment on open-telemetry#5083. Signed-off-by: Ali <alliasgher123@gmail.com>
…nvocations ProcessResourceDetector populated process.command, process.command_line, and process.command_args from sys.argv. For applications launched via `python -m <module>`, the interpreter rewrites sys.argv[0] to the resolved module path, so the ``-m <module>`` portion of the original invocation is lost and the detector emits misleading telemetry. Python 3.10+ exposes sys.orig_argv which preserves the original arguments received by the interpreter. Since the SDK already requires Python >= 3.10, switch to sys.orig_argv (with a getattr fallback for safety). This also aligns with the OTel semantic conventions that reference /proc/<pid>/cmdline for these attributes. Fixes open-telemetry#4518 Signed-off-by: Ali <alliasgher123@gmail.com>
Two review nits from MikeGoldsmith: - sys.orig_argv has been available since Python 3.10; the SDK now requires 3.10+ so the getattr fallback is dead code. Use sys.orig_argv directly and update the comment. - CHANGELOG entries should reference the PR number, not the issue number. Signed-off-by: Ali <alliasgher123@gmail.com>
b110aa0 to
d110a9c
Compare
Signed-off-by: Ali <alliasgher123@gmail.com>
Description
ProcessResourceDetectorpopulatesprocess.command,process.command_line, andprocess.command_argsfromsys.argv. For applications launched viapython -m <module>, the interpreter rewritessys.argv[0]to the resolved__main__.pypath — so the ``-m `` portion of the original invocation is lost and the emitted telemetry is misleading (e.g.process.command = "-m"orprocess.command = "<path>/__main__.py"depending on when the detector runs).Python 3.10+ exposes
sys.orig_argvwhich preserves the arguments received by the interpreter. Sinceopentelemetry-sdkalready requires Python ≥ 3.10, this PR switches the detector to read fromsys.orig_argv(with agetattrfallback for safety). This also aligns with the OTel semantic conventions that reference/proc/<pid>/cmdlinefor these attributes.Fixes #4518
Behaviour change
python -m myappprocess.command = "-m"(or"<path>/__main__.py")process.command = "<python>",command_line = "<python> -m myapp"python myscript.pyprocess.command = "myscript.py"process.command = "<python>",command_line = "<python> myscript.py"Users inspecting
process.command_line/process.command_argswill see more accurate data.process.commandchanges to match/proc/<pid>/cmdline[0], which is what the OTel semantic conventions reference.Checklist
pytest opentelemetry-sdk/tests/resources/test_resources.pytest_process_detector_uses_orig_argv_for_python_mcovers the reported scenarioSigned-off-by: Ali alliasgher123@gmail.com