Skip to content

Commit

Permalink
Add helper for globally stored client
Browse files Browse the repository at this point in the history
A new static helper method is available from `BaseClient` (exported as
`Appsignal`). It allows access to the globally stored client when
AppSignal is initialized.

This will give us the ability to develop missing features that are based
on config options.
  • Loading branch information
luismiramirez committed Dec 17, 2021
1 parent 87009c6 commit 0eb2eed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Add helper to access the globally stored AppSignal client.
8 changes: 8 additions & 0 deletions packages/nodejs/src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ describe("BaseClient", () => {
expect(global.__APPSIGNAL__).toEqual(client)
})

it("returns the client from global object", () => {
expect(BaseClient.client).toEqual(client)
})

it("returns the client config from global object", () => {
expect(BaseClient.config).toEqual(client.config)
})

it("does not start the client if config is not valid", () => {
process.env["APPSIGNAL_PUSH_API_KEY"] = undefined
client = new BaseClient({ name, enableMinutelyProbes: false })
Expand Down
11 changes: 11 additions & 0 deletions packages/nodejs/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ export class BaseClient implements Client {
#tracer: Tracer = new BaseTracer()
#metrics: Metrics = new BaseMetrics()

/**
* Global accessors to Client and Config
*/
static get client(): Client {
return global.__APPSIGNAL__
}

static get config(): Configuration {
return global.__APPSIGNAL__?.config
}

/**
* Creates a new instance of the `Appsignal` object
*/
Expand Down

0 comments on commit 0eb2eed

Please sign in to comment.