mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-10-01 08:54:52 +08:00
This reverts commit a8fbcb0c93
.
# Conflicts:
# packages/client-app/internal_packages/thread-list/lib/thread-list-quick-actions.cjsx
# packages/client-app/internal_packages/thread-list/lib/thread-list.cjsx
# packages/client-app/src/flux/actions.es6
# packages/client-app/src/flux/stores/thread-list-actions-store.es6
# packages/client-app/src/global/nylas-exports.es6
# packages/isomorphic-core/src/metrics-reporter.es6
82 lines
2.4 KiB
JavaScript
82 lines
2.4 KiB
JavaScript
import os from 'os'
|
|
import {isClientEnv, isCloudEnv} from './env-helpers'
|
|
|
|
class MetricsReporter {
|
|
|
|
constructor() {
|
|
this._honey = null
|
|
|
|
if (isCloudEnv()) {
|
|
const LibHoney = require('libhoney') // eslint-disable-line
|
|
|
|
this._honey = new LibHoney({
|
|
writeKey: process.env.HONEY_WRITE_KEY,
|
|
dataset: process.env.HONEY_DATASET,
|
|
})
|
|
}
|
|
}
|
|
|
|
async collectCPUUsage() {
|
|
return new Promise((resolve) => {
|
|
const startUsage = process.cpuUsage();
|
|
const sampleDuration = 400;
|
|
setTimeout(() => {
|
|
const {user, system} = process.cpuUsage(startUsage);
|
|
const fractionToPrecent = 100.0;
|
|
resolve(Math.round((user + system) / (sampleDuration * 1000.0) * fractionToPrecent));
|
|
}, sampleDuration);
|
|
});
|
|
}
|
|
|
|
sendToHoneycomb(info) {
|
|
if (!this._honey) {
|
|
throw new Error('Metrics Reporter: Honeycomb is not available in this environment')
|
|
}
|
|
this._honey.sendNow(info);
|
|
}
|
|
|
|
async reportEvent(info) {
|
|
if (!info.nylasId) {
|
|
throw new Error("Metrics Reporter: You must include an nylasId");
|
|
}
|
|
const logger = global.Logger.child({accountEmail: info.emailAddress})
|
|
const {workingSetSize, privateBytes, sharedBytes} = process.getProcessMemoryInfo();
|
|
|
|
info.hostname = os.hostname();
|
|
info.cpus = os.cpus().length;
|
|
info.arch = os.arch();
|
|
info.platform = process.platform;
|
|
info.version = NylasEnv.getVersion();
|
|
info.processWorkingSetSize = workingSetSize;
|
|
info.processPrivateBytes = privateBytes;
|
|
info.processSharedBytes = sharedBytes;
|
|
|
|
try {
|
|
if (isClientEnv()) {
|
|
if (NylasEnv.inDevMode()) { return }
|
|
if (!info.accountId) {
|
|
throw new Error("Metrics Reporter: You must include an accountId");
|
|
}
|
|
|
|
const {N1CloudAPI, NylasAPIRequest} = require('nylas-exports') // eslint-disable-line
|
|
const req = new NylasAPIRequest({
|
|
api: N1CloudAPI,
|
|
options: {
|
|
path: `/ingest-metrics`,
|
|
method: 'POST',
|
|
body: info,
|
|
accountId: info.accountId,
|
|
},
|
|
});
|
|
await req.run()
|
|
} else {
|
|
this.sendToHoneycomb(info)
|
|
}
|
|
logger.log(info, "Metrics Reporter: Submitted.", info);
|
|
} catch (err) {
|
|
logger.warn("Metrics Reporter: Submission Failed.", {error: err, ...info});
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new MetricsReporter();
|