Skip to content

Commit

Permalink
feat: Enrich analytics payload with missing properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Mar 23, 2021
1 parent 320e642 commit a7b9498
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
44 changes: 40 additions & 4 deletions lib/utils/analytics/generatePayload.js
Expand Up @@ -3,7 +3,9 @@
const path = require('path');
const _ = require('lodash');
const isStandalone = require('../isStandaloneExecutable');
const userConfig = require('@serverless/utils/config');
const { triggeredDeprecations } = require('../logDeprecation');
const ci = require('ci-info');

const versions = {
'serverless': require('../../../package').version,
Expand Down Expand Up @@ -38,8 +40,36 @@ module.exports = serverless => {
);
})();

let timezone;
try {
timezone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch (err) {
// Pass silently
}

const ciName = (() => {
if (ci.isCI) {
if (ci.name) {
return ci.name;
}
return 'unknown';
}
return null;
})();

const userId = (() => {
// In this situation deployment relies on existence on company-wide access key
// and `userId` from config does not matter
if (process.env.SERVERLESS_ACCESS_KEY) {
return null;
}

return userConfig.get('userId');
})();

return {
cliName: 'serverless',
ciName,
config: {
provider: {
name: providerConfig.name,
Expand All @@ -55,15 +85,21 @@ module.exports = serverless => {
})),
})),
},
npmDependencies,
triggeredDeprecations: Array.from(triggeredDeprecations),
versions,
dashboard: {
userId,
orgUid: serviceConfig.orgUid,
},
frameworkLocalUserId: userConfig.get('frameworkId'),
installationType: (() => {
if (isStandalone) return 'global:standalone';
if (!serverless.isLocallyInstalled) return 'global:npm';
if (serverless.isInvokedByGlobalInstallation) return 'local:fallback';
return 'local:direct';
})(),
isDashboardEnabled: Boolean(serverless.enterpriseEnabled),
npmDependencies,
timestamp: Date.now(),
timezone,
triggeredDeprecations: Array.from(triggeredDeprecations),
versions,
};
};
33 changes: 30 additions & 3 deletions lib/utils/analytics/generatePayload.test.js
Expand Up @@ -40,6 +40,16 @@ describe('lib/utils/analytics/generatePayload', () => {
cliArgs: ['-v'],
}).then(({ serverless }) => {
const payload = generatePayload(serverless);
expect(payload).to.have.property('frameworkLocalUserId');
delete payload.frameworkLocalUserId;
expect(payload).to.have.property('timestamp');
delete payload.timestamp;
expect(payload).to.have.property('dashboard');
delete payload.dashboard;
expect(payload).to.have.property('timezone');
delete payload.timezone;
expect(payload).to.have.property('ciName');
delete payload.ciName;
expect(payload).to.deep.equal({
cliName: 'serverless',
config: {
Expand All @@ -58,7 +68,6 @@ describe('lib/utils/analytics/generatePayload', () => {
npmDependencies: ['fooDep', 'barDep', 'fooOpt', 'someDev', 'otherDev'],
triggeredDeprecations: [],
installationType: 'global:npm',
isDashboardEnabled: false,
versions,
});
})
Expand All @@ -71,6 +80,16 @@ describe('lib/utils/analytics/generatePayload', () => {
cliArgs: ['config'],
}).then(({ serverless }) => {
const payload = generatePayload(serverless);
expect(payload).to.have.property('frameworkLocalUserId');
delete payload.frameworkLocalUserId;
expect(payload).to.have.property('timestamp');
delete payload.timestamp;
expect(payload).to.have.property('dashboard');
delete payload.dashboard;
expect(payload).to.have.property('timezone');
delete payload.timezone;
expect(payload).to.have.property('ciName');
delete payload.ciName;
expect(payload).to.deep.equal({
cliName: 'serverless',
config: {
Expand All @@ -89,7 +108,6 @@ describe('lib/utils/analytics/generatePayload', () => {
npmDependencies: [],
triggeredDeprecations: [],
installationType: 'global:npm',
isDashboardEnabled: false,
versions,
});
}));
Expand All @@ -101,6 +119,16 @@ describe('lib/utils/analytics/generatePayload', () => {
cliArgs: ['config'],
}).then(({ serverless }) => {
const payload = generatePayload(serverless);
expect(payload).to.have.property('frameworkLocalUserId');
delete payload.frameworkLocalUserId;
expect(payload).to.have.property('timestamp');
delete payload.timestamp;
expect(payload).to.have.property('dashboard');
delete payload.dashboard;
expect(payload).to.have.property('timezone');
delete payload.timezone;
expect(payload).to.have.property('ciName');
delete payload.ciName;
expect(payload).to.deep.equal({
cliName: 'serverless',
config: {
Expand All @@ -116,7 +144,6 @@ describe('lib/utils/analytics/generatePayload', () => {
npmDependencies: [],
triggeredDeprecations: [],
installationType: 'local:fallback',
isDashboardEnabled: false,
versions,
});
}));
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -37,6 +37,7 @@
"cachedir": "^2.3.0",
"chalk": "^2.4.2",
"child-process-ext": "^2.1.1",
"ci-info": "^3.1.1",
"d": "^1.0.1",
"dayjs": "^1.9.5",
"decompress": "^4.2.1",
Expand Down

0 comments on commit a7b9498

Please sign in to comment.