diff --git a/actions/setup/js/action_setup_otlp.cjs b/actions/setup/js/action_setup_otlp.cjs index c84b1760067..a7ff441d3e7 100644 --- a/actions/setup/js/action_setup_otlp.cjs +++ b/actions/setup/js/action_setup_otlp.cjs @@ -30,6 +30,20 @@ const { appendFileSync } = require("fs"); const { nowMs } = require("./performance_now.cjs"); const { getActionInput } = require("./action_input_utils.cjs"); +/** + * Append a key=value line to a GitHub Actions file (GITHUB_OUTPUT or GITHUB_ENV) + * if the file path is set and the value is truthy. + * @param {string | undefined} filePath - Path to the output/env file + * @param {string} key - The variable name + * @param {string} value - The value to write + * @param {string} logLabel - Label used in the confirmation log message + */ +function writeEnvLine(filePath, key, value, logLabel) { + if (!filePath || !value) return; + appendFileSync(filePath, `${key}=${value}\n`); + console.log(`[otlp] ${logLabel} written to ${filePath === process.env.GITHUB_OUTPUT ? "GITHUB_OUTPUT" : "GITHUB_ENV"}`); +} + /** * Send the OTLP job-setup span and propagate trace context via GITHUB_OUTPUT / * GITHUB_ENV. Non-fatal: all errors are silently swallowed. @@ -90,38 +104,25 @@ async function run() { console.log(`[otlp] setup span sent (traceId=${traceId}, spanId=${spanId})`); } + const githubOutput = process.env.GITHUB_OUTPUT; + const githubEnv = process.env.GITHUB_ENV; + // Always expose trace ID as a step output for cross-job correlation, even // when OTLP is not configured. This ensures needs.*.outputs.setup-trace-id // is populated for downstream jobs regardless of observability configuration. - if (isValidTraceId(traceId) && process.env.GITHUB_OUTPUT) { - appendFileSync(process.env.GITHUB_OUTPUT, `trace-id=${traceId}\n`); - console.log(`[otlp] trace-id=${traceId} written to GITHUB_OUTPUT`); - } - if (isValidSpanId(spanId) && process.env.GITHUB_OUTPUT) { - appendFileSync(process.env.GITHUB_OUTPUT, `span-id=${spanId}\n`); - console.log(`[otlp] span-id=${spanId} written to GITHUB_OUTPUT`); - } - if (isValidSpanId(parentSpanId) && process.env.GITHUB_OUTPUT) { - appendFileSync(process.env.GITHUB_OUTPUT, `parent-span-id=${parentSpanId}\n`); - console.log(`[otlp] parent-span-id=${parentSpanId} written to GITHUB_OUTPUT`); - } + if (isValidTraceId(traceId)) writeEnvLine(githubOutput, "trace-id", traceId, `trace-id=${traceId}`); + if (isValidSpanId(spanId)) writeEnvLine(githubOutput, "span-id", spanId, `span-id=${spanId}`); + if (isValidSpanId(parentSpanId)) writeEnvLine(githubOutput, "parent-span-id", parentSpanId, `parent-span-id=${parentSpanId}`); // Always propagate trace/span context to subsequent steps in this job so // that the conclusion span can find the same trace ID. - if (process.env.GITHUB_ENV) { - if (isValidTraceId(traceId)) { - appendFileSync(process.env.GITHUB_ENV, `GITHUB_AW_OTEL_TRACE_ID=${traceId}\n`); - console.log(`[otlp] GITHUB_AW_OTEL_TRACE_ID written to GITHUB_ENV`); - } - if (isValidSpanId(spanId)) { - appendFileSync(process.env.GITHUB_ENV, `GITHUB_AW_OTEL_PARENT_SPAN_ID=${spanId}\n`); - console.log(`[otlp] GITHUB_AW_OTEL_PARENT_SPAN_ID written to GITHUB_ENV`); - } + if (githubEnv) { + if (isValidTraceId(traceId)) writeEnvLine(githubEnv, "GITHUB_AW_OTEL_TRACE_ID", traceId, "GITHUB_AW_OTEL_TRACE_ID"); + if (isValidSpanId(spanId)) writeEnvLine(githubEnv, "GITHUB_AW_OTEL_PARENT_SPAN_ID", spanId, "GITHUB_AW_OTEL_PARENT_SPAN_ID"); // Propagate setup-end timestamp so the conclusion span can measure actual // job execution duration (setup-end → conclusion-start). - const setupEndMs = Math.floor(nowMs()); - appendFileSync(process.env.GITHUB_ENV, `GITHUB_AW_OTEL_JOB_START_MS=${setupEndMs}\n`); - console.log(`[otlp] GITHUB_AW_OTEL_JOB_START_MS written to GITHUB_ENV`); + const setupEndMs = String(Math.floor(nowMs())); + writeEnvLine(githubEnv, "GITHUB_AW_OTEL_JOB_START_MS", setupEndMs, "GITHUB_AW_OTEL_JOB_START_MS"); } }