Skip to content

Commit

Permalink
Add cpuCount configuration option
Browse files Browse the repository at this point in the history
Implement the `cpuCount` (`APPSIGNAL_CPU_COUNT` environment variable)
configuration option for the AppSignal for Node.js integration.
  • Loading branch information
unflxw committed Mar 22, 2024
1 parent 77f3d0a commit e7cf661
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .changesets/implement-cpu-count-configuration-option.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ type: "add"

Implement CPU count configuration option. Use it to override the auto-detected, cgroups-provided number of CPUs that is used to calculate CPU usage percentages.

To set it, use the `APPSIGNAL_CPU_COUNT` environment variable, the `cpu_count`
configuration option in the Ruby, Elixir or Python integrations, the `cpuCount` attribute in the Node.js instrumentation, or the `cpu_count` configuration option in the stand-alone agent TOML configuration file.
To set it, use the `cpuCount` configuration option, or the `APPSIGNAL_CPU_COUNT` environment variable.

9 changes: 8 additions & 1 deletion src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,15 @@ describe("Configuration", () => {
process.env["APPSIGNAL_ENABLE_STATSD"] = "true"
process.env["APPSIGNAL_ENABLE_HOST_METRICS"] = "false"
process.env["APPSIGNAL_DNS_SERVERS"] = "8.8.8.8,8.8.4.4"
process.env["APPSIGNAL_CPU_COUNT"] = "1.5"

const envOptions = {
enableStatsd: true,
enableHostMetrics: false,
dnsServers: ["8.8.8.8", "8.8.4.4"]
dnsServers: ["8.8.8.8", "8.8.4.4"],
cpuCount: 1.5
}

const expectedConfig = {
...expectedDefaultConfig,
...envOptions
Expand Down Expand Up @@ -319,6 +323,7 @@ describe("Configuration", () => {
expect(env("_APPSIGNAL_APP_NAME")).toBeUndefined()
expect(env("_APPSIGNAL_BIND_ADDRESS")).toBeUndefined()
expect(env("_APPSIGNAL_CA_FILE_PATH")).toMatch(/cert\/cacert\.pem$/)
expect(env("_APPSIGNAL_CPU_COUNT")).toBeUndefined()
expect(env("_APPSIGNAL_DNS_SERVERS")).toBeUndefined()
expect(env("_APPSIGNAL_ENABLE_HOST_METRICS")).toEqual("true")
expect(env("_APPSIGNAL_ENABLE_OPENTELEMETRY_HTTP")).toEqual("true")
Expand Down Expand Up @@ -367,6 +372,7 @@ describe("Configuration", () => {
name,
active: true,
bindAddress: "0.0.0.0",
cpuCount: 1.5,
pushApiKey,
dnsServers: ["8.8.8.8", "8.8.4.4"],
enableHostMetrics: false,
Expand Down Expand Up @@ -397,6 +403,7 @@ describe("Configuration", () => {
expect(env("_APPSIGNAL_ACTIVE")).toEqual("true")
expect(env("_APPSIGNAL_APP_NAME")).toEqual(name)
expect(env("_APPSIGNAL_BIND_ADDRESS")).toEqual("0.0.0.0")
expect(env("_APPSIGNAL_CPU_COUNT")).toEqual("1.5")
expect(env("_APPSIGNAL_DNS_SERVERS")).toEqual("8.8.8.8,8.8.4.4")
expect(env("_APPSIGNAL_ENABLE_HOST_METRICS")).toEqual("false")
expect(env("_APPSIGNAL_ENABLE_OPENTELEMETRY_HTTP")).toEqual("false")
Expand Down
15 changes: 14 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
BOOL_KEYS,
STRING_KEYS,
LIST_KEYS,
LIST_OR_BOOL_KEYS
LIST_OR_BOOL_KEYS,
FLOAT_KEYS
} from "./config/configmap"

/**
Expand Down Expand Up @@ -214,6 +215,18 @@ export class Configuration {
}
})

FLOAT_KEYS.forEach(k => {
const current = process.env[k]

if (current) {
const parsed = parseFloat(current)

if (!isNaN(parsed)) {
conf[ENV_TO_KEY_MAPPING[k]] = parsed
}
}
})

return conf
}

Expand Down
5 changes: 5 additions & 0 deletions src/config/configmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const ENV_TO_KEY_MAPPING: Record<string, keyof AppsignalOptions> = {
APPSIGNAL_APP_NAME: "name",
APPSIGNAL_BIND_ADDRESS: "bindAddress",
APPSIGNAL_CA_FILE_PATH: "caFilePath",
APPSIGNAL_CPU_COUNT: "cpuCount",
APPSIGNAL_DISABLE_DEFAULT_INSTRUMENTATIONS: "disableDefaultInstrumentations",
APPSIGNAL_DNS_SERVERS: "dnsServers",
APPSIGNAL_ENABLE_HOST_METRICS: "enableHostMetrics",
Expand Down Expand Up @@ -46,6 +47,7 @@ export const PRIVATE_ENV_MAPPING: Record<string, keyof AppsignalOptions> = {
_APPSIGNAL_APP_NAME: "name",
_APPSIGNAL_BIND_ADDRESS: "bindAddress",
_APPSIGNAL_CA_FILE_PATH: "caFilePath",
_APPSIGNAL_CPU_COUNT: "cpuCount",
_APPSIGNAL_DNS_SERVERS: "dnsServers",
_APPSIGNAL_ENABLE_HOST_METRICS: "enableHostMetrics",
_APPSIGNAL_ENABLE_OPENTELEMETRY_HTTP: "enableOpentelemetryHttp",
Expand Down Expand Up @@ -80,6 +82,7 @@ export const JS_TO_RUBY_MAPPING: Record<keyof AppsignalOptions, string> = {
bindAddress: "bind_address",
pushApiKey: "push_api_key",
caFilePath: "ca_file_path",
cpuCount: "cpu_count",
disableDefaultInstrumentations: "disable_default_instrumentations",
dnsServers: "dns_servers",
enableHostMetrics: "enable_host_metrics",
Expand Down Expand Up @@ -163,3 +166,5 @@ export const LIST_KEYS: string[] = [
export const LIST_OR_BOOL_KEYS: string[] = [
"APPSIGNAL_DISABLE_DEFAULT_INSTRUMENTATIONS"
]

export const FLOAT_KEYS: string[] = ["APPSIGNAL_CPU_COUNT"]
1 change: 1 addition & 0 deletions src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type AppsignalOptions = {
active: boolean
bindAddress: string
caFilePath: string
cpuCount: number
disableDefaultInstrumentations: DefaultInstrumentationName[] | boolean
dnsServers: string[]
enableHostMetrics: boolean
Expand Down

0 comments on commit e7cf661

Please sign in to comment.