diff --git a/packages/nodejs/.changesets/print-agent-diagnose-report.md b/packages/nodejs/.changesets/print-agent-diagnose-report.md new file mode 100644 index 00000000..7181f430 --- /dev/null +++ b/packages/nodejs/.changesets/print-agent-diagnose-report.md @@ -0,0 +1,5 @@ +--- +bump: "patch" +--- + +Print agent diagnose report in diagnose CLI output. diff --git a/packages/nodejs/src/cli/diagnose.ts b/packages/nodejs/src/cli/diagnose.ts index 115d22ae..ec0e4c9e 100644 --- a/packages/nodejs/src/cli/diagnose.ts +++ b/packages/nodejs/src/cli/diagnose.ts @@ -5,6 +5,37 @@ const path = require("path") const util = require("util") const readline = require("readline") +interface AgentUnitTestDefinition { + label: string + values?: { + [key: string]: string + } +} +interface AgentTestDefinition { + [key: string]: { + // component: agent/extension + label: string + tests: { + [key: string]: { + // category: config/boot/some_dir/etc + [key: string]: AgentUnitTestDefinition // test: valid/started/uid/gid/etc + } + } + } +} +interface AgentReportUnit { + result?: string | boolean + error?: any + output?: any +} +interface AgentReport { + [key: string]: { + [key: string]: { + [key: string]: AgentReportUnit + } + } +} + export class Diagnose { public async run() { const data = await new DiagnoseTool({}).generate() @@ -128,6 +159,9 @@ export class Diagnose { data["host"]["running_in_container"] )}` ) + this.print_newline() + + this.printAgentDiagnose(data["agent"]) this.print_newline() @@ -240,6 +274,108 @@ export class Diagnose { } } + printAgentDiagnose(report: AgentReport) { + if (report["error"]) { + console.log(" Error while parsing agent diagnostics report:") + console.log(` Error: ${report["error"]}`) + console.log(` Output: ${report["output"]}`) + } else { + console.log("Agent diagnostics") + const reportDefinition = this.agentDiagnosticTestDefinition() + for (const component in reportDefinition) { + const componentDefinition = reportDefinition[component] + const componentTests = componentDefinition["tests"] + console.log(` ${componentDefinition.label}`) + for (const category in componentTests) { + const tests = componentTests[category] + for (const testName in tests) { + const testDefinition = tests[testName] + const testReport = report[component][category][testName] + this.printAgentTest(testDefinition, testReport) + } + } + } + } + } + + printAgentTest(definition: AgentUnitTestDefinition, test: AgentReportUnit) { + const value = test["result"] + const error = test["error"] + const output = test["output"] + + let formattedValue + if (value !== undefined) { + const stringValue = value.toString() + formattedValue = definition.values + ? definition.values[stringValue] + : stringValue + } + if (!formattedValue) { + formattedValue = "-" + } + console.log(` ${definition["label"]}: ${formattedValue}`) + if (error) { + console.log(` Error: ${error}`) + } + if (output) { + console.log(` Output: ${output}`) + } + } + + agentDiagnosticTestDefinition(): AgentTestDefinition { + return { + extension: { + label: "Extension tests", + tests: { + config: { + valid: { + label: "Configuration", + values: { true: "valid", false: "invalid" } + } + } + } + }, + agent: { + label: "Agent tests", + tests: { + boot: { + started: { + label: "Started", + values: { true: "started", false: "not started" } + } + }, + host: { + uid: { label: "Process user id" }, + gid: { label: "Process user group id" } + }, + config: { + valid: { + label: "Configuration", + values: { true: "valid", false: "invalid" } + } + }, + logger: { + started: { + label: "Logger", + values: { true: "started", false: "not started" } + } + }, + working_directory_stat: { + uid: { label: "Working directory user id" }, + gid: { label: "Working directory user group id" }, + mode: { label: "Working directory permissions" } + }, + lock_path: { + created: { + label: "Lock path", + values: { true: "writable", false: "not writable" } + } + } + } + } + } + } + send_report(data: object) { const json = JSON.stringify(data) diff --git a/packages/nodejs/src/extension.ts b/packages/nodejs/src/extension.ts index 12477652..721a57f9 100644 --- a/packages/nodejs/src/extension.ts +++ b/packages/nodejs/src/extension.ts @@ -50,7 +50,18 @@ export class Extension { public diagnose(): object { if (this.isLoaded) { - return JSON.parse(extension.diagnoseRaw()) + process.env._APPSIGNAL_DIAGNOSE = "true" + const diagnostics_report_string = extension.diagnoseRaw() + delete process.env._APPSIGNAL_DIAGNOSE + + try { + return JSON.parse(diagnostics_report_string) + } catch (error) { + return { + error: error, + output: diagnostics_report_string.split("\n") + } + } } else { return {} } diff --git a/test/integration/diagnose b/test/integration/diagnose index f53dfd3c..a3d94d92 160000 --- a/test/integration/diagnose +++ b/test/integration/diagnose @@ -1 +1 @@ -Subproject commit f53dfd3c0405e4d7d5137d99a654547bdfb04831 +Subproject commit a3d94d92f2e0d3307c7fb62978d4069190acf237