[local-sync] Ship first sync metrics to honeycomb

This commit is contained in:
Ben Gotow 2016-12-01 14:23:49 -08:00
parent ce27b0db2a
commit 47d8614ed7
5 changed files with 94 additions and 6 deletions

View file

@ -84,7 +84,7 @@ class AccountCard extends React.Component {
let firstSyncDuration = "Incomplete";
if (account.firstSyncCompletion) {
firstSyncDuration = (new Date(account.firstSyncCompletion) - new Date(account.createdAt)) / 1000;
firstSyncDuration = (new Date(account.firstSyncCompletion / 1) - new Date(account.createdAt)) / 1000;
}
const position = calcAcctPosition(this.props.count);

View file

@ -0,0 +1,58 @@
const {N1CloudAPI, NylasAPIRequest, AccountStore} = require('nylas-exports');
const os = require('os');
class SyncMetricsReporter {
constructor() {
this._logger = global.Logger.child();
}
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);
});
}
async reportEvent(info) {
if (!info.emailAddress) {
throw new Error("You must include email_address");
}
const {workingSetSize, privateBytes, sharedBytes} = process.getProcessMemoryInfo();
const percentCPU = await this.collectCPUUsage();
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;
info.processPercentCPU = percentCPU;
const req = new NylasAPIRequest({
api: N1CloudAPI,
options: {
path: `/ingest-metrics`,
method: 'POST',
body: info,
error: () => {
this._logger.warn(info, "Metrics Collector: Submission Failed.");
},
accountId: AccountStore.accountForEmail(info.emailAddress).id,
success: () => {
this._logger.info(info, "Metrics Collector: Submitted.");
},
},
});
req.run();
}
}
module.exports = new SyncMetricsReporter();

View file

@ -66,7 +66,6 @@ class SyncProcessManager {
this._workers[accountId] = null;
}
}
}
module.exports = new SyncProcessManager();

View file

@ -11,6 +11,7 @@ const {
const FetchFolderList = require('./imap/fetch-folder-list')
const FetchMessagesInFolder = require('./imap/fetch-messages-in-folder')
const SyncbackTaskFactory = require('./syncback-task-factory')
const SyncMetricsReporter = require('./sync-metrics-reporter');
class SyncWorker {
@ -27,6 +28,36 @@ class SyncWorker {
this._syncTimer = setTimeout(() => {
this.syncNow({reason: 'Initial'});
}, 0);
// setup metrics collection. We do this in an isolated way by hooking onto
// the database, because otherwise things get /crazy/ messy and I don't like
// having counters and garbage everywhere.
if (!account.firstSyncCompletion) {
this._logger.info("This is initial sync. Setting up metrics collection!");
let seen = 0;
db.Thread.addHook('afterCreate', 'metricsCollection', () => {
if (seen === 0) {
SyncMetricsReporter.reportEvent({
type: 'imap',
emailAddress: account.emailAddress,
msecToFirstThread: (Date.now() - new Date(account.createdAt).getTime()),
})
}
if (seen === 500) {
SyncMetricsReporter.reportEvent({
type: 'imap',
emailAddress: account.emailAddress,
msecToFirst500Threads: (Date.now() - new Date(account.createdAt).getTime()),
})
}
if (seen > 500) {
db.Thread.removeHook('afterCreate', 'metricsCollection')
}
seen += 1;
});
}
}
cleanup() {

View file

@ -1,7 +1,7 @@
const detectThread = require('./detect-thread')
const extractFiles = require('./extract-files')
const extractContacts = require('./extract-contacts')
const LocalDatabaseConnector = require('../shared/local-database-connector')
const detectThread = require('./detect-thread');
const extractFiles = require('./extract-files');
const extractContacts = require('./extract-contacts');
const LocalDatabaseConnector = require('../shared/local-database-connector');
const Queue = require('promise-queue');
const queue = new Queue(1, Infinity);