-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only report extension load errors on start
Before this change, extension load errors would be reported whenever `@appsignal/nodejs` or the `client.ts` file was imported. Now they will only be reported whenever `Client` fails to initialise. This fixes an issue where the extension load errors would be reported when running `npm install` in the `appsignal-nodejs` folder. Rearrange the way that the client is initialised. Remove the `client.start()` method, as starting is done automatically during initialisation. Use early returns to clearly distinguish the client initialisation failure scenarios. Emit distinct error messages for different configuration-related failures. Change the `this.#isActive` method to reflect whether the client was actually initialised, rather than whether several factors in the configuration should mean that the client was initialised.
- Loading branch information
Showing
8 changed files
with
123 additions
and
87 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
.changesets/improve-error-reporting-during-initialisation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
bump: "patch" | ||
type: "change" | ||
--- | ||
|
||
### Improve error reporting during initialisation | ||
|
||
Do not report an error with the extension installation when AppSignal is imported -- instead, report it when attempting to initialise AppSignal. Do not report an error with the extension if AppSignal is not configured to be active. | ||
|
||
When AppSignal does not start due to its configuration (`active` is set to `false`, or the push API key is missing) report the specific reason why. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,38 @@ | ||
import fs from "fs" | ||
import { Extension } from "../extension" | ||
import { Client } from "../client" | ||
import { | ||
reportPath, | ||
processTarget | ||
} from "../../scripts/extension/support/helpers" | ||
|
||
const CLIENT_OPTIONS = { | ||
name: "app", | ||
pushApiKey: "yes", | ||
active: true | ||
} | ||
|
||
describe("Extension", () => { | ||
let ext: Extension | ||
let client: Client | undefined | ||
|
||
beforeEach(() => { | ||
ext = new Extension() | ||
client = undefined | ||
}) | ||
|
||
afterEach(() => { | ||
ext.stop() | ||
if (client) client.stop() | ||
}) | ||
|
||
it("logs an error when the module is required", () => { | ||
it("logs an error when an active client is initialised", () => { | ||
const errorSpy = jest.spyOn(console, "error") | ||
|
||
jest.resetModules() | ||
require("../extension") | ||
client = new Client(CLIENT_OPTIONS) | ||
|
||
expect(errorSpy).toHaveBeenLastCalledWith( | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
"[appsignal][ERROR] AppSignal failed to load the extension. Please run the diagnose tool and email us at [email protected]: https://docs.appsignal.com/nodejs/3.x/command-line/diagnose.html\n", | ||
expect.any(Object) | ||
) | ||
|
@@ -56,9 +66,9 @@ describe("Extension", () => { | |
const arch = process.arch | ||
|
||
jest.resetModules() | ||
require("../extension") | ||
client = new Client(CLIENT_OPTIONS) | ||
|
||
expect(errorSpy).toHaveBeenLastCalledWith( | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
`[appsignal][ERROR] The AppSignal extension was installed for architecture 'dummyArch-dummyTarget', but the current architecture is '${arch}-${target}'. Please reinstall the AppSignal package on the host the app is started.` | ||
) | ||
}) | ||
|
@@ -82,13 +92,13 @@ describe("Extension", () => { | |
const errorSpy = jest.spyOn(console, "error") | ||
|
||
jest.resetModules() | ||
require("../extension") | ||
client = new Client(CLIENT_OPTIONS) | ||
|
||
expect(errorSpy).toHaveBeenCalledWith( | ||
"[appsignal][ERROR] Unable to fetch install report:", | ||
expect.any(Object) | ||
) | ||
expect(errorSpy).toHaveBeenLastCalledWith( | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
"[appsignal][ERROR] AppSignal failed to load the extension. Please run the diagnose tool and email us at [email protected]: https://docs.appsignal.com/nodejs/3.x/command-line/diagnose.html\n", | ||
expect.any(Object) | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,25 +27,6 @@ try { | |
mod = require("../build/Release/extension.node") as ExtensionWrapper | ||
mod.isLoaded = true | ||
} catch (error) { | ||
const [installArch, installTarget] = fetchInstalledArch() | ||
const arch = process.arch | ||
const target = processTarget() | ||
|
||
if ( | ||
installArch && | ||
installTarget && | ||
(arch !== installArch || target !== installTarget) | ||
) { | ||
console.error( | ||
`[appsignal][ERROR] The AppSignal extension was installed for architecture '${installArch}-${installTarget}', but the current architecture is '${arch}-${target}'. Please reinstall the AppSignal package on the host the app is started.` | ||
) | ||
} else { | ||
console.error( | ||
"[appsignal][ERROR] AppSignal failed to load the extension. Please run the diagnose tool and email us at [email protected]: https://docs.appsignal.com/nodejs/3.x/command-line/diagnose.html\n", | ||
error | ||
) | ||
} | ||
|
||
mod = { | ||
isLoaded: false, | ||
extension: { | ||
|
@@ -66,6 +47,26 @@ try { | |
}, | ||
log() { | ||
return | ||
}, | ||
logLoadingErrors() { | ||
const [installArch, installTarget] = fetchInstalledArch() | ||
const arch = process.arch | ||
const target = processTarget() | ||
|
||
if ( | ||
installArch && | ||
installTarget && | ||
(arch !== installArch || target !== installTarget) | ||
) { | ||
console.error( | ||
`[appsignal][ERROR] The AppSignal extension was installed for architecture '${installArch}-${installTarget}', but the current architecture is '${arch}-${target}'. Please reinstall the AppSignal package on the host the app is started.` | ||
) | ||
} else { | ||
console.error( | ||
"[appsignal][ERROR] AppSignal failed to load the extension. Please run the diagnose tool and email us at [email protected]: https://docs.appsignal.com/nodejs/3.x/command-line/diagnose.html\n", | ||
error | ||
) | ||
} | ||
} | ||
} | ||
} as ExtensionWrapper | ||
|