Skip to content

Commit

Permalink
Only collect backtraces thaht happened while this run was happening
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc committed Aug 26, 2024
1 parent 4961a0a commit 21e6930
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
34 changes: 20 additions & 14 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions src/backtrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ import { isLinux, isMacOS } from "./actions-core-platform.js";
import { stringifyError } from "./errors.js";
import * as actionsCore from "@actions/core";
import * as exec from "@actions/exec";
import { readFile, readdir } from "node:fs/promises";
import { readFile, readdir, stat } from "node:fs/promises";
import { promisify } from "node:util";
import { gzip } from "node:zlib";

export async function collectBacktraces(
prefixes: string[],
startTimestampMs: number,
): Promise<Map<string, string>> {
if (isMacOS) {
return await collectBacktracesMacOS(prefixes);
return await collectBacktracesMacOS(prefixes, startTimestampMs);
}
if (isLinux) {
return await collectBacktracesSystemd(prefixes);
return await collectBacktracesSystemd(prefixes, startTimestampMs);
}

return new Map();
}

export async function collectBacktracesMacOS(
prefixes: string[],
startTimestampMs: number,
): Promise<Map<string, string>> {
const backtraces: Map<string, string> = new Map();

Expand Down Expand Up @@ -85,12 +87,14 @@ export async function collectBacktracesMacOS(
const doGzip = promisify(gzip);
for (const fileName of fileNames) {
try {
const logText = await readFile(`${dir}/${fileName}`);
const buf = await doGzip(logText);
backtraces.set(
`backtrace_value_${source}_${fileName}`,
buf.toString("base64"),
);
if ((await stat(`${dir}/${fileName}`)).ctimeMs >= startTimestampMs) {
const logText = await readFile(`${dir}/${fileName}`);
const buf = await doGzip(logText);
backtraces.set(
`backtrace_value_${source}_${fileName}`,
buf.toString("base64"),
);
}
} catch (innerError: unknown) {
backtraces.set(
`backtrace_failure_${source}_${fileName}`,
Expand All @@ -110,15 +114,17 @@ type SystemdCoreDumpInfo = {

export async function collectBacktracesSystemd(
prefixes: string[],
startTimestampMs: number,
): Promise<Map<string, string>> {
const sinceSeconds = Math.ceil((Date.now() - startTimestampMs) / 1000);
const backtraces: Map<string, string> = new Map();

const coredumps: SystemdCoreDumpInfo[] = [];

try {
const { stdout: coredumpjson } = await exec.getExecOutput(
"coredumpctl",
["--json=pretty", "list", "--since", "1 hour ago"],
["--json=pretty", "list", "--since", `${sinceSeconds} seconds ago`],
{
silent: true,
},
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const STATE_KEY_EXECUTION_PHASE = "detsys_action_execution_phase";
const STATE_KEY_NIX_NOT_FOUND = "detsys_action_nix_not_found";
const STATE_NOT_FOUND = "not-found";
const STATE_KEY_CROSS_PHASE_ID = "detsys_cross_phase_id";
const STATE_BACKTRACE_START_TIMESTAMP = "detsys_backtrace_start_timestamp";

const DIAGNOSTIC_ENDPOINT_TIMEOUT_MS = 30_000; // 30 seconds in milliseconds
const CHECK_IN_ENDPOINT_TIMEOUT_MS = 5_000; // 5 seconds in milliseconds
Expand Down Expand Up @@ -787,6 +788,8 @@ export abstract class DetSysAction {
"DETSYS_BACKTRACE_COLLECTOR",
this.getCrossPhaseId(),
);

actionsCore.saveState(STATE_BACKTRACE_START_TIMESTAMP, Date.now());
}
}

Expand All @@ -798,6 +801,7 @@ export abstract class DetSysAction {

const backtraces = await collectBacktraces(
this.actionOptions.binaryNamePrefixes,
parseInt(actionsCore.getState(STATE_BACKTRACE_START_TIMESTAMP)),
);
actionsCore.debug(`Backtraces identified: ${backtraces.size}`);
if (backtraces.size > 0) {
Expand Down

0 comments on commit 21e6930

Please sign in to comment.