fix(sdk): use sys.orig_argv for process.command to handle python -m invocations#5083
Open
alliasgher wants to merge 2 commits intoopen-telemetry:mainfrom
Open
fix(sdk): use sys.orig_argv for process.command to handle python -m invocations#5083alliasgher wants to merge 2 commits intoopen-telemetry:mainfrom
alliasgher wants to merge 2 commits intoopen-telemetry:mainfrom
Conversation
…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>
MikeGoldsmith
requested changes
Apr 13, 2026
Member
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
approved these changes
Apr 14, 2026
Member
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.
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>
6db2e8a to
0fea141
Compare
xrmx
reviewed
Apr 14, 2026
| Resource({"service.name": "from-service-name"}), | ||
| ) | ||
|
|
||
| @patch( |
| tuple(sys.orig_argv), | ||
| ) | ||
|
|
||
| @patch("sys.argv", ["/path/to/myapp/__main__.py"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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