Skip to content

Commit 4f967aa

Browse files
authored
Merge pull request #21999 from storybookjs/shilman/cli-version-telemetry
Telemetry: Add CLI version to context
2 parents e015ce0 + 9efb45c commit 4f967aa

File tree

9 files changed

+41
-6
lines changed

9 files changed

+41
-6
lines changed

code/frameworks/angular/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@storybook/manager-api": "7.1.0-alpha.0",
5050
"@storybook/node-logger": "7.1.0-alpha.0",
5151
"@storybook/preview-api": "7.1.0-alpha.0",
52+
"@storybook/telemetry": "7.1.0-alpha.0",
5253
"@storybook/types": "7.1.0-alpha.0",
5354
"@types/node": "^16.0.0",
5455
"@types/react": "^16.14.34",

code/frameworks/angular/src/builders/build-storybook/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { sync as readUpSync } from 'read-pkg-up';
1313
import { BrowserBuilderOptions, StylePreprocessorOptions } from '@angular-devkit/build-angular';
1414

1515
import { CLIOptions } from '@storybook/types';
16-
import { getEnvConfig } from '@storybook/cli';
16+
import { getEnvConfig, versions } from '@storybook/cli';
17+
import { addToGlobalContext } from '@storybook/telemetry';
1718

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

28+
addToGlobalContext('cliVersion', versions.storybook);
29+
2730
export type StorybookBuilderOptions = JsonObject & {
2831
browserTarget?: string | null;
2932
tsConfig?: string;

code/frameworks/angular/src/builders/start-storybook/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { sync as findUpSync } from 'find-up';
1313
import { sync as readUpSync } from 'read-pkg-up';
1414

1515
import { CLIOptions } from '@storybook/types';
16-
import { getEnvConfig } from '@storybook/cli';
17-
16+
import { getEnvConfig, versions } from '@storybook/cli';
17+
import { addToGlobalContext } from '@storybook/telemetry';
1818
import { buildDevStandalone, withTelemetry } from '@storybook/core-server';
1919
import {
2020
AssetPattern,
@@ -24,6 +24,8 @@ import { StandaloneOptions } from '../utils/standalone-options';
2424
import { runCompodoc } from '../utils/run-compodoc';
2525
import { printErrorDetails, errorSummary } from '../utils/error-handler';
2626

27+
addToGlobalContext('cliVersion', versions.storybook);
28+
2729
export type StorybookBuilderOptions = JsonObject & {
2830
browserTarget?: string | null;
2931
tsConfig?: string;

code/lib/cli/src/generate.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import leven from 'leven';
66
import { sync as readUpSync } from 'read-pkg-up';
77

88
import { logger } from '@storybook/node-logger';
9+
import { addToGlobalContext } from '@storybook/telemetry';
910

1011
import type { CommandOptions } from './generators/types';
1112
import { initiate } from './initiate';
@@ -20,6 +21,9 @@ import { generateStorybookBabelConfigInCWD } from './babel-config';
2021
import { dev } from './dev';
2122
import { build } from './build';
2223
import { parseList, getEnvConfig } from './utils';
24+
import versions from './versions';
25+
26+
addToGlobalContext('cliVersion', versions.storybook);
2327

2428
const pkg = readUpSync({ cwd: __dirname }).packageJson;
2529
const consoleLogger = console;

code/lib/cli/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
import versions from './versions';
2+
3+
export { versions };
4+
15
export * from './js-package-manager';
26
export * from './utils';

code/lib/telemetry/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export { getStorybookCoreVersion } from './package-json';
1515

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

18+
export { addToGlobalContext } from './telemetry';
19+
1820
export const telemetry = async (
1921
eventType: EventType,
2022
payload: Payload = {},

code/lib/telemetry/src/telemetry.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ let tasks: Promise<any>[] = [];
1616
// send telemetry
1717
const sessionId = nanoid();
1818

19+
export const addToGlobalContext = (key: string, value: any) => {
20+
globalContext[key] = value;
21+
};
22+
23+
// context info sent with all events, provided
24+
// by the app. currently:
25+
// - cliVersion
26+
const globalContext = {
27+
inCI: Boolean(process.env.CI),
28+
isTTY: process.stdout.isTTY,
29+
} as Record<string, any>;
30+
1931
export async function sendTelemetry(
2032
data: TelemetryData,
2133
options: Partial<Options> = { retryDelay: 1000, immediate: false }
@@ -27,11 +39,10 @@ export async function sendTelemetry(
2739
// flatten the data before we send it
2840
const { eventType, payload, metadata, ...rest } = data;
2941
const context = options.stripMetadata
30-
? {}
42+
? globalContext
3143
: {
44+
...globalContext,
3245
anonymousId: getAnonymousProjectId(),
33-
inCI: Boolean(process.env.CI),
34-
isTTY: process.stdout.isTTY,
3546
};
3647
const eventId = nanoid();
3748
const body = { ...rest, eventType, eventId, sessionId, metadata, payload, context };

code/yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -5636,6 +5636,7 @@ __metadata:
56365636
"@storybook/manager-api": 7.1.0-alpha.0
56375637
"@storybook/node-logger": 7.1.0-alpha.0
56385638
"@storybook/preview-api": 7.1.0-alpha.0
5639+
"@storybook/telemetry": 7.1.0-alpha.0
56395640
"@storybook/types": 7.1.0-alpha.0
56405641
"@types/node": ^16.0.0
56415642
"@types/react": ^16.14.34

scripts/event-log-checker.ts

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import chalk from 'chalk';
33
import assert from 'assert';
44
import fetch from 'node-fetch';
55
import { allTemplates } from '../code/lib/cli/src/sandbox-templates';
6+
import versions from '../code/lib/cli/src/versions';
67
import { oneWayHash } from '../code/lib/telemetry/src/one-way-hash';
78

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

4950
const [bootEvent, mainEvent] = events;
5051

52+
test(`both events should have cliVersion in context`, () => {
53+
const cliVersion = versions.storybook;
54+
assert.equal(bootEvent.context.cliVersion, cliVersion);
55+
assert.equal(mainEvent.context.cliVersion, cliVersion);
56+
});
57+
5158
test(`Should log a boot event with a payload of type ${eventType}`, () => {
5259
assert.equal(bootEvent.eventType, 'boot');
5360
assert.equal(bootEvent.payload?.eventType, eventType);

0 commit comments

Comments
 (0)