Skip to content

Commit

Permalink
tweak: send command diez was run with in analytics ping and crash re…
Browse files Browse the repository at this point in the history
…ports (#249)
  • Loading branch information
gumptious committed Nov 14, 2019
1 parent eed04ed commit 462119c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/cli-core/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tslint:disable no-var-requires */
import {emitDiagnostics, enableAnalytics, Registry} from '@diez/storage';
import {emitDiagnostics, enableAnalytics, getCommandArguments, Registry} from '@diez/storage';
import {captureException, configureScope, getCurrentHub, init as initSentry} from '@sentry/node';
import {args, command, on, outputHelp, parse, version} from 'commander';
import {
Expand Down Expand Up @@ -208,6 +208,7 @@ export const run = async (bootstrapRoot?: string) => {

configureScope((scope) => {
scope.setUser({id: global.analyticsUuid});
scope.setExtra('command_arguments', getCommandArguments());
});
}

Expand Down
2 changes: 2 additions & 0 deletions packages/cli-core/test/cli.analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {assignMock} from '@diez/test-utils';
const mockEmitDiagnostics = jest.fn().mockResolvedValue(undefined);
const mockEnableAnalytics = jest.fn();
const mockDisableAnalytics = jest.fn();
const mockGetCommandArguments = jest.fn();
jest.doMock('@diez/storage', () => ({
Registry: {
get (key: string) {
Expand All @@ -19,6 +20,7 @@ jest.doMock('@diez/storage', () => ({
emitDiagnostics: mockEmitDiagnostics,
enableAnalytics: mockEnableAnalytics,
disableAnalytics: mockDisableAnalytics,
getCommandArguments: mockGetCommandArguments,
}));

import {ModuleWrappedCliAction} from '../src';
Expand Down
7 changes: 6 additions & 1 deletion packages/cli-core/test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
class MockSentry {
static mockClose = jest.fn();
static mockSetUser = jest.fn();
static mockSetExtra = jest.fn();
static mockGetClient = jest.fn().mockImplementation((() => ({close: MockSentry.mockClose})));
static captureException = jest.fn();
static init = jest.fn();
static configureScope = jest.fn().mockImplementation((callback) => callback({setUser: MockSentry.mockSetUser}));
static configureScope = jest.fn().mockImplementation((callback) => callback({
setUser: MockSentry.mockSetUser,
setExtra: MockSentry.mockSetExtra,
}));
static getCurrentHub = jest.fn().mockImplementation(() => ({getClient: MockSentry.mockGetClient}));
}
jest.doMock('@sentry/node', () => MockSentry);
Expand Down Expand Up @@ -88,6 +92,7 @@ describe('cli', () => {
});

await run();
expect(MockSentry.mockSetExtra).toHaveBeenCalledWith('command_arguments', 'foobar --stringParam foo');

process.nextTick(() => {
expect(eventTrap).toHaveBeenCalledWith({
Expand Down
6 changes: 6 additions & 0 deletions packages/storage/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {v4} from 'uuid';
import {DiezDiagnostics} from './api';
import {Registry} from './registry';

/**
* Provides the arguments Diez was run with.
*/
export const getCommandArguments = () => process.argv.slice(2).join(' ');

/**
* Creates a diagnostics payload for Diez analytics.
* @internal
Expand All @@ -23,6 +28,7 @@ const getDiagnosticsPayload = async (
uuid,
properties: {
diezVersion,
commandArguments: getCommandArguments(),
platform: platform(),
arch: arch(),
nodeVersion: process.version,
Expand Down
2 changes: 2 additions & 0 deletions packages/storage/test/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('analytics', () => {
await enableAnalytics();
expect(await Registry.get('analyticsEnabled')).toBe(true);
expect(await Registry.get('uuid')).toBe('secret-id');
process.argv = ['node', 'diez', 'do', '--something'];
await emitDiagnostics('some-event', '10.10.10', {extra: 'data'});
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(
Expand All @@ -52,6 +53,7 @@ describe('analytics', () => {
uuid: 'secret-id',
properties: {
diezVersion: '10.10.10',
commandArguments: 'do --something',
platform: platform(),
arch: arch(),
nodeVersion: process.version,
Expand Down

0 comments on commit 462119c

Please sign in to comment.