-
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.
Improve extension loading state and starting
## The problem The `Extension`'s '`isLoaded` attribute was only set when the extension's `start` function was called. This made it difficult to determine if the extension was really loaded in the app or not, without starting the extension first. This made it necessary to start the extension in scenarios where we need to call extension functions without needing to starting it first, such as in the diagnose command. Calling `Extension`'s `start` doesn't change any state in the extension that needs to be set before any other function can be called. I think it was written this way as a way to communicate loading issues. A better name for this attribute would have been `isRunning`, but this is not something we have to track. ## Solution Update how the `isLoaded` is set, by checking if the extension was loaded into the app or not on load, not just when `start` is called. I've chosen to communicate this with an export from the extension wrapper, rather than if the start function returns an error or not. Since the value doesn't change at runtime it's now a static attribute of the class. I've removed the `isLoaded` return values from the extension's `start` and `stop` function, these are unused. ## About the error thrown by `start` I've left the `Extension`'s `start` function intact to print an error when called and the extension isn't loaded. In other integrations we print this on load of the extension. I suspect this was done on start, so that it wouldn't always be printed on unsupported Operating Systems. We may want to revisit this, or find another way to not print this all the time for those systems. ## Only start extension when its loaded and active Do not automatically start the extension when its initialized, but do this from the client like in all the other integrations. This also makes it easier to initialize the `Extension` class for other uses, like the diagnose without having to worry about the active state or not. Closes #551
- Loading branch information
Showing
10 changed files
with
80 additions
and
52 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/nodejs/.changesets/change-the-way-the-extension-is-started.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,6 @@ | ||
--- | ||
bump: "patch" | ||
type: "change" | ||
--- | ||
|
||
Change the way the extension is started. Previously it was started on initialization of the extension class (if the config was active), but now it's started by the client class. |
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,6 @@ | ||
--- | ||
bump: "patch" | ||
type: "change" | ||
--- | ||
|
||
Improve the reporting of extension loading state in the diagnose report. Previously it would only report it as loaded when it was also running. |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Extension } from "../extension" | ||
|
||
describe("Extension", () => { | ||
let ext: Extension | ||
|
||
beforeEach(() => { | ||
ext = new Extension() | ||
}) | ||
|
||
afterEach(() => { | ||
ext.stop() | ||
}) | ||
|
||
it("is not loaded", () => { | ||
expect(Extension.isLoaded).toEqual(false) | ||
}) | ||
|
||
it("does not start the client", () => { | ||
const warnSpy = jest.spyOn(console, "warn") | ||
expect(() => { | ||
ext.start() | ||
}).not.toThrow() | ||
expect(warnSpy).toHaveBeenLastCalledWith( | ||
"AppSignal extension not loaded. This could mean that your current environment isn't supported, or that another error has occurred." | ||
) | ||
}) | ||
|
||
it("does not error on stopping the client", () => { | ||
expect(() => { | ||
ext.stop() | ||
}).not.toThrow() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { extension } from "./extension_wrapper" | ||
import { extension, isLoaded as extensionLoaded } from "./extension_wrapper" | ||
|
||
/** | ||
* The public interface for the extension. | ||
* | ||
* @class | ||
*/ | ||
export class Extension { | ||
isLoaded = false | ||
static isLoaded = extensionLoaded | ||
|
||
constructor(options?: { active: boolean }) { | ||
if (options?.active) this.start() | ||
|
@@ -15,10 +15,9 @@ export class Extension { | |
/** | ||
* Starts the extension. | ||
*/ | ||
public start(): boolean { | ||
public start() { | ||
try { | ||
extension.start() | ||
this.isLoaded = true | ||
} catch (e) { | ||
if (e.message === "Extension module not loaded") { | ||
console.warn( | ||
|
@@ -29,41 +28,28 @@ export class Extension { | |
`Failed to load AppSignal extension with error: ${e.message}. Please email us at [email protected] for support.` | ||
) | ||
} | ||
|
||
this.isLoaded = false | ||
} | ||
|
||
return this.isLoaded | ||
} | ||
|
||
/** | ||
* Stops the extension. | ||
*/ | ||
public stop(): boolean { | ||
if (this.isLoaded) { | ||
extension.stop() | ||
this.isLoaded = false | ||
} | ||
|
||
return this.isLoaded | ||
public stop() { | ||
extension.stop() | ||
} | ||
|
||
public diagnose(): object { | ||
if (this.isLoaded) { | ||
process.env._APPSIGNAL_DIAGNOSE = "true" | ||
const diagnostics_report_string = extension.diagnoseRaw() | ||
delete process.env._APPSIGNAL_DIAGNOSE | ||
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") | ||
} | ||
try { | ||
return JSON.parse(diagnostics_report_string) | ||
} catch (error) { | ||
return { | ||
error: error, | ||
output: diagnostics_report_string.split("\n") | ||
} | ||
} else { | ||
return {} | ||
} | ||
} | ||
|
||
|
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,4 +1,5 @@ | ||
export interface ExtensionWrapper { | ||
isLoaded: boolean | ||
extension: any | ||
span: any | ||
datamap: any | ||
|