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

Propagate workflow and job attributes to child spans #134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
95 changes: 54 additions & 41 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,39 +367,42 @@ async function traceWorkflowRunJobs({ provider, workflowRunJobs, }) {
};
}, {});
}
// Metadata about the workflow run. These attributes will also be included in
// job and step spans.
const workflowAttributes = {
"github.workflow_id": workflowRunJobs.workflowRun.workflow_id,
"github.run_id": workflowRunJobs.workflowRun.id,
"github.run_number": workflowRunJobs.workflowRun.run_number,
"github.run_attempt": workflowRunJobs.workflowRun.run_attempt || 1,
"github.html_url": workflowRunJobs.workflowRun.html_url,
"github.workflow_url": workflowRunJobs.workflowRun.workflow_url,
"github.event": workflowRunJobs.workflowRun.event,
"github.workflow": workflowRunJobs.workflowRun.name || undefined,
"github.run_started_at": workflowRunJobs.workflowRun.run_started_at,
"github.author_name": ((_e = (_d = workflowRunJobs.workflowRun.head_commit) === null || _d === void 0 ? void 0 : _d.author) === null || _e === void 0 ? void 0 : _e.name) || undefined,
"github.author_email": ((_g = (_f = workflowRunJobs.workflowRun.head_commit) === null || _f === void 0 ? void 0 : _f.author) === null || _g === void 0 ? void 0 : _g.email) || undefined,
"github.head_commit.id": ((_h = workflowRunJobs.workflowRun.head_commit) === null || _h === void 0 ? void 0 : _h.id) || undefined,
"github.head_commit.tree_id": ((_j = workflowRunJobs.workflowRun.head_commit) === null || _j === void 0 ? void 0 : _j.tree_id) || undefined,
"github.head_commit.author.name": ((_l = (_k = workflowRunJobs.workflowRun.head_commit) === null || _k === void 0 ? void 0 : _k.author) === null || _l === void 0 ? void 0 : _l.email) || undefined,
"github.head_commit.author.email": ((_o = (_m = workflowRunJobs.workflowRun.head_commit) === null || _m === void 0 ? void 0 : _m.author) === null || _o === void 0 ? void 0 : _o.email) || undefined,
"github.head_commit.committer.name": ((_q = (_p = workflowRunJobs.workflowRun.head_commit) === null || _p === void 0 ? void 0 : _p.committer) === null || _q === void 0 ? void 0 : _q.email) || undefined,
"github.head_commit.committer.email": ((_s = (_r = workflowRunJobs.workflowRun.head_commit) === null || _r === void 0 ? void 0 : _r.committer) === null || _s === void 0 ? void 0 : _s.email) || undefined,
"github.head_commit.message": ((_t = workflowRunJobs.workflowRun.head_commit) === null || _t === void 0 ? void 0 : _t.message) || undefined,
"github.head_commit.timestamp": ((_u = workflowRunJobs.workflowRun.head_commit) === null || _u === void 0 ? void 0 : _u.timestamp) || undefined,
"github.head_sha": workflowRunJobs.workflowRun.head_sha,
"github.head_ref": headRef,
"github.base_ref": baseRef,
"github.base_sha": baseSha,
...pull_requests,
};
const rootSpan = tracer.startSpan(workflowRunJobs.workflowRun.name ||
`${workflowRunJobs.workflowRun.workflow_id}`, {
attributes: {
"github.workflow_id": workflowRunJobs.workflowRun.workflow_id,
"github.run_id": workflowRunJobs.workflowRun.id,
"github.run_number": workflowRunJobs.workflowRun.run_number,
"github.run_attempt": workflowRunJobs.workflowRun.run_attempt || 1,
"github.html_url": workflowRunJobs.workflowRun.html_url,
"github.workflow_url": workflowRunJobs.workflowRun.workflow_url,
"github.event": workflowRunJobs.workflowRun.event,
"github.workflow": workflowRunJobs.workflowRun.name || undefined,
"github.conclusion": workflowRunJobs.workflowRun.conclusion || undefined,
...workflowAttributes,
"github.created_at": workflowRunJobs.workflowRun.created_at,
"github.updated_at": workflowRunJobs.workflowRun.updated_at,
"github.run_started_at": workflowRunJobs.workflowRun.run_started_at,
"github.author_name": ((_e = (_d = workflowRunJobs.workflowRun.head_commit) === null || _d === void 0 ? void 0 : _d.author) === null || _e === void 0 ? void 0 : _e.name) || undefined,
"github.author_email": ((_g = (_f = workflowRunJobs.workflowRun.head_commit) === null || _f === void 0 ? void 0 : _f.author) === null || _g === void 0 ? void 0 : _g.email) || undefined,
"github.head_commit.id": ((_h = workflowRunJobs.workflowRun.head_commit) === null || _h === void 0 ? void 0 : _h.id) || undefined,
"github.head_commit.tree_id": ((_j = workflowRunJobs.workflowRun.head_commit) === null || _j === void 0 ? void 0 : _j.tree_id) || undefined,
"github.head_commit.author.name": ((_l = (_k = workflowRunJobs.workflowRun.head_commit) === null || _k === void 0 ? void 0 : _k.author) === null || _l === void 0 ? void 0 : _l.email) || undefined,
"github.head_commit.author.email": ((_o = (_m = workflowRunJobs.workflowRun.head_commit) === null || _m === void 0 ? void 0 : _m.author) === null || _o === void 0 ? void 0 : _o.email) || undefined,
"github.head_commit.committer.name": ((_q = (_p = workflowRunJobs.workflowRun.head_commit) === null || _p === void 0 ? void 0 : _p.committer) === null || _q === void 0 ? void 0 : _q.email) ||
undefined,
"github.head_commit.committer.email": ((_s = (_r = workflowRunJobs.workflowRun.head_commit) === null || _r === void 0 ? void 0 : _r.committer) === null || _s === void 0 ? void 0 : _s.email) ||
undefined,
"github.head_commit.message": ((_t = workflowRunJobs.workflowRun.head_commit) === null || _t === void 0 ? void 0 : _t.message) || undefined,
"github.head_commit.timestamp": ((_u = workflowRunJobs.workflowRun.head_commit) === null || _u === void 0 ? void 0 : _u.timestamp) || undefined,
"github.head_sha": workflowRunJobs.workflowRun.head_sha,
"github.head_ref": headRef,
"github.base_ref": baseRef,
"github.base_sha": baseSha,
"github.conclusion": workflowRunJobs.workflowRun.conclusion || undefined,
error: workflowRunJobs.workflowRun.conclusion === "failure",
...pull_requests,
},
root: true,
startTime,
Expand Down Expand Up @@ -427,6 +430,7 @@ async function traceWorkflowRunJobs({ provider, workflowRunJobs, }) {
tracer,
job,
workflowArtifacts: workflowRunJobs.workflowRunArtifacts,
workflowAttributes,
});
}
}
Expand All @@ -436,30 +440,35 @@ async function traceWorkflowRunJobs({ provider, workflowRunJobs, }) {
return rootSpan.spanContext();
}
exports.traceWorkflowRunJobs = traceWorkflowRunJobs;
async function traceWorkflowRunJob({ parentContext, trace, parentSpan, tracer, job, workflowArtifacts, }) {
async function traceWorkflowRunJob({ parentContext, trace, parentSpan, tracer, job, workflowArtifacts, workflowAttributes, }) {
var _a;
core.debug(`Trace Job ${job.id}`);
if (!job.completed_at) {
console.warn(`Job ${job.id} is not completed yet`);
return;
}
job.name;
const ctx = trace.setSpan(parentContext, parentSpan);
const startTime = new Date(job.started_at);
const completedTime = new Date(job.completed_at);
// Metadata about the workflow job. These attributes will also be included in
// step spans.
const jobAttributes = {
"github.job.id": job.id,
"github.job.name": job.name,
"github.job.run_id": job.run_id,
"github.job.run_attempt": job.run_attempt || 1,
"github.job.runner_group_id": job.runner_group_id || undefined,
"github.job.runner_group_name": job.runner_group_name || undefined,
"github.job.runner_name": job.runner_name || undefined,
"github.job.conclusion": job.conclusion || undefined,
"github.job.labels": job.labels.join(", ") || undefined,
"github.job.started_at": job.started_at || undefined,
"github.job.completed_at": job.completed_at || undefined,
};
const span = tracer.startSpan(job.name, {
attributes: {
"github.job.id": job.id,
"github.job.name": job.name,
"github.job.run_id": job.run_id,
"github.job.run_attempt": job.run_attempt || 1,
"github.job.runner_group_id": job.runner_group_id || undefined,
"github.job.runner_group_name": job.runner_group_name || undefined,
"github.job.runner_name": job.runner_name || undefined,
"github.job.conclusion": job.conclusion || undefined,
"github.job.labels": job.labels.join(", ") || undefined,
"github.job.started_at": job.started_at || undefined,
"github.job.completed_at": job.completed_at || undefined,
...workflowAttributes,
...jobAttributes,
"github.conclusion": job.conclusion || undefined,
error: job.conclusion === "failure",
},
Expand All @@ -486,6 +495,8 @@ async function traceWorkflowRunJob({ parentContext, trace, parentSpan, tracer, j
tracer,
workflowArtifacts,
step,
workflowAttributes,
jobAttributes,
});
}
}
Expand Down Expand Up @@ -529,7 +540,7 @@ exports.traceWorkflowRunStep = void 0;
const core = __importStar(__nccwpck_require__(42186));
const api_1 = __nccwpck_require__(65163);
const trace_otlp_file_1 = __nccwpck_require__(40535);
async function traceWorkflowRunStep({ job, parentContext, parentSpan, trace, tracer, workflowArtifacts, step, }) {
async function traceWorkflowRunStep({ job, parentContext, parentSpan, trace, tracer, workflowArtifacts, step, workflowAttributes, jobAttributes, }) {
if (!step || !step.completed_at || !step.started_at) {
const stepName = (step === null || step === void 0 ? void 0 : step.name) || "UNDEFINED";
console.warn(`Step ${stepName} is not completed yet.`);
Expand All @@ -545,6 +556,8 @@ async function traceWorkflowRunStep({ job, parentContext, parentSpan, trace, tra
const completedTime = new Date(step.completed_at);
const span = tracer.startSpan(step.name, {
attributes: {
...workflowAttributes,
...jobAttributes,
"github.job.step.name": step.name,
"github.job.step.number": step.number,
"github.job.step.started_at": step.started_at || undefined,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

117 changes: 67 additions & 50 deletions src/tracing/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Context,
trace,
SpanContext,
Attributes,
} from "@opentelemetry/api";
import { BasicTracerProvider, Tracer } from "@opentelemetry/sdk-trace-base";
import * as core from "@actions/core";
Expand Down Expand Up @@ -68,52 +69,56 @@ export async function traceWorkflowRunJobs({
);
}

// Metadata about the workflow run. These attributes will also be included in
// job and step spans.
const workflowAttributes = {
"github.workflow_id": workflowRunJobs.workflowRun.workflow_id,
"github.run_id": workflowRunJobs.workflowRun.id,
"github.run_number": workflowRunJobs.workflowRun.run_number,
"github.run_attempt": workflowRunJobs.workflowRun.run_attempt || 1,
"github.html_url": workflowRunJobs.workflowRun.html_url,
"github.workflow_url": workflowRunJobs.workflowRun.workflow_url,
"github.event": workflowRunJobs.workflowRun.event,
"github.workflow": workflowRunJobs.workflowRun.name || undefined,
"github.run_started_at": workflowRunJobs.workflowRun.run_started_at,
"github.author_name":
workflowRunJobs.workflowRun.head_commit?.author?.name || undefined,
"github.author_email":
workflowRunJobs.workflowRun.head_commit?.author?.email || undefined,
"github.head_commit.id":
workflowRunJobs.workflowRun.head_commit?.id || undefined,
"github.head_commit.tree_id":
workflowRunJobs.workflowRun.head_commit?.tree_id || undefined,
"github.head_commit.author.name":
workflowRunJobs.workflowRun.head_commit?.author?.email || undefined,
"github.head_commit.author.email":
workflowRunJobs.workflowRun.head_commit?.author?.email || undefined,
"github.head_commit.committer.name":
workflowRunJobs.workflowRun.head_commit?.committer?.email || undefined,
"github.head_commit.committer.email":
workflowRunJobs.workflowRun.head_commit?.committer?.email || undefined,
"github.head_commit.message":
workflowRunJobs.workflowRun.head_commit?.message || undefined,
"github.head_commit.timestamp":
workflowRunJobs.workflowRun.head_commit?.timestamp || undefined,
"github.head_sha": workflowRunJobs.workflowRun.head_sha,
"github.head_ref": headRef,
"github.base_ref": baseRef,
"github.base_sha": baseSha,
...pull_requests,
};

const rootSpan = tracer.startSpan(
workflowRunJobs.workflowRun.name ||
`${workflowRunJobs.workflowRun.workflow_id}`,
{
attributes: {
"github.workflow_id": workflowRunJobs.workflowRun.workflow_id,
"github.run_id": workflowRunJobs.workflowRun.id,
"github.run_number": workflowRunJobs.workflowRun.run_number,
"github.run_attempt": workflowRunJobs.workflowRun.run_attempt || 1,
"github.html_url": workflowRunJobs.workflowRun.html_url,
"github.workflow_url": workflowRunJobs.workflowRun.workflow_url,
"github.event": workflowRunJobs.workflowRun.event,
"github.workflow": workflowRunJobs.workflowRun.name || undefined,
"github.conclusion":
workflowRunJobs.workflowRun.conclusion || undefined,
...workflowAttributes,
"github.created_at": workflowRunJobs.workflowRun.created_at,
"github.updated_at": workflowRunJobs.workflowRun.updated_at,
"github.run_started_at": workflowRunJobs.workflowRun.run_started_at,
"github.author_name":
workflowRunJobs.workflowRun.head_commit?.author?.name || undefined,
"github.author_email":
workflowRunJobs.workflowRun.head_commit?.author?.email || undefined,
"github.head_commit.id":
workflowRunJobs.workflowRun.head_commit?.id || undefined,
"github.head_commit.tree_id":
workflowRunJobs.workflowRun.head_commit?.tree_id || undefined,
"github.head_commit.author.name":
workflowRunJobs.workflowRun.head_commit?.author?.email || undefined,
"github.head_commit.author.email":
workflowRunJobs.workflowRun.head_commit?.author?.email || undefined,
"github.head_commit.committer.name":
workflowRunJobs.workflowRun.head_commit?.committer?.email ||
undefined,
"github.head_commit.committer.email":
workflowRunJobs.workflowRun.head_commit?.committer?.email ||
undefined,
"github.head_commit.message":
workflowRunJobs.workflowRun.head_commit?.message || undefined,
"github.head_commit.timestamp":
workflowRunJobs.workflowRun.head_commit?.timestamp || undefined,
"github.head_sha": workflowRunJobs.workflowRun.head_sha,
"github.head_ref": headRef,
"github.base_ref": baseRef,
"github.base_sha": baseSha,
"github.conclusion":
workflowRunJobs.workflowRun.conclusion || undefined,
error: workflowRunJobs.workflowRun.conclusion === "failure",
...pull_requests,
},
root: true,
startTime,
Expand Down Expand Up @@ -149,6 +154,7 @@ export async function traceWorkflowRunJobs({
tracer,
job,
workflowArtifacts: workflowRunJobs.workflowRunArtifacts,
workflowAttributes,
});
}
} finally {
Expand All @@ -164,6 +170,7 @@ type TraceWorkflowRunJobParams = {
tracer: Tracer;
job: WorkflowRunJob;
workflowArtifacts: WorkflowArtifactLookup;
workflowAttributes: Attributes;
};

async function traceWorkflowRunJob({
Expand All @@ -173,31 +180,39 @@ async function traceWorkflowRunJob({
tracer,
job,
workflowArtifacts,
workflowAttributes,
}: TraceWorkflowRunJobParams) {
core.debug(`Trace Job ${job.id}`);
if (!job.completed_at) {
console.warn(`Job ${job.id} is not completed yet`);
return;
}
job.name;
const ctx = trace.setSpan(parentContext, parentSpan);
const startTime = new Date(job.started_at);
const completedTime = new Date(job.completed_at);

// Metadata about the workflow job. These attributes will also be included in
// step spans.
const jobAttributes = {
"github.job.id": job.id,
"github.job.name": job.name,
"github.job.run_id": job.run_id,
"github.job.run_attempt": job.run_attempt || 1,
"github.job.runner_group_id": job.runner_group_id || undefined,
"github.job.runner_group_name": job.runner_group_name || undefined,
"github.job.runner_name": job.runner_name || undefined,
"github.job.conclusion": job.conclusion || undefined,
"github.job.labels": job.labels.join(", ") || undefined,
"github.job.started_at": job.started_at || undefined,
"github.job.completed_at": job.completed_at || undefined,
};

const span = tracer.startSpan(
job.name,
{
attributes: {
"github.job.id": job.id,
"github.job.name": job.name,
"github.job.run_id": job.run_id,
"github.job.run_attempt": job.run_attempt || 1,
"github.job.runner_group_id": job.runner_group_id || undefined,
"github.job.runner_group_name": job.runner_group_name || undefined,
"github.job.runner_name": job.runner_name || undefined,
"github.job.conclusion": job.conclusion || undefined,
"github.job.labels": job.labels.join(", ") || undefined,
"github.job.started_at": job.started_at || undefined,
"github.job.completed_at": job.completed_at || undefined,
...workflowAttributes,
...jobAttributes,
"github.conclusion": job.conclusion || undefined,
error: job.conclusion === "failure",
},
Expand Down Expand Up @@ -227,6 +242,8 @@ async function traceWorkflowRunJob({
tracer,
workflowArtifacts,
step,
workflowAttributes,
jobAttributes,
});
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/tracing/step.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as core from "@actions/core";
import { TraceAPI, Context, SpanStatusCode, Span } from "@opentelemetry/api";
import {
TraceAPI,
Context,
SpanStatusCode,
Span,
Attributes,
} from "@opentelemetry/api";
import { Tracer } from "@opentelemetry/sdk-trace-base";
import {
WorkflowRunJobStep,
Expand All @@ -16,7 +22,10 @@ export type TraceWorkflowRunStepParams = {
tracer: Tracer;
workflowArtifacts: WorkflowArtifactLookup;
step?: WorkflowRunJobStep;
workflowAttributes: Attributes;
jobAttributes: Attributes;
};

export async function traceWorkflowRunStep({
job,
parentContext,
Expand All @@ -25,6 +34,8 @@ export async function traceWorkflowRunStep({
tracer,
workflowArtifacts,
step,
workflowAttributes,
jobAttributes,
}: TraceWorkflowRunStepParams) {
if (!step || !step.completed_at || !step.started_at) {
const stepName = step?.name || "UNDEFINED";
Expand All @@ -43,6 +54,8 @@ export async function traceWorkflowRunStep({
step.name,
{
attributes: {
...workflowAttributes,
...jobAttributes,
"github.job.step.name": step.name,
"github.job.step.number": step.number,
"github.job.step.started_at": step.started_at || undefined,
Expand Down