Skip to content

Commit

Permalink
Merge pull request #21999 from storybookjs/shilman/cli-version-telemetry
Browse files Browse the repository at this point in the history
Telemetry: Add CLI version to context
  • Loading branch information
shilman committed Apr 11, 2023
2 parents e015ce0 + 9efb45c commit 4f967aa
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions code/frameworks/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@storybook/manager-api": "7.1.0-alpha.0",
"@storybook/node-logger": "7.1.0-alpha.0",
"@storybook/preview-api": "7.1.0-alpha.0",
"@storybook/telemetry": "7.1.0-alpha.0",
"@storybook/types": "7.1.0-alpha.0",
"@types/node": "^16.0.0",
"@types/react": "^16.14.34",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { sync as readUpSync } from 'read-pkg-up';
import { BrowserBuilderOptions, StylePreprocessorOptions } from '@angular-devkit/build-angular';

import { CLIOptions } from '@storybook/types';
import { getEnvConfig } from '@storybook/cli';
import { getEnvConfig, versions } from '@storybook/cli';
import { addToGlobalContext } from '@storybook/telemetry';

import { buildStaticStandalone, withTelemetry } from '@storybook/core-server';
import {
Expand All @@ -24,6 +25,8 @@ import { StandaloneOptions } from '../utils/standalone-options';
import { runCompodoc } from '../utils/run-compodoc';
import { errorSummary, printErrorDetails } from '../utils/error-handler';

addToGlobalContext('cliVersion', versions.storybook);

export type StorybookBuilderOptions = JsonObject & {
browserTarget?: string | null;
tsConfig?: string;
Expand Down
6 changes: 4 additions & 2 deletions code/frameworks/angular/src/builders/start-storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { sync as findUpSync } from 'find-up';
import { sync as readUpSync } from 'read-pkg-up';

import { CLIOptions } from '@storybook/types';
import { getEnvConfig } from '@storybook/cli';

import { getEnvConfig, versions } from '@storybook/cli';
import { addToGlobalContext } from '@storybook/telemetry';
import { buildDevStandalone, withTelemetry } from '@storybook/core-server';
import {
AssetPattern,
Expand All @@ -24,6 +24,8 @@ import { StandaloneOptions } from '../utils/standalone-options';
import { runCompodoc } from '../utils/run-compodoc';
import { printErrorDetails, errorSummary } from '../utils/error-handler';

addToGlobalContext('cliVersion', versions.storybook);

export type StorybookBuilderOptions = JsonObject & {
browserTarget?: string | null;
tsConfig?: string;
Expand Down
4 changes: 4 additions & 0 deletions code/lib/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import leven from 'leven';
import { sync as readUpSync } from 'read-pkg-up';

import { logger } from '@storybook/node-logger';
import { addToGlobalContext } from '@storybook/telemetry';

import type { CommandOptions } from './generators/types';
import { initiate } from './initiate';
Expand All @@ -20,6 +21,9 @@ import { generateStorybookBabelConfigInCWD } from './babel-config';
import { dev } from './dev';
import { build } from './build';
import { parseList, getEnvConfig } from './utils';
import versions from './versions';

addToGlobalContext('cliVersion', versions.storybook);

const pkg = readUpSync({ cwd: __dirname }).packageJson;
const consoleLogger = console;
Expand Down
4 changes: 4 additions & 0 deletions code/lib/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import versions from './versions';

export { versions };

export * from './js-package-manager';
export * from './utils';
2 changes: 2 additions & 0 deletions code/lib/telemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export { getStorybookCoreVersion } from './package-json';

export { getPrecedingUpgrade } from './event-cache';

export { addToGlobalContext } from './telemetry';

export const telemetry = async (
eventType: EventType,
payload: Payload = {},
Expand Down
17 changes: 14 additions & 3 deletions code/lib/telemetry/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ let tasks: Promise<any>[] = [];
// send telemetry
const sessionId = nanoid();

export const addToGlobalContext = (key: string, value: any) => {
globalContext[key] = value;
};

// context info sent with all events, provided
// by the app. currently:
// - cliVersion
const globalContext = {
inCI: Boolean(process.env.CI),
isTTY: process.stdout.isTTY,
} as Record<string, any>;

export async function sendTelemetry(
data: TelemetryData,
options: Partial<Options> = { retryDelay: 1000, immediate: false }
Expand All @@ -27,11 +39,10 @@ export async function sendTelemetry(
// flatten the data before we send it
const { eventType, payload, metadata, ...rest } = data;
const context = options.stripMetadata
? {}
? globalContext
: {
...globalContext,
anonymousId: getAnonymousProjectId(),
inCI: Boolean(process.env.CI),
isTTY: process.stdout.isTTY,
};
const eventId = nanoid();
const body = { ...rest, eventType, eventId, sessionId, metadata, payload, context };
Expand Down
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5636,6 +5636,7 @@ __metadata:
"@storybook/manager-api": 7.1.0-alpha.0
"@storybook/node-logger": 7.1.0-alpha.0
"@storybook/preview-api": 7.1.0-alpha.0
"@storybook/telemetry": 7.1.0-alpha.0
"@storybook/types": 7.1.0-alpha.0
"@types/node": ^16.0.0
"@types/react": ^16.14.34
Expand Down
7 changes: 7 additions & 0 deletions scripts/event-log-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk from 'chalk';
import assert from 'assert';
import fetch from 'node-fetch';
import { allTemplates } from '../code/lib/cli/src/sandbox-templates';
import versions from '../code/lib/cli/src/versions';
import { oneWayHash } from '../code/lib/telemetry/src/one-way-hash';

const PORT = process.env.PORT || 6007;
Expand Down Expand Up @@ -48,6 +49,12 @@ async function run() {

const [bootEvent, mainEvent] = events;

test(`both events should have cliVersion in context`, () => {
const cliVersion = versions.storybook;
assert.equal(bootEvent.context.cliVersion, cliVersion);
assert.equal(mainEvent.context.cliVersion, cliVersion);
});

test(`Should log a boot event with a payload of type ${eventType}`, () => {
assert.equal(bootEvent.eventType, 'boot');
assert.equal(bootEvent.payload?.eventType, eventType);
Expand Down

0 comments on commit 4f967aa

Please sign in to comment.