Skip to content

Commit

Permalink
Rename Agent to Extension
Browse files Browse the repository at this point in the history
To fit with our naming standards, Agent is now called Extension
  • Loading branch information
luismiramirez committed Oct 8, 2021
1 parent 91bd1c3 commit 1db657a
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 94 deletions.
7 changes: 7 additions & 0 deletions packages/nodejs/.changesets/rename-agent-to-extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
bump: "patch"
---

Rename Agent to Extension

To fit with our naming standards, Agent class is now Called Extension.
2 changes: 1 addition & 1 deletion packages/nodejs/src/__tests__/data.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Data } from "../internal/data"
import { datamap, dataarray } from "../extension"
import { datamap, dataarray } from "../extension_wrapper"

describe("Data", () => {
it("creates an empty map", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Agent } from "../agent"
import { Extension } from "../extension"

describe("Extension", () => {
let ext: Agent
let ext: Extension

beforeEach(() => {
ext = new Agent()
ext = new Extension()
})

afterEach(() => {
Expand All @@ -29,7 +29,7 @@ describe("Extension", () => {

it("starts the client when the active option is true", () => {
expect(() => {
ext = new Agent({ active: true })
ext = new Extension({ active: true })
}).not.toThrow()

expect(ext.isLoaded).toBeTruthy()
Expand Down
58 changes: 0 additions & 58 deletions packages/nodejs/src/agent.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/nodejs/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NodeClient, Metrics, Plugin, Tracer } from "@appsignal/types"

import { Agent } from "./agent"
import { Extension } from "./extension"
import { Configuration } from "./config"
import { BaseTracer } from "./tracer"
import { BaseMetrics } from "./metrics"
Expand All @@ -24,7 +24,7 @@ export class Client implements NodeClient {
readonly VERSION = VERSION

config: Configuration
agent: Agent
extension: Extension
instrumentation: Instrumentation

#tracer: Tracer = new BaseTracer()
Expand All @@ -41,7 +41,7 @@ export class Client implements NodeClient {
} = options

this.config = new Configuration(options)
this.agent = new Agent({ active })
this.extension = new Extension({ active })

this.instrumentation = new Instrumentation(this.tracer(), this.metrics())

Expand All @@ -53,7 +53,7 @@ export class Client implements NodeClient {
* Returns `true` if the agent is loaded and configuration is valid
*/
get isActive(): boolean {
return this.agent.isLoaded && this.config.isValid
return this.extension.isLoaded && this.config.isValid
}

set isActive(arg) {
Expand All @@ -68,7 +68,7 @@ export class Client implements NodeClient {
*/
public start(): void {
if (this.config.isValid) {
this.agent.start()
this.extension.start()
} else {
console.error("Not starting, no valid config for this environment")
}
Expand All @@ -87,7 +87,7 @@ export class Client implements NodeClient {
console.log("Stopping AppSignal")
}

this.agent.stop()
this.extension.stop()
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/nodejs/src/diagnose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import https from "https"
import { URL, URLSearchParams } from "url"
import { createHash } from "crypto"

import { Agent } from "./agent"
import { Extension } from "./extension"
import { Configuration } from "./config"
import { AGENT_VERSION, VERSION } from "./version"
import { JS_TO_RUBY_MAPPING } from "./config/configmap"
Expand All @@ -24,11 +24,11 @@ interface FileMetadata {

export class DiagnoseTool {
#config: Configuration
#agent: Agent
#extension: Extension

constructor({ active = true }) {
this.#config = new Configuration({ active })
this.#agent = new Agent({ active })
this.#extension = new Extension({ active })
}

/**
Expand All @@ -45,7 +45,7 @@ export class DiagnoseTool {
installation: this.getInstallationReport(),
host: this.getHostData(),
app: {},
agent: this.#agent.diagnose(),
extension: this.#extension.diagnose(),
config: {
options: this.getConfigData(),
sources: {}
Expand All @@ -63,7 +63,7 @@ export class DiagnoseTool {
language: "nodejs",
package_version: VERSION,
agent_version: AGENT_VERSION,
extension_loaded: this.#agent.isLoaded
extension_loaded: this.#extension.isLoaded
}
}

Expand Down
72 changes: 55 additions & 17 deletions packages/nodejs/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
import { Extension } from "./interfaces/extension"

let mod: Extension

try {
mod = require("@appsignal/nodejs-ext") as Extension
} catch (e) {
mod = {
extension: {
start() {
throw new Error("Extension module not loaded")
},
stop() {
return
import { extension } from "./extension_wrapper"

/**
* The public interface for the extension.
*
* @class
*/
export class Extension {
isLoaded = false

constructor(options?: { active: boolean }) {
if (options?.active) this.start()
}

/**
* Starts the extension.
*/
public start(): boolean {
try {
extension.start()
this.isLoaded = true
} catch (e) {
if (e.message === "Extension module not loaded") {
console.warn(
"AppSignal extension not loaded. This could mean that your current environment isn't supported, or that another error has occurred."
)
} else {
console.error(
`Failed to load AppSignal extension with error: ${e.message}. Please email us at [email protected] for support.`
)
}

this.isLoaded = false
}
} as Extension
}

export = mod
return this.isLoaded
}

/**
* Stops the extension.
*/
public stop(): boolean {
if (this.isLoaded) {
extension.stop()
this.isLoaded = false
}

return this.isLoaded
}

public diagnose(): object {
if (this.isLoaded) {
return JSON.parse(extension.diagnoseRaw())
} else {
return {}
}
}
}
20 changes: 20 additions & 0 deletions packages/nodejs/src/extension_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ExtensionWrapper } from "./interfaces/extension_wrapper"

let mod: ExtensionWrapper

try {
mod = require("@appsignal/nodejs-ext") as ExtensionWrapper
} catch (e) {
mod = {
extension: {
start() {
throw new Error("Extension module not loaded")
},
stop() {
return
}
}
} as ExtensionWrapper
}

export = mod
7 changes: 7 additions & 0 deletions packages/nodejs/src/interfaces/extension_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface ExtensionWrapper {
extension: any
span: any
datamap: any
dataarray: any
metrics: any
}
2 changes: 1 addition & 1 deletion packages/nodejs/src/internal/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HashMap } from "@appsignal/types"
import { datamap, dataarray } from "../extension"
import { datamap, dataarray } from "../extension_wrapper"

export class Data {
public static generate(
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Metrics, Probes } from "@appsignal/types"

import { BaseProbes } from "./probes"
import { metrics } from "./extension"
import { metrics } from "./extension_wrapper"
import { Data } from "./internal/data"

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
HashMapValue
} from "@appsignal/types"

import { span } from "./extension"
import { span } from "./extension_wrapper"
import { Data } from "./internal/data"
import { getAgentTimestamps } from "./utils"

Expand Down

0 comments on commit 1db657a

Please sign in to comment.