Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix env var propagation with telemetry enabled #1123

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions artcommon/artcommonlib/exectools.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,21 +464,24 @@ async def cmd_gather_async(cmd: Union[List[str], str], check: bool = True, **kwa
span = trace.get_current_span()
span.set_attribute("pyartcd.param.cmd", cmd_list)

logger.info("Executing:cmd_gather_async %s", cmd_list)
# capture stdout and stderr if they are not set in kwargs
if "stdout" not in kwargs:
kwargs["stdout"] = asyncio.subprocess.PIPE
if "stderr" not in kwargs:
kwargs["stderr"] = asyncio.subprocess.PIPE

# Propagate trace context to subprocess
env = kwargs.get("env", {})
carrier = {}
TraceContextTextMapPropagator().inject(carrier)
if "traceparent" in carrier:
env = kwargs.get("env")
if env is None:
# Per Popen doc, a None env means "inheriting the current process’ environment".
# To inject the trace context, we need to copy the current environment.
env = kwargs["env"] = os.environ.copy()
env["TRACEPARENT"] = carrier["traceparent"]
kwargs["env"] = env

logger.info("Executing:cmd_gather_async %s", cmd_list)
proc = await asyncio.subprocess.create_subprocess_exec(cmd_list[0], *cmd_list[1:], **kwargs)
stdout, stderr = await proc.communicate()
stdout = stdout.decode() if stdout else ""
Expand Down Expand Up @@ -511,13 +514,15 @@ async def cmd_assert_async(cmd: Union[List[str], str], check: bool = True, **kwa
span.set_attribute("pyartcd.param.cmd", cmd_list)

# Propagate trace context to subprocess
env = kwargs.get("env", {})
carrier = {}
TraceContextTextMapPropagator().inject(carrier)
if "traceparent" in carrier:
env = kwargs.get("env")
if env is None:
# Per Popen doc, a None env means "inheriting the current process’ environment".
# To inject the trace context, we need to copy the current environment.
env = kwargs["env"] = os.environ.copy()
env["TRACEPARENT"] = carrier["traceparent"]
logger.warning("Pass TRACEPARENT %s", env["TRACEPARENT"])
kwargs["env"] = env

logger.info("Executing:cmd_assert_async %s", cmd_list)
proc = await asyncio.subprocess.create_subprocess_exec(cmd_list[0], *cmd_list[1:], **kwargs)
Expand Down