Skip to content

Commit c136350

Browse files
committed
cache node environment info
1 parent 4e8736c commit c136350

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

packages/browser/src/plugins/BrowserModuleInfoPlugin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import {
1111
export class BrowserModuleInfoPlugin implements IEventPlugin {
1212
public priority: number = 50;
1313
public name: string = "BrowserModuleInfoPlugin";
14-
private _cachedModules: ModuleInfo[] = null;
14+
private _modules: ModuleInfo[] = null;
1515

1616
public startup(context: PluginContext): Promise<void> {
17-
if (!this._cachedModules) {
18-
this._cachedModules = this.getModules();
17+
if (!this._modules) {
18+
this._modules = this.getModules();
1919
}
2020

2121
return Promise.resolve();
2222
}
2323

2424
public run(context: EventPluginContext): Promise<void> {
2525
const error = context.event.data[KnownEventDataKeys.Error];
26-
if (this._cachedModules?.length > 0 && !error?.modules) {
27-
error.modules = this._cachedModules;
26+
if (this._modules?.length > 0 && !error?.modules) {
27+
error.modules = this._modules;
2828
}
2929

3030
return Promise.resolve();

packages/node/src/plugins/NodeEnvironmentInfoPlugin.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ import {
3232
export class NodeEnvironmentInfoPlugin implements IEventPlugin {
3333
public priority: number = 80;
3434
public name: string = "NodeEnvironmentInfoPlugin";
35+
private _environmentInfo: EnvironmentInfo = null;
3536

3637
public run(context: EventPluginContext): Promise<void> {
37-
// PERF: Ensure module info is cached and rework below statement.
3838
if (!context.event.data[KnownEventDataKeys.EnvironmentInfo]) {
39-
const environmentInfo: EnvironmentInfo = this.getEnvironmentInfo(context);
40-
if (environmentInfo) {
41-
context.event.data[KnownEventDataKeys.EnvironmentInfo] = environmentInfo;
39+
const info: EnvironmentInfo = this.getEnvironmentInfo(context);
40+
if (info) {
41+
context.event.data[KnownEventDataKeys.EnvironmentInfo] = info;
4242
}
4343
}
4444

@@ -60,14 +60,25 @@ export class NodeEnvironmentInfoPlugin implements IEventPlugin {
6060
return ips.join(", ");
6161
}
6262

63+
function populateMemoryAndUptimeInfo(ei: EnvironmentInfo) {
64+
ei.process_memory_size = memoryUsage().heapTotal;
65+
ei.total_physical_memory = totalmem();
66+
ei.available_physical_memory = freemem();
67+
ei.data.loadavg = loadavg();
68+
ei.data.uptime = uptime();
69+
}
70+
6371
if (!cpus) {
6472
return null;
6573
}
6674

67-
const environmentInfo: EnvironmentInfo = {
75+
if (this._environmentInfo) {
76+
populateMemoryAndUptimeInfo(this._environmentInfo);
77+
return this._environmentInfo;
78+
}
79+
80+
const info: EnvironmentInfo = {
6881
processor_count: cpus().length,
69-
total_physical_memory: totalmem(),
70-
available_physical_memory: freemem(),
7182
command_line: argv.join(" "),
7283
process_name: (title || "").replace(/[\uE000-\uF8FF]/g, ""),
7384
process_id: pid + "",
@@ -79,26 +90,27 @@ export class NodeEnvironmentInfoPlugin implements IEventPlugin {
7990
// install_id: "",
8091
runtime_version: version,
8192
data: {
82-
loadavg: loadavg(),
8393
platform: platform(),
84-
tmpdir: tmpdir(),
85-
uptime: uptime(),
94+
tmpdir: tmpdir()
8695
},
8796
};
8897

8998
const config = context.client.config;
9099
if (config.includeMachineName) {
91-
environmentInfo.machine_name = hostname();
100+
info.machine_name = hostname();
92101
}
93102

94103
if (config.includeIpAddress) {
95-
environmentInfo.ip_address = getIpAddresses();
104+
info.ip_address = getIpAddresses();
96105
}
97106

98107
if (endianness) {
99-
environmentInfo.data.endianness = endianness();
108+
info.data.endianness = endianness();
100109
}
101110

102-
return environmentInfo;
111+
populateMemoryAndUptimeInfo(info);
112+
113+
this._environmentInfo = info;
114+
return this._environmentInfo;
103115
}
104116
}

0 commit comments

Comments
 (0)