Skip to content

Commit

Permalink
Merge pull request #1123 from vfreex/fix-env-vars-for-trace-context
Browse files Browse the repository at this point in the history
Fix env var propagation with telemetry enabled
  • Loading branch information
openshift-merge-bot[bot] authored Nov 15, 2024
2 parents ca4ed4c + ef5acde commit 2946ab9
Showing 1 changed file with 11 additions and 6 deletions.
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

0 comments on commit 2946ab9

Please sign in to comment.