diff --git a/packages/nodejs/.changesets/diagnose-api-key-transmission.md b/packages/nodejs/.changesets/diagnose-api-key-transmission.md new file mode 100644 index 00000000..f1fd4bcc --- /dev/null +++ b/packages/nodejs/.changesets/diagnose-api-key-transmission.md @@ -0,0 +1,5 @@ +--- +bump: "patch" +--- + +Fix diagnose report recognition when sent to the server. It was sent without an `api_key` parameter, which resulted in apps not being linked to the parent organization based on the known Push API key. diff --git a/packages/nodejs/src/cli/diagnose.ts b/packages/nodejs/src/cli/diagnose.ts index 0bcf38ba..b9fc609e 100644 --- a/packages/nodejs/src/cli/diagnose.ts +++ b/packages/nodejs/src/cli/diagnose.ts @@ -7,8 +7,14 @@ const readline = require("readline") import { HashMap } from "@appsignal/types" export class Diagnose { + #diagnose: typeof DiagnoseTool + + constructor() { + this.#diagnose = new DiagnoseTool({}) + } + public async run() { - const data = await new DiagnoseTool({}).generate() + const data = await this.#diagnose.generate() console.log(`AppSignal diagnose`) console.log(`=`.repeat(80)) @@ -218,7 +224,7 @@ export class Diagnose { ` Not sending report. (Specified with the --no-send-report option.)` ) } else if (process.argv.includes("--send-report")) { - this.send_report(data) + this.#diagnose.sendReport(data) } else { const rl = readline.createInterface({ input: process.stdin, @@ -231,7 +237,7 @@ export class Diagnose { function (answer: String) { switch (answer || "y") { case "y": - self.send_report(data) + self.#diagnose.sendReport(data) break default: @@ -348,45 +354,6 @@ export class Diagnose { } } - send_report(data: object) { - const json = JSON.stringify(data) - - const opts = { - port: 443, - method: "POST", - host: "appsignal.com", - path: "/diag", - headers: { - "Content-Type": "application/json", - "Content-Length": json.length - }, - cert: fs.readFileSync( - path.resolve(__dirname, "../../cert/cacert.pem"), - "utf-8" - ) - } - - const req = https.request(opts, (res: any) => { - res.setEncoding("utf8") - - // print token to the console - res.on("data", (chunk: any) => { - const { token } = JSON.parse(chunk.toString()) - console.log(` Your support token:`, token) - console.log( - ` View this report: https://appsignal.com/diagnose/${token}` - ) - }) - }) - - req.on("error", (e: any) => { - console.error(`Problem with diagnose request: ${e.message}`) - }) - - req.write(json) - req.end() - } - print_newline() { console.log(``) } diff --git a/packages/nodejs/src/diagnose.ts b/packages/nodejs/src/diagnose.ts index f5f47d5e..304a9d27 100644 --- a/packages/nodejs/src/diagnose.ts +++ b/packages/nodejs/src/diagnose.ts @@ -194,6 +194,52 @@ export class DiagnoseTool { return config } + + public sendReport(data: object) { + const json = JSON.stringify(data) + + const config = this.#config.data + const params = new URLSearchParams({ api_key: config["apiKey"] || "" }) + + const opts = { + port: 443, + method: "POST", + host: "appsignal.com", + path: `/diag?${params.toString()}`, + headers: { + "Content-Type": "application/json", + "Content-Length": json.length + }, + cert: fs.readFileSync( + path.resolve(__dirname, "../cert/cacert.pem"), + "utf-8" + ) + } + + const request = https.request(opts, (response: any) => { + const responseStatus = response.statusCode + response.setEncoding("utf8") + + response.on("data", (responseData: any) => { + if (responseStatus === 200) { + const { token } = JSON.parse(responseData.toString()) + console.log(` Your support token:`, token) + console.log( + ` View this report: https://appsignal.com/diagnose/${token}` + ) + } else { + console.error( + " Error: Something went wrong while submitting the report to AppSignal." + ) + console.error(` Response code: ${responseStatus}`) + console.error(` Response body:\n${responseData}`) + } + }) + }) + + request.write(json) + request.end() + } } // This implementation should match the `packages/nodejs-ext/scripts/report.js`